Need help with a Folder Action for moving new files

I have an applescript that moves all files with the extension .jpg from one folder to another. It works fine if I click run from the script editor or if I use it manually from the menubar scripts dropdown.

What doesn’t work is I want newly added .jpgs to automatically be moved when they are created in the original location by another app so I tried to add the script as a folder action but it does not automatically move .jpgs when they are added to the folder. The source finder window blinks as though the script is running but the files are not moved.

What am I doing wrong?

Here is my script.


tell application "Finder"
	try
		set the this_folder to "RacerX:Users:cameron:Desktop:something" as alias
		set the that_folder to "RacerX:Users:cameron:Desktop:another" as alias
		move (every file of this_folder whose name contains ".jpg") to that_folder
	on error
		move (every file of this_folder whose name contains ".jpg") to that_folder
	end try
end tell

Also what would be the correct code so that I can use the script on any folder I attach it to rather than hard coding a specific source folder?

Hi,

a folder action has a special syntax
this should do, what you want:

on adding folder items to this_folder after receiving these_items
	set that_folder to ((path to desktop as Unicode text) & "another:")
	try -- abort if the folder "another" doesn't exist
		repeat with oneItem in these_items
			tell application "Finder" to if name extension of oneItem is "jpg" then move oneItem to folder that_folder
		end repeat
	end try
end adding folder items to

the script must be saved in Library/Scripts/Folder Action Scripts or ~/Library/Scripts/Folder Action Scripts,
then enable Folder Actions and attach the script to the folder

Thank you very much Stefan!!

Works perfectly and now I understand what to do to make a folder action script work so I can complete a couple other scripts.

Have a good day. :slight_smile: