If you don’t mind, I have a related script that I’d like to share. It’s a bit messy, but it might help someone.
The script will take a folder full of files and sort them into sub-folders based on their names. It actually strips off the file extension and any numberical part and compares the names, so it might not be applicable to the current question, but it could be modified to compare the first characters/numbers of the name.
I’ve commented it throughout so you can see the logic. It could also probably be simplified a little, but it currently works for my prorposes.
(*
This is an AppleScript which will organize a folder-full of files into sub-folders based on the file names. It will remove any numbers, dashes, spaces and file extensions and compare what remains.
If there is more that one file in the series, a new folder will be created and all similar files will be placed into the folder.
If there is only one file (of if the file name is just numbers) then the file will be put into the ‘other’ folder.
For example:
George_Washington_01.jpg
George_Washington_02.jpg
George_Washington_03.jpg
George_Washington_22.jpg
Martin Van Buren 22.tif
Martin Van Buren 24.tif
Martin Van Buren 35.tif
Andrew Jackson 01.jpg
01-01234-03.tif
The George_Washington files will be put into a “George Washington” folder; Martin Van Buren into “Martin Van Buren” folder; but Andrew Jackson and 01-01234-03 will be put into the “Other” folder.
*)
------------ Initialize variables
set previous_item to “”
set this_item to “”
set short_this_item to “”
------------ Choose Source Folder & get list of visible files
set the source_folder to choose folder with prompt “Folder containing items to organize:”
set the the_list to list folder source_folder without invisibles
set source_folder to source_folder as string
------------ Set the variables for first item (since there is no previous item)
set next_item to (extract_text(item (1) of the_list)) ----- simplify the file name for comparison
------------ Loop through the list
set total_items to number of items in the_list
tell application “Finder”
activate
–open folder source_folder
–set current view of window 1 to list view
end tell
repeat with i from 1 to total_items
------------ Set current item
copy item i of the_list to this_item ----- get the current file name
set this_item_file to (source_folder & this_item) as alias ---- get the file path
set this_info to info for this_item_file --- get info (to check that it's not a folder)
if the folder of this_info is false then ------------ Make sure it's not a folder
set previous_item to short_this_item ----- move the current file name to the previous name spot
set short_this_item to next_item ----- make the next file name the current file name
if i != total_items then --- make sure it's not the last item
set next_item to (extract_text(item (i + 1) of the_list)) --- get the next item's short (extracted) name
else
set next_item to ""
end if
tell application "Finder"
--Uncomment the next two lines if you want the source folder to open in the Finder
--open folder source_folder
--set current view of window 1 to list view
if short_this_item = previous_item then ----- is the short name the same as the previous short name?
try
move file this_item_file to folder (source_folder & short_this_item as string) --- if it's the same as previous, move the file to the exisiting folder
end try
else if short_this_item = next_item then ------ is the short name the same as the next file's short name? Then try to make a folder for them
if not (exists folder (source_folder & short_this_item as string)) then --- does a folder already exist for it?
if not (exists file (source_folder & short_this_item as string)) then ---- does the folder name confict with a file name?
set new_folder to make new folder at alias source_folder with properties {name:short_this_item} --- make new folder
else
set new_folder to make new folder at alias source_folder with properties {name:short_this_item & " f"} --- make new folder (& 'f")
end if
try
move file this_item_file to new_folder ---- move the file to the new folder
end try
else
try
move file this_item_file to folder (source_folder & short_this_item as string) ---- move this file to the exisiting folder
end try
end if
else --- if the short name doesn't match the previous or next file, move it to the 'other' folder
if not (exists folder (source_folder & "Other" as string)) then --- does the 'other' folder exist?
set new_folder to make new folder at alias source_folder with properties {name:"Other"} --- make an 'other' folder
try
move file this_item_file to new_folder ---- move the file
end try
else
try
move file this_item_file to folder (source_folder & "Other" as string) --- move the file
end try
end if
end if
end tell
end if
end repeat
beep 2
on extract_text(current_name) ------ calculate the short name
copy “” to new_name
copy current_name to new_name
repeat with search_string in (every item of "0123456789!#$'+%()") ---- remove numers and some other characters
if new_name = " " or new_name = "" then return current_name
if new_name contains search_string then
set AppleScript's text item delimiters to the search_string
set the text_item_list to every text item of the new_name
set AppleScript's text item delimiters to the ""
set the new_name to the text_item_list as string
end if
end repeat
--display dialog new_name --------------------**************
repeat with search_string in (every item of "_-") ----- replace these characters with spaces
if new_name = " " or new_name = "" then return current_name
if new_name contains search_string then
set AppleScript's text item delimiters to the search_string
set the text_item_list to every text item of the new_name
set AppleScript's text item delimiters to the " "
set the new_name to the text_item_list as string
set AppleScript's text item delimiters to the ""
end if
end repeat
--display dialog new_name --------------------for testing purposes
--- Make sure the short name is not just the file extension (may occur if file name is only numbers
repeat with search_string in {every item of "", " ", ".jpg", ".gif", ".tif", ".jpeg", ".uu", ".mim", ".mime", ".txt"}
if new_name = " " or new_name = "" then return current_name
if new_name = search_string then
return current_name
end if
end repeat
--display dialog new_name --------------------for testing purposes
if new_name = ".jpg" then ------ is the name just a number and extension?
return current_name
else
repeat with search_string in {every item of ".jpg", ".gif", ".tif", ".jpeg", ".uu", ".mim", ".mime", ".txt"} ------ remove these file extentions
if new_name = " " or new_name = "" then return current_name
if new_name contains search_string then
set AppleScript's text item delimiters to the search_string
set the text_item_list to every text item of the new_name
set AppleScript's text item delimiters to the ""
set the new_name to the text_item_list as string
set AppleScript's text item delimiters to the ""
end if
end repeat
set AppleScript's text item delimiters to the " " -------- remove double spaces
set the text_item_list to every text item of the new_name
set AppleScript's text item delimiters to the " "
set the new_name to the text_item_list as string
set AppleScript's text item delimiters to the ""
--display dialog new_name --------------------for testing purposes
repeat while last character of new_name = " " ----- remove spaces at the end
if new_name = " " or new_name = "" or new_name = "." then return current_name
set new_name to characters 1 thru -2 of new_name as string
end repeat
repeat while last character of new_name = "." ----- remove periods at the end
if new_name = "." or new_name = "" or new_name = "." then return current_name
set new_name to characters 1 thru -2 of new_name as string
end repeat
--display dialog new_name --------------------for testing purposes
return new_name
end if
end extract_text