Skip to content

Instantly share code, notes, and snippets.

@tanob
Created November 12, 2009 01:45
Show Gist options
  • Save tanob/232515 to your computer and use it in GitHub Desktop.
Save tanob/232515 to your computer and use it in GitHub Desktop.
(*This folder action script is designed to keep your downloads folder organised *)
property image_extension_list : {"tif", "tiff", "gif", "png", "pict", "pct", "jpg"}
property media_extension_list : {"mp3", "avi", "mov", "mpg"}
property document_extension_list : {"doc", "pdf", "ps", "mpg"}
property archive_extension_list : {"zip", "sit", "dmg", "tar", "hqx", "toast", "rar"}
property torrent_extension_list : {"torrent"}
property image_foldername : "images"
property media_foldername : "media"
property document_foldername : "docs"
property archive_foldername : "archives"
property torrent_foldername : "torrents"
on adding folder items to this_folder after receiving added_items
repeat with this_item in added_items
tell application "Finder"
if (the name extension of the this_item is in the image_extension_list) then
my makeamove(this_item, this_folder, image_foldername)
end if
if (the name extension of the this_item is in the media_extension_list) then
my makeamove(this_item, this_folder, media_foldername)
end if
if (the name extension of the this_item is in the archive_extension_list) then
my makeamove(this_item, this_folder, archive_foldername)
end if
if (the name extension of the this_item is in the torrent_extension_list) then
my makeamove(this_item, this_folder, torrent_foldername)
end if
if (the name extension of the this_item is in the document_extension_list) then
my makeamove(this_item, this_folder, document_foldername)
end if
end tell
end repeat
end adding folder items to
on makeamove(this_item, root_folder, target_foldername)
tell application "Finder"
if not (exists folder target_foldername of root_folder) then
make new folder at root_folder with properties {name:target_foldername}
end if
set the target_folder to folder target_foldername of root_folder
my resolve_conflicts(this_item, root_folder, target_folder)
move file this_item to the target_folder
end tell
end makeamove
on resolve_conflicts(this_item, root_folder, target_folder) --renames the item if dublicate exists in target folder
tell application "Finder"
set file_extension to the name extension of this_item
set the file_name to the name of this_item
if the file_extension is "" then
set the trimmed_name to the file_name
else
set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
end if
if (exists document file file_name of target_folder) then
set the name_increment to 1
repeat
set the new_name to (the trimmed_name & "_" & (name_increment as string) & "." & file_extension) as string
if not (exists document file new_name of the target_folder) then
set the name of document file file_name of folder root_folder to the new_name
exit repeat
else
set the name_increment to the name_increment + 1
end if
end repeat
end if
end tell
return the file_name
end resolve_conflicts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment