the last two posts looked at using applescript to develop an app to make batch renaming finder items even easier than automator, or bridge, or whatever. first, we created the basic functionality, then we improved the script to make it more user friendly. this post will be the final instalment — the final revision to optimise usability.
the app has been created as a droplet — drop files on the app’s icon and they are processed by the script. pretty simple. but the app would be better still if it could be activated in a number of ways. so, we’re going to alter it so that, as well as accepting dropped files, it can accept a dropped folder of files, or just run when it is double-clicked.
it would also be better if it ran faster.
making it faster is easy. in its current form the script will rename 1000 files in a bit over a minute — not bad. but if you take every instance of this line :
tell application "Finder"
and replace it with :
tell application "System Events"
you’ll find that renaming 1000 files will take about a second. that’s even more not bad.
“System Events is an agent (or faceless background application) that supplies the terminology for using a number of features in AppleScript scripts. … Provides terminology to access disks, files, and folders without targeting the Finder. This can be more efficient than using the Finder …”
Mac Developer Library
ok, so much for speed, now for the varied functionality. the original script started with an on open command and assumed that the user would be dropping a bunch of files on the droplet.
here’s how to change that on open command so that it could accept either a bunch of files or a folder :
on open (mgDropped) tell application "System Events" if (count of mgDropped) = 1 then if (mgDropped as string) ends with ":" then set mgFiles to files of item 1 of mgDropped else display dialog "You're really using a script to rename one file?" & return & return & "OK ... whatever." set mgFiles to mgDropped end if else set mgFiles to mgDropped end if end tell my mgProcess(mgFiles) end open
the files to be renamed are now defined by the variable mgFiles before being passed to the subhandler mgProcess. the reason we are now using a subhandler for the renaming is that the on open command is not the only script initiator — we’ll also be including an on run command.
the on run command is what allows the app to run by just double-clicking it. when the app is double-clicked it will ask the user to choose a folder of files to rename — again passing those files to the mgProcess subhandler. so the full start of the script now looks like this :
-- thanks to Yvan Koenig for the on run/on open clue : -- http://macscripter.net/viewtopic.php?id=41214 on run set mgFolder to choose folder tell application "System Events" set mgFiles to files of mgFolder end tell my mgProcess(mgFiles) end run on open (mgDropped) tell application "System Events" if (count of mgDropped) = 1 then if (mgDropped as string) ends with ":" then set mgFiles to files of item 1 of mgDropped else display dialog "You're really using a script to rename one file?" & return & return & "OK ... whatever." set mgFiles to mgDropped end if else set mgFiles to mgDropped end if end tell my mgProcess(mgFiles) end open ---------------------------------------------
so, to reiterate, having both the on run and on open commands allows this script to behave both as a standard app and as a droplet.
there are a bunch of other minor revisions that need to be made to the original script to accommodate this new functionality — not least the encapsulating of the basic script within a subhandler. the new completed script would look like this :
but you can get the complete EasyRenaming app here.
have fun improving this script for even greater functionality.
• 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.