Hi All,
I have a script that I use as a folder action for moving files into folders based on file name. Basically when I download a TV show my folder action passes that filename and then in my script I have it use all of the text before the season (usually entered as S1, S2 etc…) to create a folder based on the show name. If the folder already exists then it just moves the file into that folder.
Right now I am hard coding my delimiters for where the folder name cutoff is. So I have it look for S0, S1, S2 etc… This works well but is very limited. what I would like is something like S##E## so there isn’t the limitation that I currently have. I would also like for it to look at date formats too, for example ####.##.## or ##.##.####
I believe this type of thing is possible in the shell using grep or using regular expressions I just can’t figure out how to implement this stuff into applescript. If someone wanted to do me a huge favor and look at my script and tell me what I need to do, or even make the necessary changes I would be forever gratefull.
Thanks,
Tanner
--Script Setup Variables--
set sendGrowl to "Yes" -- if Yes then growl notifications will be sent, if set to No they won't
--End Script Setup Variables--
--Functions--
--Function to replace text
on str_replace(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set returnList to true
-- This wouldn't make sense (you could have it raise an error instead)
if class of find is not list and class of replace is list then return subject
if class of find is not list then set find to {find}
if class of subject is not list then ¬
set {subject, returnList} to {{subject}, false}
set findCount to count find
set usingReplaceList to class of replace is list
try
repeat with i from 1 to (count subject)
set thisSubject to item i of subject
repeat with n from 1 to findCount
set text item delimiters of AppleScript to item n of find
set thisSubject to text items of thisSubject
if usingReplaceList then
try
item n of replace
on error
"" -- `replace` ran out of items
end try
else
replace
end if
set text item delimiters of AppleScript to result
set thisSubject to "" & thisSubject
end repeat
set item i of subject to thisSubject
end repeat
end try
set text item delimiters of AppleScript to prevTIDs
if not returnList then return beginning of subject
return subject
end str_replace
--end replace text function
--Function to trim leading and trailing spaces
on trim(someText)
repeat until someText does not start with " "
set someText to text 2 thru -1 of someText
end repeat
repeat until someText does not end with " "
set someText to text 1 thru -2 of someText
end repeat
return someText
end trim
--end trim function
--Function to change case of text. Used to create title case folders
property lower_alphabet : "abcdefghijklmnopqrstuvwxyz"
property upper_alphabet : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
property white_space : {space, tab, return, ASCII character 10, ASCII character 13}
on change_case(this_text, this_case)
set new_text to ""
if this_case is not in {"UPPER", "lower", "Title", "Sentence"} then
return "Error: Case must be UPPER, lower, Title or Sentence"
end if
if this_case is "lower" then
set use_capital to false
else
set use_capital to true
end if
repeat with this_char in this_text
set x to offset of this_char in lower_alphabet
if x is not 0 then
if use_capital then
set new_text to new_text & character x of upper_alphabet as string
if this_case is not "UPPER" then
set use_capital to false
end if
else
set new_text to new_text & character x of lower_alphabet as string
end if
else
if this_case is "Title" and this_char is in white_space then
set use_capital to true
end if
set new_text to new_text & this_char as string
end if
end repeat
return new_text
end change_case
--End title case function
--End Functions--
--Watch for items added to the folder specified by the folder action--
on adding folder items to this_folder
--
--Main Script--
--Set delimiter for where to end the folder name, in this case we want everything before the season
tell application "Finder"
try
--Set chosen_folder variable to the folder passed via the folder action
set chosen_folder to this_folder as alias
set file_list to files of chosen_folder
set file_names to name of files of chosen_folder as string
on error
set file_list to {}
end try
repeat with this_file in file_list
set folder_name to name of this_file
if (folder_name contains "S0") then
tell (a reference to my text item delimiters) to set {old_delim, contents} to {contents, "S0"}
else if (folder_name contains "S1") then
tell (a reference to my text item delimiters) to set {old_delim, contents} to {contents, "S1"}
else if (folder_name contains "S2") then
tell (a reference to my text item delimiters) to set {old_delim, contents} to {contents, "S2"}
else if (folder_name contains "S3") then
tell (a reference to my text item delimiters) to set {old_delim, contents} to {contents, "S3"}
else if (folder_name contains "1x") then
tell (a reference to my text item delimiters) to set {old_delim, contents} to {contents, "1x"}
else if (folder_name contains "2x") then
tell (a reference to my text item delimiters) to set {old_delim, contents} to {contents, "2x"}
else if (folder_name contains "3x") then
tell (a reference to my text item delimiters) to set {old_delim, contents} to {contents, "3x"}
else if (folder_name contains "4x") then
tell (a reference to my text item delimiters) to set {old_delim, contents} to {contents, "4x"}
else if (folder_name contains "5x") then
tell (a reference to my text item delimiters) to set {old_delim, contents} to {contents, "5x"}
else if (folder_name contains "6x") then
tell (a reference to my text item delimiters) to set {old_delim, contents} to {contents, "6x"}
else if (folder_name contains "7x") then
tell (a reference to my text item delimiters) to set {old_delim, contents} to {contents, "7x"}
else if (folder_name contains "8x") then
tell (a reference to my text item delimiters) to set {old_delim, contents} to {contents, "8x"}
else if (folder_name contains "9x") then
tell (a reference to my text item delimiters) to set {old_delim, contents} to {contents, "9x"}
else if (folder_name contains "0x") then
tell (a reference to my text item delimiters) to set {old_delim, contents} to {contents, "0x"}
end if
set folder_name to ((text items 1 thru -2 of folder_name) as string)
--Run the string replace command on the folder name variable
set folder_name to str_replace(".", " ", folder_name as Unicode text) of me
set folder_name to str_replace("-", " ", folder_name as Unicode text) of me
set folder_name to str_replace("_", " ", folder_name as Unicode text) of me
set folder_name to str_replace(" ", " ", folder_name as Unicode text) of me
--Run the trim command on the folder variable
set folder_name to trim(folder_name) of me
--Run title case command
set folder_name to change_case(folder_name, "Title") of me
--
set new_folder to ((chosen_folder as string) & folder_name & ":")
try
get new_folder as alias
on error
make new folder at chosen_folder with properties {name:folder_name}
end try
move this_file to folder new_folder
set my text item delimiters to old_delim
end repeat
end tell
--End Main Script--
--Send Growl Notification--
--Check if growl is running--
tell application "System Events"
set isRunning to ¬
(count of (every process whose name is "GrowlHelperApp")) > 0
end tell
--If growl is running then proceed--
if isRunning is true then
tell application "GrowlHelperApp"
set fileName to file_names
if fileName is not "" then
set the allNotificationsList to {"Welcome", "Transfer Complete", "Error"}
set the enabledNotificationsList to {"Welcome", "Transfer Complete", "Error"}
register as application "TV Show Mover" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Plex"
notify with name "Transfer Complete" title "Transfer Successfull" description "File " & fileName application name "TV Show Mover" sticky "no"
end if
end tell
end if
--End Send Growl Notification--
--
end adding folder items to