Folder Action to move files.... doesn't!

Ok, I found a bit of AppleScript that looks like it should do exactly what I need, but after attaching it to a folder as a Folder Action, nothing happens.

This is the code I have:


set myHome to (path to home folder) as string
set destination_path to myHome & "test upload folder 2"

on adding folder items to this_folder after receiving these_items
        tell application "Finder"
                move these_items to folder destination_path with replacing
        end tell
end adding folder items to

Please help, this is frustrating!!

-brian

Model: PPC Mini
AppleScript: 1.10.7
Browser: Firefox 2.0.0.11
Operating System: Mac OS X (10.4)

Make sure that the name you’ve specified and the actual folder are spelled/typed identically. In fact, for a test, you could use choose folder and select your test folder to force the script to interact with you while you’re testing. That would at least tell you if it’s being activated!

Also, remember that you can use a try…on error block to trap for errors and report them to you. Some applications don’t report Applescript errors like they probably should, and often scripts will die a silent death for no apparent reason, hence the reason for using try.

Hi, Brian.

Your first two lines, which set the destination folder, aren’t inside ” and aren’t accessed by ” the folder action handler, so they’re not executed when the script’s run as a folder action. Moving them inside the handler solves the problem:

on adding folder items to this_folder after receiving these_items
	set myHome to (path to home folder as Unicode text)
	set destination_path to myHome & "test upload folder 2"
	
	tell application "Finder"
		move these_items to folder destination_path with replacing
	end tell
end adding folder items to

It so happens that the Finder has its own reference for the user’s home folder, so you could write instead:

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		move these_items to folder "test upload folder 2" of home with replacing
	end tell
end adding folder items to

Good catch Nigel, I didn’t see it either (and I looked right at it!).

That’s good to know, I’ll keep that in mind in the future.

Ok, so trying this second bit here, I still get no action on the addition of items to that folder.

I tried Kevin’s idea with ‘choose folder’, but that doesn’t do anything either.

Could I possibly be attaching Folder Actions incorrectly or something? This code is simple enough that I don’t see any reason why it shouldn’t just work.

Thanks!!

-brian

ps: this forum editor needs a vi mode, I keep trying to do vi movement keys with it, which ends up with me typing lots of silly looking sentences. :wink:

You’re right, it should work. It works fine for me here. Try a right-click on your folder that this is supposed to work with and choose “Configure Folder Actions.” Make sure that “Enable Folder Actions” is checked and that the folder in question is listed as having a Folder Action and script associated with it.

And I believe the script itself has to be in a folder called “Folder Action Scripts” in your user’s “Scripts” folder. (/Users/username/Library/Scripts/Folder Action Scripts/). If the folder doesn’t exist, you can create it yourself.

I bet this is the problem as the script isn’t located there. I’ll try moving it, and I’ll let you know what happens.

Thanks!!

-brian

And that was exactly the problem. Thanks!!

-brian

Ok, one last question, is the finder command ‘move’ blocking? Will it move all the files in that folder before continuing on?

-brian

I’m not sure on this, if you have a large number of very large files, Applescript may hand the move command to the Finder and move on, but usually a script should wait for the command to finish. If you find that it doesn’t, you can use a with timeout block or a delay to wait a given amount of time.

Also, keep in mind if you’re moving items to another disk’s folder, that move won’t actually move the items, it will copy them, leaving the original items on the first disk. You’ll have to use delete to move them to the trash.