OK "bonis", nu TROR jag att jag har en variant med bättre tillförlitlighet än tidigare variant. Prova den gärna!
property image_extension_list : {"tif", "tiff", "gif", "png", "pict", "pct", "jpg"}
property media_extension_list : {"mp3", "avi", "mov", "mpg"}
property archive_extension_list : {"zip", "sit", "dmg", "tar", "hqx", "toast", "rar"}
property torrent_extension_list : {"torrent"}
property pdf_extension_list : {"pdf"}
property image_foldername : "images"
property media_foldername : "media"
property archive_foldername : "archives"
property torrent_foldername : "torrents"
property pdf_foldername : "pdf"
on adding folder items to this_folder after receiving added_items
tell application "Finder"
repeat with this_item in added_items
if (the name extension of this_item is in image_extension_list) then
my makeamove(this_item, this_folder, image_foldername)
else if (name extension of this_item is in media_extension_list) then
my makeamove(this_item, this_folder, media_foldername)
else if (name extension of this_item is in archive_extension_list) then
my makeamove(this_item, this_folder, archive_foldername)
else if (name extension of this_item is in torrent_extension_list) then
my makeamove(this_item, this_folder, torrent_foldername)
else if (name extension of this_item is in pdf_extension_list) then
my makeamove(this_item, this_folder, pdf_foldername)
end if
end repeat
end tell
-- Try this to reset folder action in case file was not in the above set
tell application "System Events" to set folder actions enabled to false
delay 1
tell application "System Events" to set folder actions enabled to true
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
tell application "System Events" to set folder actions enabled to false -- we don't want to trigger the script
make new folder at root_folder with properties {name:target_foldername}
--delay 1
tell application "System Events" to set folder actions enabled to true -- turn on the folder script again
end if
set target_folder to folder target_foldername of root_folder
my resolve_conflicts(this_item, root_folder, target_folder)
tell application "System Events" to set folder actions enabled to false -- we don't want to trigger the script
move item this_item to target_folder
tell application "System Events" to set folder actions enabled to true -- turn on the folder script again
end tell
end makeamove
on resolve_conflicts(this_item, root_folder, target_folder) --renames the item if duplicate exists in target folder
tell application "Finder"
set file_extension to name extension of this_item
set file_name to name of this_item
set trimmed_name to text 1 thru -((length of file_extension) + 2) of file_name
if (exists item file_name of target_folder) then
set name_increment to 1
repeat
set new_name to (trimmed_name & "_" & (name_increment as string) & "." & file_extension) as string
if not (exists item new_name of target_folder) then
set name of item file_name of folder root_folder to new_name
exit repeat
else
set name_increment to name_increment + 1
end if
end repeat
end if
end tell
return file_name
end resolve_conflicts