Simple folder actions question

So why does this work:

tell application "Finder"
	set theFolder to "Drobo:Applied Perspective LLC:statements:Printed"
	set addedItems to every file of folder theFolder
end tell
repeat with anItem in addedItems
	if the name of anItem contains "estimate" then
		move anItem to "Drobo:Applied Perspective LLC:statements:Printed:Estimates"
	else if name of anItem contains "invoice" then
		move anItem to "Drobo:Applied Perspective LLC:statements:Printed:Invoices"
	else if name of anItem contains "payment" then
		move anItem to "Drobo:Applied Perspective LLC:statements:Printed:Payments"
	end if
	
end repeat

But this does not:

on adding folder items to thisFolder after receiving addedItems
	repeat with anItem in addedItems
		if the name of anItem contains "estimate" then
			move anItem to "Drobo:Applied Perspective LLC:statements:Printed:Estimates"
		else if name of anItem contains "invoice" then
			move anItem to "Drobo:Applied Perspective LLC:statements:Printed:Invoices"
		else if name of anItem contains "payment" then
			move anItem to "Drobo:Applied Perspective LLC:statements:Printed:Payments"
		end if
		
	end repeat
end adding folder items to

I’m sure I’m missing something incredibly simple, but after dealing with network issues all day, my brain just isn’t seeing it.

Hi,

does the first script really work?

Applescript itself has no idea to retrieve the name of a file nor to move it somewhere.
The Finder (or System Events) is supposed to do it


on adding folder items to thisFolder after receiving addedItems
	repeat with anItem in addedItems
		tell application "Finder"
			if the name of anItem contains "estimate" then
				move anItem to folder "Drobo:Applied Perspective LLC:statements:Printed:Estimates:"
			else if name of anItem contains "invoice" then
				move anItem to folder "Drobo:Applied Perspective LLC:statements:Printed:Invoices:"
			else if name of anItem contains "payment" then
				move anItem to folder "Drobo:Applied Perspective LLC:statements:Printed:Payments:"
			end if
		end tell
	end repeat
end adding folder items to


The first script really does work, with or without a Finder tell block around the if statement. The script you provided also does not work. I have also tried putting Finder tell blocks around each individual Move statement as well as wrapping the whole repeat statement in one. I’ve tried making anItem an alias. I’ve tried setting variables for the folders instead of writing them out in the Move commands. Nothing I have done has been able to trigger the sort and move I’m searching for as a folder action.

Interesting that AppleScript can move files without involving Finder or System Events.
This might be new.
In your first script the class of the files is Finder file specifier, whose name can be read.

The syntax of my script is correct, I guess there is a problem with the folder action

Yes. That first script is about as wrong as it can be, but it works! I think it’s because of the coincidence of each ‘file’ being a FInder reference (which causes it to be offered to the Finder to handle), the ‘name’ and ‘move’ keywords having standard tokens which the compiler can supply without reference to a particular application, and the Finder being forgiving about being given text paths instead of proper specifiers.

Agreed. Stefan’s version is perfectly correct and should work if Folder Actions are enabled in the host user, the script’s in a folder called “Folder Action Scripts” in the user’s “/Library/Scripts/” folder, and the script’s attached as a Folder Action to the folder to which the items are to be added.

As blasphemous as it might be for a scripter to suggest this, I’ve long abandonned Folder Actions as flakey and unreliable. Instead, I use Noodlesoft’s Hazel, a pref pane that enables setting up rules for any folder.

Hello

This edited script works flawlessly :


(*
set thisFolder to (path to desktop as text) & "Printed:" as alias
my adding_folder_items(thisFolder, {})

on adding_folder_items(thisFolder, {})
*)
on adding folder items to thisFolder after receiving addedItems
	(*
Here thisFolder is an alias so we can't code « of folder thisFolder » *)
	
	tell application "Finder"
		set added_Items to every file of thisFolder
		set this_Folder to thisFolder as text
		repeat with anItem in added_Items
			set anItem to anItem as alias
			if the name of anItem contains "estimate" then
				try
					move anItem to folder (this_Folder & "Estimates")
				end try
			else if name of anItem contains "invoice" then
				try
					move anItem to folder (this_Folder & "Invoices")
				end try
			else if name of anItem contains "payment" then
				try
					move anItem to folder (this_Folder & "Payments")
				end try
			end if
		end repeat
	end tell
end adding folder items to
--end adding_folder_items

I left some extraneous instructions (disabled of course) which I use to test this kind of scripts.

I don’ treat the addedItems set of files because sometimes one or two files aren’t passed because the Finder is a bit lazy to treat folder actions.

I added try/end try blocks because I don’t know what you want to do if there is already a file xxx estimate.yyy in the Estimates subfolder.

I wish to add that I’m puzzled by the behavior of your original applet.

Reading it I was sure that it would fail because the move instructions and the one extracting the name of each files are out of the tell application “Finder” block.

When I ran it, I discovered that AppleScript execute the script as if the end tell was not where it is written but as if it was at the very end of it.

So, this day is a good one : I learnt something !

I’m not sure that I was clear enough so I post the log report issued by the original script (with a single change :
I was forced to remove the redondant word folder in the instruction :

set addedItems to every file of folder theFolder

It must be :

set addedItems to every file of theFolder

tell current application
path to desktop as text
→ “Macintosh HD:Users:xxxxxx:Desktop:”
end tell
tell application “Finder”
get every file of alias “Macintosh HD:Users:xxxxxx:Desktop:Printed:”
→ {document file “.DS_Store” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk, document file “CodeSigningGuide estimate co.pdf” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk, document file “CodeSigningGuide estimate copie.pdf” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk, document file “CodeSigningGuide.pdf” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk, document file “Convert to header row copie.scpt” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk}
get name of document file “.DS_Store” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk
→ “.DS_Store”
get name of document file “.DS_Store” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk
→ “.DS_Store”
get name of document file “.DS_Store” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk
→ “.DS_Store”
get name of document file “CodeSigningGuide estimate co.pdf” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk
→ “CodeSigningGuide estimate co.pdf”
move document file “CodeSigningGuide estimate co.pdf” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk to “Macintosh HD:Users:xxxxxx:Desktop:Printed:Estimates”
→ document file “CodeSigningGuide estimate co.pdf” of folder “Estimates” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk
→ error number 0
get name of document file “CodeSigningGuide estimate copie.pdf” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk
→ “CodeSigningGuide estimate copie.pdf”
move document file “CodeSigningGuide estimate copie.pdf” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk to “Macintosh HD:Users:xxxxxx:Desktop:Printed:Estimates”
→ document file “CodeSigningGuide estimate copie.pdf” of folder “Estimates” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk
→ error number 0
get name of document file “CodeSigningGuide.pdf” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk
→ “CodeSigningGuide.pdf”
get name of document file “CodeSigningGuide.pdf” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk
→ “CodeSigningGuide.pdf”
get name of document file “CodeSigningGuide.pdf” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk
→ “CodeSigningGuide.pdf”
get name of document file “Convert to header row copie.scpt” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk
→ “Convert to header row copie.scpt”
get name of document file “Convert to header row copie.scpt” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk
→ “Convert to header row copie.scpt”
get name of document file “Convert to header row copie.scpt” of folder “Printed” of folder “Desktop” of folder “xxxxxx” of folder “Users” of startup disk
→ “Convert to header row copie.scpt”
end tell

Yvan KOENIG (VALLAURIS, France) lundi 27 août 2012 17:07:10

Well, there is definitely something wrong with the folder action itself. I replaced it with this:

on adding folder items to thisFolder after receiving addedItems
	beep 4
end adding folder items to

The first time it beeped 3 times, the second time it beeped once, and after that nothing at all would happen. So I turned off folder actions, deleted the action, deleted the folder from the list and then went back in and added everything again, using Stefan’s script, and it’s working as far as I can tell. I guess only time will tell.

Adam, I will look into Hazel. It seems like it may be a good alternative for some of the simpler stuff I traditionally use folder actions for.

You are far from alone in that…