Currently I have 3 scripts that I would like to combine into 1. A little backstory: Everyday I receive many files formatted like this: #1photo.jpg, #2photo.jpg, etc. I need them to sort into folders that have been previously created based on their numbers, i.e: #1photo.jpg would need to be moved into a folder titled 1_Sometext, #2photo.jpg would need to be moved to folder titled 2_Sometext, and so on. Right now I have 3 separate codes that seem to accomplish what I need but I was hoping to either combine them or streamline the process/code somehow. The first code removes the hashtag from the filename. The second code removes all the letters from the filename. The third code moves the files into the numbered folders.
First Code: Removing Hashtags
set whichFile to choose file with multiple selections allowed
repeat with aFile in whichFile
tell application "Finder"
set filename to name of aFile
set name of aFile to ((characters 2 thru -1 of filename) as string)
end tell
end repeat
Second Code: Removing Letters from Filename
set theF to choose file
tell application "System Events"
tell item (theF as text)
set {name:fileName, name extension:fileExtension} to it
set parentPath to path of its container as text
end tell
end tell
try
set trimmedFileName to (do shell script "tr -d '[:alpha:]' <<<" & quoted form of fileName & " | grep -E [[:digit:]] ")
on error
-- no digits in the resulting filename!
set trimmedFileName to ""
end try
if trimmedFileName is not "" then
tell application "System Events"
if not (exists item (parentPath & trimmedFileName & fileExtension)) then
tell item (theF as text)
set name of it to (trimmedFileName & fileExtension)
end tell
end if
end tell
end if
Third Code: Moving Files into Folders Based on Matching Numbers
set baseFolder to choose folder with prompt "Choose Folder with Photos"
set printFolder to choose folder with prompt "Choose Today's Versioning Folder"
-- or hard-coded
-- set printFolder to alias "MacHD:Users:myuser:path:to:Print:"
tell application "Finder" to set fileList to files of baseFolder
repeat with aFile in fileList
set prefix to getPrefix(name of aFile, name extension of aFile)
tell application "Finder"
try
set destinationFolder to (1st folder of printFolder whose name begins with (prefix & "_"))
move aFile to destinationFolder
end try
end tell
end repeat
on getPrefix(aName, anExtension)
set {TID, text item delimiters} to {text item delimiters, "_"}
if (count text items of aName) > 1 then
set prefix to text item 1 of aName
else
set prefix to text 1 thru ((get offset of "." & anExtension in aName) - 1) of aName
end if
set text item delimiters to TID
return prefix
end getPrefix
I am very new to all this but trying to pick it up as I go along. Any and all help is greatly appreciated. Thanks!!
PS - I am using OS X 10.9.5 and AppleScript Editor 2.3.2