folder action script, very usefull

this script is a folder action script that will sort any items dropped on to it!!
more if statements can be added to adapt the script around your needs.

property dialog_timeout : 30
on adding folder items to this_folder after receiving added_items
set booger_items to every item of the added_items
repeat with x from 1 to the number of items in the added_items
copy item x of the added_items to padded_items
copy the (info for padded_items) to fileInfo
copy the name extension of the fileInfo to nameExtension
copy the file type of the fileInfo to fileType
tell application “Finder”
if the nameExtension is “mov” or the nameExtension is “asf” or the nameExtension is “avi” then
move padded_items to folder “Movies” of home
set finalFolder to “Movies”
set doner to true
end if
if the nameExtension is “jpg” or the nameExtension is “jpeg” or the nameExtension is “png” or ¬
the nameExtension is “gif” or the nameExtension is “bmp” or the nameExtension is “pdf” then
move padded_items to folder “Pictures” of home
set finalFolder to “Pictures”
set doner to true
end if
if the nameExtension is “mp3” or the nameExtension is “wav” then
move padded_items to folder “iTunes Library” of folder “Music” of home
set finalFolder to “iTunes Library”
set doner to true
end if
if the nameExtension is “torrent” then
tell application “BitTorrent”
activate
copy padded_items to bit_torrent
open bit_torrent

			end tell
		end if
	end tell
	if x is equal to the number of items in the added_items then
		-- find out how many new items have been placed in the folder
		set the item_count to the number of items in the added_items
		--create the alert string
		set alert_message to ("Folder Actions Alert:" & return & return) as Unicode text
		if the item_count is greater than 1 then
			set alert_message to alert_message & (the item_count as text) & " new items have "
		else
			set alert_message to alert_message & "One new item has "
		end if
		set alert_message to alert_message & "been placed in folder " & «data utxt201C» & the finalFolder & «data utxt201D» & "."
		set the alert_message to (the alert_message & return & return & "Would you like to view the added items?")
		
		display dialog the alert_message buttons {"Desktop", "Yes", "No"} default button 3 with icon 1 giving up after dialog_timeout
		set the user_choice to the button returned of the result
		
		if user_choice is "Yes" then
			tell application "Finder"
				--go to the desktop 
				activate
				--open the folder
				
				--select the items
				reveal the booger_items
			end tell
		else if the user_choice is "Desktop" then
			tell application "Finder"
				move booger_items to folder "Macintosh HD:Library:Desktop Pictures:"
			end tell
			display dialog "Reveal?" buttons {"Yes", "No"} default button 2 with icon 1 giving up after dialog_timeout
			set the user_choice2 to the button returned of the result
			if user_choice2 is "Yes" then
				tell application "Finder"
					activate
					reveal the booger_items
				end tell
			end if
			
		end if
	end if
end repeat

end adding folder items to

rember this is folder action script so it will have to be saved as a compiled script and attached to a folder, I have it attached to my downloads folder so when something is downloaded it is automatically sorted. if you use this please post and tell me of any improvements to be made.

First, this looks like something that should be posted to ScriptBuilders or Code Exchange. Second, there are auto-sorting scripts out there. Still, there seem to be lots of ways to streamline this. Here is a quick attempt (minus the bit about moving the files to the Desktop Pictures folder which doesn’t make a lot of sense since you obviously are testing for file types that aren’t always images). Also, this loops through all the files since the files may be of different file types and will be sorted appropriately (or not at all if they fail the extension test). Also note, this wasn’t tested and there should probably be some try blocks in here in case of duplicate file names in the destination folders or if the destination folders don’t even exist.

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]