Folder actions script: Sort Downloads folder

Could someone have a look at this Automator action script and give me any suggestions as to why it doesn’t work?

http://min.us/idfDps.png

I want to move anything added to the Downloads folder which is an image to an Images folder, anything which is an archive to a Zip folder, PDFs to a PDF folder and so on and so forth, but I can’t seem to get anything to work as expected.

Please help! Thanks.

EDIT: Should also say, I tried using the Find Finder Items action instead and although it worked as expected, it also moved everything in sub-folders of the Downloads folder as well, which is unwanted.

Hi,

add your path’s and edit the suffixes as you like …

save in folder action scripts and attach it to your folder …

on adding folder items to thisFolder after receiving these_items
	set SourceFolder to quoted form of POSIX path of (thisFolder as text)
	set thePics to {{"*.jpg", "*.tif", "*.psd", "*.tiff", "*.png"}, {quoted form of "/Path/to/DestFolder/for/Pics/"}} --list of suffixes + DestPath
	set thePDFs to {{"*.pdf"}, {quoted form of "/Path/to/DestFolder/for/Pdfs/"}} --same as above
	set theZips to {{"*.zip"}, {quoted form of "/Path/to/DestFolder/for/Zips/"}} --...
	
	set theList to {thePics, thePDFs, theZips}
	
	repeat with i from 1 to count of theList
		set theSubList to item i of theList
		repeat with j from 1 to count item 1 of theSubList
			
			set theSuffix to item j of item 1 of theSubList
			tell application "Terminal"
				activate
				do script "cd " & SourceFolder in window 1
				do script "mv -f " & theSuffix & space & (item 2 of theSubList) in window 1
			end tell
		end repeat
	end repeat
	
end adding folder items to

… hope it’ll work :slight_smile:

It works great yes, thank you. Is there a way to do it without having to bring Terminal.app to the front, or even open Terminal at all?

Thanks in advance.

Hi,

this is a slightly different approach


on adding folder items to thisFolder after receiving these_items
	repeat with oneItem in these_items
		set Ex to name extension of (info for oneItem)
		set theDestination to missing value
		if Ex is in {"jpg", "tif", "psd", "tiff", "png"} then
			set theDestination to quoted form of "/Path/to/DestFolder/for/Pics/"
		else if Ex is "pdf" then
			set theDestination to quoted form of "/Path/to/DestFolder/for/Pdfs/"
		else if Ex is "zip" then
			set theDestination to quoted form of "/Path/to/DestFolder/for/Zips/"
		end if
		if theDestination is not missing value then
			do shell script "/bin/mv -f " & quoted form of POSIX path of oneItem & space & theDestination
		end if
	end repeat
end adding folder items to

Edit: added some code to avoid an error if theDestination is not defined

Wow, that’s much faster. Thank you very much. What I’m noticing now is a flicker every now and then, as if Finder is briefly switching to itself to check if anything has been added to the folder. What’s that about?

But, seriously, you’ve no idea how long I’ve searched for something that does this. You should put a “Choose source folder” and “Choose destination folder” interface on it and package it up as an application.

Thanks again.

Hi,

just did it that way to play around with the filtering command of "mv " … :wink:

@Stefan I couldn’t figure out how to bring it in a do shell script line …¿ any advice?