I have been looking at creating a droplet to sort each of my project folders into subfolders with the following titles:
Images
Freehand files
PDFs
Sounds
Movies
Text files
etc, etc…
I would have thought this would be easy to script in Applescript, but as a newbie I’m having a few problems when it comes to actually shifting the files once they have been identified. The following script is based on an example I found at apple-club.hk (i think, sorry if it’s the incorrect address).
-- I intend to create a filetype list here
property type_list : {}
property extension_list : {"jpg", "pic", "jpeg", "tif", "tiff", "pict", "psd"}
-- This droplet processes both files or folders of files dropped onto the applet
on open these_items
repeat with i from 1 to the count of these_items
set this_item to (item i of these_items)
set the item_info to info for this_item
if folder of the item_info is true then
process_folder(this_item)
else if (alias of the item_info is false) and ¬
((the file type of the item_info is in the type_list) or ¬
the name extension of the item_info is in the extension_list) then
process_item(this_item)
end if
end repeat
end open
-- this sub-routine processes folders
on process_folder(this_folder)
set these_items to list folder this_folder without invisibles
repeat with i from 1 to the count of these_items
set this_item to alias ((this_folder as text) & (item i of these_items))
set the item_info to info for this_item
if folder of the item_info is false then
if (alias of the item_info is false) and ¬
((the file type of the item_info is in the type_list) or ¬
the name extension of the item_info is in the extension_list) then
process_item(this_item, this_folder)
end if
end if
end repeat
end process_folder
-- this sub-routine processes files
on process_item(this_item, this_folder)
try
move item this_item to (this_folder & "Images")
on error
make new folder at this_folder with properties {name:"Images"}
move this_item to (this_folder & "Images:")
end try
end process_item
What the script should be doing is searching the dropped folder contents (not recursively) and checking each file for matches in the property and type lists (at the moment, I’m only checking for image files, but later on I want to check for all types and sort accordingly). Once the script makes a match, it launches the process_item subroutine and attempts to move the file to the “Images” folder. If it cannot find the Images folder, it creates one, then moves the file.
The script seems to be able to identify each file, but refuses to move any file and brings up an error message with a “Can’t make a {class adsr}” error. I know it’s something REALLY basic, so could anyone help me out?
Thanks in advance.
I miss the tell “Finder” statement:
tell "Finder" to move this_item to (this_folder & "Images:")
Thanks Dutch… the tell “Finder” command got rid of the class error. It still isn’t working though.
For some reason, when I drag a folder that already contains an “Images” folder onto the script, it isn’t noticing the Images folder at all, and runs the “on error” part of the process_item subroutine. This then displays an error dialog stating “Finder doesn’t understand the “make” message.”
I’ve tried it with a folder that contains images, but no Images folder, and I get the same message - it’s running the on error part due to the lack of “Images” folder, but why is it refusing to create a new folder?
I’ve also run the script on a folder that contains no images at all, and the scripts runs perfectly. Obviously the problem is in process_item.
I just thought I’d post the results of my hard work into the forum to see if anyone could offer a more streamlined style of coding. I’m used to coding in PHP, lingo and actionscript, and to be honest, Applescript really has me stumped.
See what you think of this - it runs perfectly (if a little slow), and seperates all files on the root level of the dropped folder into categorised folders (according to filetype and extension):
property imageList : {"gif", "jpg", "psd", "tif", "pic", "pict", "jpeg"}
property textList : {"txt", "doc", "rtf", "clpt", "tab", "sql"}
property pdfList : {"pdf"}
property soundsList : {"wav", "mp3", "m4a", "aif", "aiff"}
property flashList : {"fla", "swf"}
property directorList : {"dir", "dcr"}
property macList : {"sit", "dmg"}
property pcList : {"exe", "zip"}
property freehandList : {"AGD6"}
property webList : {"htm", "php"}
property finalList : {"FCPF"}
property filemakerList : {"fp5"}
property afterList : {"aep"}
property movieList : {"mov", "avi", "mpg", "MooV", "MPEG", "3gp"}
property appList : {"appl"}
property typeList : {}
on open TheFolder
tell application "Finder"
(* set the TheFolder to the dropped folder name *)
set TheFolder to TheFolder as text
(* set FolderList to the dropped folder name - in case of multiple folders, list as list *)
set FolderList to {TheFolder}
(* start with first item on list of dropped items *)
(* and set TheFolder to the first dropped item *)
set TheFolder to item 1 of FolderList
set TheFolder to TheFolder as text
repeat
try
set TheType to {name, file type, creator type} of first file of folder TheFolder
set TheName to item 1 of TheType
set TheFileType to item 2 of TheType
set N to count of characters of TheName
if (N > 3) and character (N - 3) of TheName is "." then
set TheName to characters (N - 2) thru N of TheName
set TheName to TheName as text
end if
if ((imageList contains TheName) or (imageList contains TheFileType)) then
set TheFolderName to "Images"
else if ((textList contains TheName) or (textList contains TheFileType)) then
set TheFolderName to "Text files"
else if ((pdfList contains TheName) or (pdfList contains TheFileType)) then
set TheFolderName to "PDF files"
else if ((soundsList contains TheName) or (soundsList contains TheFileType)) then
set TheFolderName to "Sounds"
else if ((flashList contains TheName) or (flashList contains TheFileType)) then
set TheFolderName to "Flash files"
else if ((directorList contains TheName) or (directorList contains TheFileType)) then
set TheFolderName to "Director files"
else if ((macList contains TheName) or (macList contains TheFileType)) then
set TheFolderName to "Mac files"
else if ((pcList contains TheName) or (pcList contains TheFileType)) then
set TheFolderName to "PC files"
else if ((freehandList contains TheName) or (freehandList contains TheFileType)) then
set TheFolderName to "Freehand files"
else if ((webList contains TheName) or (webList contains TheFileType)) then
set TheFolderName to "Site files"
else if ((finalList contains TheName) or (finalList contains TheFileType)) then
set TheFolderName to "Final Cut files"
else if ((filemakerList contains TheName) or (filemakerList contains TheFileType)) then
set TheFolderName to "Filemaker files"
else if ((afterList contains TheName) or (afterList contains TheFileType)) then
set TheFolderName to "After Effect files"
else if ((movieList contains TheName) or (movieList contains TheFileType)) then
set TheFolderName to "Movies"
else if ((appList contains TheName) or (appList contains TheFileType)) then
set TheFolderName to "Application files"
else
set TheFolderName to "Unknown"
end if
try
move file 1 of folder TheFolder to folder TheFolderName of folder TheFolder
on error
make new folder at folder TheFolder with properties {name:TheFolderName}
end try
on error
exit repeat
end try
end repeat
end tell
end open
The script is based on the File Type Sorter script from Scriptbuilders.