Help with Folder Action Script

Hi

i have made a script to put files in a folder,shows a dialog, give it a name to make a new folder and then move it to another folder with the items. I use the handler “on adding” but the dialogwindow repeating itself again and i do not know how stop this.

What do i do wrong ???

on adding folder items to thisFolder after receiving addedItems
repeat with anItem in addedItems
tell application “Finder”
activate
display dialog “Enter Order nummer” with icon caution �
default answer “”
copy the result to myReSult
if the button returned of myReSult is “OK” then
set propertie to the text returned of myReSult
tell application “Finder”
activate
set my_folder to make new folder at folder “Advertentie’s” of desktop with properties {name:the text returned of myReSult}
tell application “Finder” to set fileList to every file of folder “Advertentie’s”
move the fileList to my_folder
tell application “Finder”
move my_folder to folder “Dolf HD:Advertenties in:”
end tell
end tell
end if
end tell
end repeat
end adding folder items to

I’m not clear on what you want the script to do when items are added to the watched folder. Can you elaborate?

– Rob

Hi Rob,

The following happens, when i put some items in de watchedfolder the script makes
a new folder with a ordernumber. (wich i enter in the dialog window before) and put in the items wich i added and then the new folder moves to another folder till then
it works well, but then the script shows “again” a dialog window with the same question
(enter a ordernumber) while the script already did what it wanted and this i do not want because de new folder is already gone. so the script ask me two or more times
to make a new folder this depends how many items i added. it keep repeating to ask
me to make a new folder.

dolf

Hi Dolf,
Can not test, is this what you want?:

on adding folder items to thisFolder after receiving addedItems
display dialog “Enter Order nummer” with icon caution ¬
default answer “”
copy the result to myReSult
if the button returned of myReSult is “OK” then
set propertie to the text returned of myReSult
end if
repeat with anItem in addedItems
tell application “Finder”
activate
set my_folder to make new folder at folder “Advertentie’s” of desktop with properties {name:the text returned of myReSult}
set fileList to every file of folder “Advertentie’s”
move the fileList to my_folder
move my_folder to folder “Dolf HD:Advertenties in:”
end tell
end repeat
end adding folder items to

this is your own script. I only set the “display dialog” out of the repeat statement and
you were calling too much the Finder. One call is enough.

Is the folder action attached to the folder named “Advertentie’s”? If so, it appears that the repeat loop can be eliminated…

– Rob

Hi

Yes Rob this script is attached to the folder “Advertentie’s” and i removed the repeat loop but still an second dialogbox “Enter Order nummer” appears. I also used the the second thought to move the repeat code but again the dailogbox is there again ??? it seems that
the “on adding” code notice that there is a second “thing” in the folder, mayby the
new folder ???

Dolf

Yes, it notice that your new folder is added.
I don’t know if it’s exactly what you want , but this works :



on adding folder items to thisFolder after receiving addedItems
	display dialog "Enter Order nummer" with icon caution ¬
		default answer ""
	copy the result to myReSult
	if the button returned of myReSult is "OK" then
		set propertie to the text returned of myReSult
	end if
	
	tell application "Finder"
		activate
		set my_folder to make new folder at folder "Advertentie's in" of desktop with properties {name:the text returned of myReSult}
		set fileList to every file of folder thisFolder
		move the fileList to my_folder
	end tell
	
end adding folder items to


Pffff…

what a headbreaker
It is still coming up with a second dialogbox, maybe i have to put in a third folder ??

dolf

Yes, I would use a folder other than the one with the folder action so that the newly created folders don’t trigger the folder action. This should solve the problem.

– Rob

Does this work? During testing, it ignored folders that were created by the script but it will fail if only a folder is added to the attached folder.

on adding folder items to thisFolder after receiving addedItems
	repeat with item_ in addedItems
		if folder of (info for item_) is false then
			set myReSult to (display dialog "Enter Order nummer" with icon caution ¬
				default answer "")
			
			if the button returned of myReSult is "OK" and ¬
				text returned of myReSult is not "" then
				set propertie to the text returned of myReSult
			else
				if the button returned of myReSult is "OK" and ¬
					text returned of myReSult is "" then
					display dialog "No folder name was entered!" buttons ¬
						{"OK"} default button 1 with icon 0
					error number -128 -- terminate further execution
				end if
			end if
			
			tell application "Finder"
				activate
				try
					set my_folder to (make new folder at ¬
						thisFolder with properties {name:propertie})
				on error e
					display dialog e buttons {"OK"} default button 1 with icon 0
					error number -128 -- terminate further execution
				end try
				move addedItems to my_folder
			end tell
			exit repeat
		end if
	end repeat
end adding folder items to

– Rob

Rob and DeBloem,

I think i solved the problem by using a third folder and it’s works.
at first i made this code to put the files or folder somewhere.

on adding folder items to thisFolder after receiving addedItems
repeat with anItem in addedItems
tell application “Finder”
activate
move every file of folder “Dolf HD:Desktop Folder:Werkmap: ADVERTENIE ZONDER:” to folder ¬
“Dolf HD:Desktop Folder:TF:”
move every folder of folder “Dolf HD:Desktop Folder:Werkmap: ADVERTENIE ZONDER:” to folder ¬
“Dolf HD:Desktop Folder:TF:”
end tell
exit repeat
end repeat
end adding folder items to

Then i do the job in another folder

on adding folder items to thisFolder after receiving addedItems
tell application “Finder”
display dialog “Enter Order nummer” with icon caution ¬
default answer “”
copy the result to myReSult
if the button returned of myReSult is “OK” then
set propertie to the text returned of myReSult
set my_folder to (make new folder at folder “TF:” of desktop with properties ¬
{name:the text returned of myReSult})
if exists items in folder “TF” then
set theFolderList to folder “Dolf HD:Desktop Folder:TF:”
set theNameList to “”
repeat with thisItem in theFolderList
set theNameList to (the name of thisItem)
move item (the name of thisItem) of folder “Dolf HD:Desktop Folder:TF:” to my_folder
set label index of my_folder to 1
move my_folder to folder “Dolf HD:Desktop Folder:Werkmap: ADVERTENTIE MET:”
exit repeat
end repeat
end if
end if
end tell
end adding folder items to

maybe it’s not it, but it is working for me, any comment is welcome.

thank you for your help.

Here’s a slightly modified version of your second script. It simply requires that something is actually entered in the dialog if the OK button is chosen.

on adding folder items to thisFolder after receiving addedItems
	tell application "Finder"
		repeat -- make sure that something is entered in the dialog
			set myReSult to (display dialog "Enter Order nummer" with icon caution ¬
				default answer "")
			if text returned of myReSult is not "" then exit repeat
		end repeat
		
		if the button returned of myReSult is "OK" then
			set propertie to the text returned of myReSult
			set my_folder to (make new folder at folder "TF:" of desktop with properties ¬
				{name:the text returned of myReSult})
			if exists items in folder "TF" then
				set theFolderList to folder "Dolf HD:Desktop Folder:TF:"
				set theNameList to ""
				repeat with thisItem in theFolderList
					set theNameList to (the name of thisItem)
					move item (the name of thisItem) of folder "Dolf HD:Desktop Folder:TF:" to my_folder
					set label index of my_folder to 1
					move my_folder to folder "Dolf HD:Desktop Folder:Werkmap: ADVERTENTIE MET:"
					exit repeat
				end repeat
			end if
		end if
	end tell
end adding folder items to

– Rob

And now a modified version of your first script :


on adding folder items to thisFolder after receiving addedItems 

tell application "Finder" 
activate 
move entire contents of folder "Dolf HD:Desktop Folder:Werkmap: ADVERTENIE ZONDER:" to folder ¬ 
"Dolf HD:Desktop Folder:TF:" 

end adding folder items to 

N.B. : in your second script, you don’t need “exit repeat” before “end repeat”( and it’s confusing).
Debloem

Hi Rob and DeBloem,

Many Many thank you again for your help :smiley:
it’s working great !!!

thank you

dolf