My First Applescript: Intelligent Photo Renaming
Before a week ago, the most time I’d spent on a mac was just playing around in the Apple store, while waiting for a Genius Bar kid to confirm that my salt-water soaked phone was indeed ready for the electronics graveyard.
But then, my laptop’s hard drive crashed, and I spent a night or two playing around on my wife’s Macbook Air. A coincidental promotion hit my email box, and fast forward to today, where I’m posting from my new Macbook.
Tonight, I wrote my first applescript. Applescript is a confusing but useful language that lets you automate a lot of things. There are operating system tasks, and functions that you can pass along to programs to do your bidding.
What I wanted to do was to import photos from my camera’s memory card. There’s software on the mac that will do it. And there’s more software from the camera company that will do it slightly differently. But I wanted one particular configuration available from the camera manufacturer’s software, but I didn’t want that software’s bloat.
What I specifically wanted to do was to is this:
Images are stored on my camera card with file names like IMG_xxxx.JPG, where XXXX is a sequential number. I want those files to live on with names like IMG_yyyymmdd_xxxsx.JPG where that middle part is the date that the picture was taken.
The applescript below does just that. I drag the files from the memory card to an app (icon) that has this script associated with it, and my desires are fulfilled.
As an added bonus, it moves RAW files to a subfolder named Raw. For movies taken by the camera, it changes IMG at the beginning to MOV, then moves it to a subfolder named Movies.
Perhaps this will help you do something similar in the future.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
on run {input, parameters} tell application "Finder" set theSelection to selection repeat with thisFile in theSelection set strFileName to the name of thisFile as text set strExt to the name extension of thisFile set strPath to the container of thisFile as text if not (class of thisFile is folder) then set rename to false set fileMove to "" if (strExt is "JPG" or strExt is "jpg") then set strBegin to "IMG_" set rename to true else if (strExt is "CR2") then set strBegin to "IMG_" set rename to true set fileMove to "Raw" else if (strExt is "MOV" or strExt is "mov" or strExt is "mpg" or strExt is "MPG") then set strBegin to "MOV_" set rename to true set fileMove to "Movies" end if if (rename) then set {year:y, month:m, day:d} to the creation date of thisFile set strThisDate to (y * 10000 + m * 100 + d) as text set AppleScript's text item delimiters to {"_", "."} set strEnum to text item 2 of strFileName set strNewFileName to strBegin & strThisDate & "_" & strEnum & "." & strExt set the name of thisFile to strNewFileName end if if (not fileMove is "") then if (not (exists strPath & fileMove)) then make new folder at strPath with properties {name:fileMove} end if move thisFile to folder (strPath & fileMove) end if end if end repeat end tell end run |
If you’re interested in how to configure the scripts to run automatically, let me know, and I can post more about that configuration.