Executing exiftool from within Automator on Mac OS X Snow Leopard

Started by agill, March 06, 2014, 09:26:52 PM

Previous topic - Next topic

agill

INTRODUCTION - Automator Wrapper for Exiftool
Firstly I'd note that exiftool seems extremely powerful and comprehensive to me - all credit to Phil Harvey for creating it and making it freely available.
I'm new to exiftool - a complete beginner, and give my thanks to StarGeek and Phil for the help they've given. What I've done is to put a Mac OS X Automator wrapper on certain aspects of exiftool to execute what I need to do, as I find this more convenient than using the command line. I'm posting this explanation to help anyone else who wants to do something similar - updates/suggestions/corrections are always welcome.

CREATING THE AUTOMATOR APP
I've attached a PDF of the workflow/app I created, alongside which I'll step through what's in there and why.

To start open (double-click) Automator.app which normally resides in the system level Applications folder of your Mac. When Automator opens it gives a dialog picturing the options available. I'd advise starting off with a Workflow as that's easy to edit, and can be saved as an app when you no longer need to change anything.

Once you've selected Workflow, ensure you're in the Actions tab (over on the top left), and you'll see 2 lists on the left, and a panel on the right. To create your app you'll drag from the list nearest the panel into the panel itself.

I'd advise dragging over Ask for Confirmation first. When you do that it will present a dialog to complete. I put the text "Add ≥1 Keywords into that tag for photo(s)" (without the quotes) into the message area, telling anyone who runs it that this app adds to the Keywords tag of one or more photos. This may help in case someone runs it by mistake, so they can see what's happening and cancel as needed. I also put in some description to state that more than one file can be selected and passed to exiftool, and what happens if one or more folders are selected.
This Ask for Confirmation is not strictly necessary as the next step also has a cancel option.

Next drag over Ask for Finder Items. Select your Start at folder, which in the example is called Photos, and also the Type, which I put as Files and Folders to enable selection of both. I'd advise checking the Allow Multiple Selection box as that enables more than one file/folder to be passed through to exiftool which will then handle them all in the one command.

Next drag over Run Shell Script. Leave Shell as /bin/bash, and ensure Pass input is set to "as arguments". In the script area type the "for ...", "do" and "done" as shown in the PDF (no quotes). The "f" in  "for" simply acts as the target for what's then passed to exiftool, the link being the "$f" at the end of the exiftool command. That target is what you select in the Ask for Finder part of the app when it runs.

I'll explain the exiftool command as well as I can, but if you're not very familiar with exiftool I'd advise you follow the advice I was given, which is to try executing specific exiftool statements on the command line (i.e. in a Terminal window) before building them into the app. That helps a lot with problem solving as the errors returned on the command line are a lot more specific and helpful than what's returned from the app.

I'm using this app to add more keywords to the Keywords tag of photos, so after "exiftool"  and a space I specify that tag using:
-Keywords
Because I'm adding to keywords that already exist, I've then put "+" (no quotes).
If I were replacing the contents of the Keywords tag (i.e. overwriting) I'd omit the "+".
The "=" sign (no quotes) is then needed to let exiftool know the value that's going into the Keywords tag, but because i'm pointing at a file to obtain the new keywords, the "<" is needed after the "+" and before the "=". If I were stating the keyword actually in the exiftool script, I would omit the "<" and just use the "=". I've given an example of that in Points to Note below.
I've given an absolute path to a file called keyw.txt - I explain more about why in Points to Note below.
Single quotes are needed to encase everything from '-Keywords through to keyw.txt' to prevent shell redirection by the "<" sign.
As I want to add multiple keywords at one time, I've added a separator via "-sep ," (no quotes in the script) which tells exiftool that a comma will be used to separate each keyword in the keyw.txt file. Note the space before -sep and between -sep and the comma. Other separators are possible, I just chose comma.
Finally I've provided the link to the file(s)/folder(s) I selected in the Ask for Finders Items section via the "$f" - note the space preceding it.

The format of the keyw.txt file I've used is simply -
newkey1,newkey2,newkey3
where "newkeyn" represents the keyword I want to add. I'm not aware of any limit to the number of keywords. If you omitted the -sep, then using the example given, exiftool would add the complete text "newkey1,newkey2,newkey3" (without the quotes) to the next available Keyword tag level - basically as one keyword. With the -sep in place, it adds each keyword to the next 3 available levels in the Keywords tag, so if one keyword is already present at 0, newkey1 will go to 1, newkey2 to 2, and newkey3 to 3 in the Keywords tag.

Because I have Hardware Growler installed, I've dragged Show Growl Notification from the list to the panel, completing the message and description to let me know the new keywords have been added. The app will work without this added step.

Once everything's in place you can Save As... (Shift+Command+S) either a Workflow (easier to edit) or an Application. To run it more easily (e.g. by double-click) it needs to be an app. It's not hard to edit an app - instead of double-clicking the app to run it, just CTRL+click it, select Open With from the drop down menu, then Automator.app from the next drop down menu.

RUNNING THE APP
Double-click it and you'll be presented first with a confirmation dialog (Cancel / OK buttons) that informs you what's happening - i.e. the message you first typed in the Ask for Confirmation above.
If you hit Cancel it closes the app. If you hit OK it will take you to a Finder selection window pointing at the Start at folder you gave in Ask for Finder Items, where you can select the file(s)/folder(s) you want to edit.
Once you select file(s)'folder(s) and click the Choose button, the app will run the shell script to execute exiftool on the file(s)/folder(s).
Then, if you have Hardware Growler and have the Show Growl Notification in place, it will tell you it's finished.

POINTS TO NOTE 
Exiftool can take in multiple files and/or folders. Multiples of both can be selected contiguously by click+Shift, or at random using click+Command. For any folder(s) selected, the exiftool command will be carried out on all files within the folder(s). I'm not aware of any limit to the number of files/folders, and have tried with a few folders of varying numbers of photos; the response has always been fast.

There's no need to construct exiftool using an absolute path to a file to obtain keywords, e.g. you can simply put keywords you want to add directly into the app as follows -
exiftool -Keywords+=newkeyword "$f"
where "newkeyword" is the keyword you want to add.
But that would mean changing the app each time you want to add a different keyword which may prove inconvenient if you've a lot to add. Note that the single quotes are not needed here as there's no "<" sign.

Similarly you could omit the absolute path and point to a file in the same directory as the photos whose metadata you're changing, e.g. -
exiftool '-Keywords+<=%dkeyw.txt' "$f"
That would mean either moving the file to where you want to edit the photo metadata, or keeping multiple instances of the file, or ... to me it just seemed easier to have one file that I can edit as needed. It could also work well to have files for specific functions, e.g. mine in the example is named keyw.txt, but I could have another named datetag.txt for changing dates such as the CreateDate, DateTimeOriginal, etc. The tag being edited would also have to change, so a new app specific to a date type tag may be the best approach. The formats for each tag are explained in the exiftool documentation.

Please note that I'm using Automator within Mac OS X Snow Leopard 10.6.8, though from experience I believe anything I've put here will also work with Leopard (10.5) and Tiger (10.4 - when Automator was first introduced). I don't know if any of the above works with 10.7/10.8/10.9. though suspect that Automator has not changed so significantly in Lion, Mountain Lion and Mavericks respectively that the construction would need to change very much, if at all.
I understand that notifications are built into Mavericks however, so Hardware Growler may not be needed. Not having Mavericks, I don't know how that part of it would work, but I'd guess there's likely to be a notification that can be dragged from the list to the panel in Automator as with the Show Growl Notification.

agill

Just a quick follow-up note.

When creating the "for" loop in the Run Shell Script action of the Automator app, I put for f in "$@"

The in "$@" is not strictly necessary as $@ is assumed when no "in" is specified.

Minor point, but hope it contributes,
Adrian

DGMAC

This guide was very helpful for me with the following tweak to make it work on El Capitan (per this thread: https://forums.macrumors.com/threads/exiftool-el-capitan-10-11.1906401/):

Add /usr/local/bin/ before the exiftool command under Run Shell Script. E.g., /usr/local/bin/exiftool ...

Otherwise the workflow will return the error 'exiftool: command not found.'

Apparently Automator doesn't know where to find the exiftool command without adding /usr/local/bin/ ever since ExifTool version 9.99 released on July 23, 2015, which 'Fixed problem with OS X installer on El Capitan (now installs in /usr/local/bin instead of /usr/bin).'

And, as agill predicted, the Show Growl Notification can now be replaced with 'Display Notification.'