I need a little help sorting files to folders based on extension...

Hi there, totally new to the forums, and totally stoked on AppleScripts (picked up a MacBook in September, just getting around to the fun stuff)!
I read some guides, and a handful of tutorials, and from what I’ve gathered that a script I’ve been writing SHOULD be working, but this is of course only MY opinion (which is wrong, because its not working).
Here is what I was shooting for with this script, before I post the snippet:

  1. Watch my “Downloads” folder, which is located in /Users/Me
  2. Sort files as they arrive in my “Downloads” folder according to their extensions to other folders
    • Some of these folders are inside of my “Downloads” folder, IE “Compressed” (.dmg, .zip, .rar, and .tar) and “Torrents” (.torrent)
    • Others are located in my sidebar, IE “Movies” (.mov, .mpg, etc.), “Music” (.acc, .mp3, etc.), “Pictures” (.jpg, .pct, etc.), and “eLiterature” (.pdf, .cbr, etc.)
      And now, the code in its latest mutated form!
on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		if name of added_file contains ".torrent" then
			move added_file to folder "Macintosh HD:Users:Me:Downloads:Torrents:"
		else if name of added_file contains {".smi", ".zip", ".dmg", ".img", ¬
			".pkg", ".rar", ".tar", ".sit", ".sitx"} then
			move added_file to folder "Macintosh HD:Users:Me:Downloads:Compressed:"
		else if name of added_file contains {".cbr", ".chm", ".pdf"} then
			move added_file to folder "Macintosh HD:Users:Me:eLiterature:"
		else if name of added_file contains {".jpg", ".jpeg", ".gif", ".tiff", ¬
			".psd", ".tif", ".pct", ".png"} then
			move added_file to folder "Macintosh HD:Users:Me:Pictures:"
		else if name of added_file contains {".mp3", ".mp4", ".ogg", ".acc", ¬
			".wma"} then
			move added_file to folder "Macintosh HD:Users:Me:Music:"
		else if name of added_file contains {".mpg", ".wmv", ".rm", ".rmvb.", ¬
			".avi", ".mov"} then
			move added_file to folder "Macintosh HD:Users:Me:Movies:"
		end if
	end tell
end adding folder items to

Thats that!
Feel free to tear me apart, or offer help in shortening, optimizing, etc. my code, as I’m a total beginner.
Thanks in advance!
Edit: This is not the only forum I’m on, so I’d like to add that I DID use the search function to check to see if I could find a solution before posting, but nothing seemed to work for me

I don’t use folder actions much and I can’t test this at the moment, but try this…

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		repeat with added_file in these_items
			if name of added_file contains ".torrent" then
				move added_file to folder "Macintosh HD:Users:Me:Downloads:Torrents:"
			else if name of added_file contains {".smi", ".zip", ".dmg", ".img", ".pkg", ".rar", ".tar", ".sit", ".sitx"} then
				move added_file to folder "Macintosh HD:Users:Me:Downloads:Compressed:"
			else if name of added_file contains {".cbr", ".chm", ".pdf"} then
				move added_file to folder "Macintosh HD:Users:Me:eLiterature:"
			else if name of added_file contains {".jpg", ".jpeg", ".gif", ".tiff", ".psd", ".tif", ".pct", ".png"} then
				move added_file to folder "Macintosh HD:Users:Me:Pictures:"
			else if name of added_file contains {".mp3", ".mp4", ".ogg", ".acc", ".wma"} then
				move added_file to folder "Macintosh HD:Users:Me:Music:"
			else if name of added_file contains {".mpg", ".wmv", ".rm", ".rmvb.", ".avi", ".mov"} then
				move added_file to folder "Macintosh HD:Users:Me:Movies:"
			end if
		end repeat
	end tell
end adding folder items to

James,
No dice, and I can’t figure out why.
That looks like it should be right, and feel like that was my code when I first gave this a try.
I’m totally lost. :confused:
But thanks for the effort! :smiley:

I have never used folder actions myself, but I believe that your main issue involves your logical operators. When you use contains in your original script, you are asking Finder if the name of your file contains a list. That will never evaluate to true. Your first line:

f name of added_file contains ".torrent" then

will evaluate to true when the added file is a .torrent file, but only because are asking Finder if the name contains a particular string value. All of the others will always evaluate to false, because no filename can contain a list.

Instead, you need to know if your filename has an extension that is in your list of possibilities. So, instead of using name, how about using name extension:

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		repeat with added_file in these_items
			if name extension of added_file is "torrent" then
				move added_file to folder "Macintosh HD:Users:Me:Downloads:Torrents:"
			else if name extension of added_file is in {"smi", "zip", "dmg", "img", "pkg", "rar", "tar", "sit", "sitx"} then
				move added_file to folder "Macintosh HD:Users:Me:Downloads:Compressed:"
			else if name extension of added_file is in {"cbr", "chm", "pdf"} then
				move added_file to folder "Macintosh HD:Users:Me:eLiterature:"
			else if name extension of added_file is in {"jpg", "jpeg", "gif", "tiff", "psd", "tif", "pct", "png"} then
				move added_file to folder "Macintosh HD:Users:Me:Pictures:"
			else if name extension of added_file is in {"mp3", "mp4", "ogg", "acc", "wma"} then
				move added_file to folder "Macintosh HD:Users:Me:Music:"
			else if name extension of added_file is in {"mpg", "wmv", "rm", "rmvb.", "avi", "mov"} then
				move added_file to folder "Macintosh HD:Users:Me:Movies:"
			end if
		end repeat
	end tell
end adding folder items to

The Finder does not use the dots when it examines a name extension, so I removed all of those (man, I love Find & Replace!!) and changed your contains to is in.

I am too lazy to enable folder actions to test this, so have at it, and report back soon.

Hi myacademy

This works for me as a folder action maybe you can add to it to get what you want:

on adding folder items to this_folder after receiving these_items
	set movieExts to {".mpg", ".wmv", ".rm", ".rmvb.", ".avi", ".mov"}
	repeat with x in movieExts
		tell application "Finder"
			repeat with added_file in these_items
				if name extension of added_file is in x then
					move added_file to folder "Donatello:Users:steve:Desktop:movie:"
				end if
			end repeat
		end tell
	end repeat
end adding folder items to

edit: oops craig beat me to it!!

Craig & Pidge,
You folks rule!
I thought that the list items would be evaluated individually.
I suppose I should’ve tried to run a loop to test them extension by extension rather than as extensions in a list.
Needless to say, your solutions (both) worked beautifully.
In the future I’ll make sure to check the API on things like that. :wink:
Thanks a lot for the help!