renaming finder items IV

when we first started looking at batch renaming files, we saw how you could use automator to replace part of a filename with new text (see renaming finder items). here’s an applescript version of that functionality which makes batch renaming files in the finder even easier than automator.

this script works on batches of filenames that have something in common (eg. ‘12345 ThisFile_01’, ‘12345 ThisFile_02’, etc.) allowing the user to replace or add to that common element (eg. ‘ABCD_ThisFile_01’, ‘ABCD_ThisFile_02’, etc. … or … ‘12345 ThisFinishedFile_01’, ‘12345 ThisFinishedFile_02’, etc.)

the first part of the script references the selected finder items (the files to be changed) and the name of the first of these files. we use text item delimiters to strip the extension from that name :

tell application "Finder"
  set mgSelection to the selection
  set mgFirstName to name of item 1 of mgSelection
  set text item delimiters of AppleScript to "."
  set mgFirstName to text item 1 of mgFirstName
  set text item delimiters of AppleScript to ""
end tell

the next bit presents the user with two dialog windows. the first captures the change from text — with the filename from above as the initial default answer. the second captures the change to text — with the change from text as the initial default answer. including default answers in the dialog windows is a handy prompt for the user :

set mgChangeFromDialog to display dialog "What part of the filename do you want changed?" default answer mgFirstName with title "THIS SCRIPT CHANGES FILENAMES"
set mgChangeFrom to text returned of mgChangeFromDialog
delay 1
set mgChangeToDialog to display dialog "What is your new replacement text?" default answer mgChangeFrom with title "THIS SCRIPT CHANGES FILENAMES"
set mgChangeTO to text returned of mgChangeToDialog

here’s an example to make things clearer (hopefully). the first dialog is the change from window showing the first filename as it is presented to the user (left). on the right is that same window after the user specifies the part of the filename to change, before hitting ok :
screen grab of the change from dialog window

then the next dialog window — change to — pops up with that same user-defined text as the prompt (left). and the user specifies what that text should be changed to before hitting ok again (right) :
screen grab of the change to dialog window

you’ll notice in that script snippet above a one second delay between the presentation of the windows. this is purely a psychological gap to help the user realise that a new window has been presented. try it without the delay if you prefer.

next, just for shits and giggles, here’s a surly little message just in case the change from text is the same as the change to text. of course, this is entirely unnecessary — but good for a laugh :

considering case
  if mgChangeFrom = mgChangeTO then
    display dialog "OK — so you don't want any change at all." & return & return & "Thanks for wasting my time tosser." buttons {"get stuffed"} default button 1 giving up after 2
    error number -128
  end if
end considering

screen grab of surly message

that snippet demonstrates the handy ‘considering case’ control statement. this means the message would only get triggered if you tried to change “ThisFile” to “ThisFile” but not for “THISFILE” or “thisFILE” or “Thisfile” etc.

the rest of the script looks like this :

tell application "Finder"
  repeat with mgitem in mgSelection
    set mgOriginalName to name of mgitem
    set text item delimiters of AppleScript to mgChangeFrom
    set mgNameItems to text items of mgOriginalName
    set text item delimiters of AppleScript to ""
    set mgItemCount to count mgNameItems
    if mgItemCount is not 1 then
      set text item delimiters of AppleScript to mgChangeTO
      set mgNewName to mgNameItems as string
      set text item delimiters of AppleScript to ""
      set name of mgitem to mgNewName
    end if
  end repeat
end tell
set mgMessage to "All Done."
display dialog mgMessage

let’s use the examples from the dialog windows above to explain this part of the script. the files we are changing are pdfs. mgChangeFrom is ‘ThisFile’ and mgChangeTo is ‘ThisFinishedFile’. On the first pass through the loop, mgOriginalName will be the name of the first item in the Finder selection — ‘12345 ThisFile_01.pdf’. We set applescript’s text item delimiters (the characters which separate sections of text) to mgChangeFrom (‘ThisFile’) and then get the text items of mgOriginalName. this will give us the variable mgNameItems as a list — {“12345 “, “_01.pdf”}.

next we do a count of mgNameItems. if the list has only one item, it means mgOriginalName DOES NOT contain mgChangeFrom (eg. the filename ‘54321 ThatFile_31.pdf’ would return the list {“54321 ThatFile_31.pdf”}) and that file will be skipped over because the rest of the functionality is held within that if statement.

incidentally, if mgChangeFrom in our working example was ‘12345’ instead of ‘ThisFile’, the list returned would be {“”, ” ThisFile_01.pdf”} — that is, two list items — so it would pass the requirements of the if statement.

so, if the filename passes that if requirement we then change the text item delimiters to mgChangeTo (‘ThisFinishedFile’) and run the list items back together as a string — that is, {“12345 “, “_01.pdf”} becomes “12345 ThisFinishedFile_01.pdf”.

and finally there’s the line which actually does the name change. then the process is repeated for all the items in mgSelection. and we end with a message which alerts the user to the fact that the process is complete.

to turn this into a functioning app you’d add an on open handler and delete (or comment out) the line “set mgSelection to the selection”. the finished script would look like this :
screen grab of finished script

then you’d save it as an application, drag it to your sidebar, and drag-drop the files you want to rename onto it (left). and quicker than you can say “jiggle my janglers” the work’s done (right). you can see here how files that do not contain the change from text are simply skipped over :
screen grab of files being dropped onto app and finished result

so, this is the basic functioning script. but the next post will look at how to make it just a little more user friendly and robust. we need to add some error handling for different scenarios and maybe some other user safeguards.

’til then, keep grunting.


• related post : renaming finder items : renaming using automator or applescript.
• related post : renaming finder items II : renaming by list.
• related post : renaming finder items III : renaming by creation date.
• related post : renaming finder items IV — part 2 : the next step.

macgrunt icon