Building Automator Droplets

Thanks to Ben Waldie’s new book, Automator for Mac OS X 10.5 Leopard: Visual QuickStart Guide, I now have a new friend in my circle of scripting. I know that a number of AppleScripters eschew Automator, and I admit that until I read Ben’s book, I was a closet member of that group. Not anymore. Automator has some extremely cool, useful, and easy to learn functions that offer us some simplified methods to personalize our machines, and just plain do some nifty things.

Our topic today, of course, is the Automator way of creating droplets. In my opinion, there is not an easier way to create a droplet outside of Automator, notwithstanding my world famous article: “AppleScript Tutorial for Beginners IX - Getting the Drop on Droplets”. For instance, let’s explore the droplet I mentioned in that same article about my kid’s GarageBand tracks. When they have created something truly extraordinary (or so they believe, anyway), they typically export the project in full CD quality to burn onto a disc for my wife (she has yet to join the iPod generation). I subsequently take those .aif files, and drop them onto a simple droplet to convert them to AAC, add them to a particular playlist in iTunes, attach them to a Mail message to my parents, and send the mail.

Building the Basic Automator Action

Using AppleScript, I spent an hour or so getting the code and variables just right, running some tests, adding plenty of comments to myself for later, etc., etc. Using Automator, I stacked up four actions, added some specifics for those actions, and was done in five minutes. Take a look at this baby:

Figure 1: First Four Actions Required (click images to enlarge them).

Using nothing but pre-installed Automator actions, I was able to choose the encoder I wanted to alter the .aif file to AAC, add the new track to a playlist of my choice, attach it to a new email message with email address, subject, and subject matter already defined, and then send it off. Absolutely amazing!!

Converting a Workflow to a Droplet

But wait, there’s more!! How did I go about achieving the complex task of converting this workflow into a functional drag-and-drop application? I am so glad you asked. I clicked File → Save As and saved it as an application to the Desktop:

Figure 2: Saving a Workflow as an Application.

A Second Droplet for Renaming and Moving Screenshots for this Article

The subtle beauty of all this is that any Automator workflow saved as an application automatically functions as a droplet!! It is completely ready to receive folders and/or files once it has been saved. And, yes, it really, really works. Consider, for a moment, all the lovely screenshots you hove in this very article you are reading. Those of you familiar to Macs know that screenshots are automatically saved to the Desktop with the names Picture 1, Picture 2, and so on. Well, instead of just re-naming the files as I generated the screenshots for this missive, I made a droplet instead:

Figure 3: Droplet for Renaming Screenshots.

Well, that worked splendidly for the first image, but since I was doing images one at a time, and not in a group, it would not function after that. Turns out that the rename action did it’s job just fine, but when it tried to move the file to the chosen folder, well, a file of that name already existed, and the action just quit. This particular workflow is really designed to accept a folder full of images, so I was left with a file on my Desktop with a new name, and NOT re-named and moved as I had hoped. Well, that was not acceptable, so I put in a couple more actions between Rename Finder Items and Move Finder Items to take care of the problem. Since all I wanted was to drag a new .png screen shot file for re-naming and moving to a certain folder, I changed the Rename Finder Items action to just give the basename of the dropped image NewAutomatorSS. Now, I had a name for the file that would be stable through the next couple of actions. Next, I added a Get Specified Finder Items action to grab the destination folder for all the screenshot images. Then came the tricky part. There were no actions already present to sort through all the filenames in the chosen folder, so I needed to add a Run AppleScript action to not only sort through those names, but assign a new name to the original, dropped image file that would also be the next name in the numbering sequence:


on run {input, parameters}
	set Next_Image_File to (path to desktop as Unicode text) & "NewAutomatorSS.png"--This is the original dropped image, with its first new name
	tell application "Finder"
		set Last_Name to the name of file -1 of folder (item 1 of input)--This is the last file in the folder passed from the previous action
		set ss_num to (characters -6 thru -5 of Last_Name as text) as number--Analysis of the two digit number of that file
		set New_img_Name to ("AutoDrop" & text -2 thru -1 of ("0" & ((ss_num + 1) as text)) & ".png")--Generation of new name for the original image
		set name of file Next_Image_File to New_img_Name--Actual changing of the name of the original image
	end tell
	return file ((path to desktop as Unicode text) & New_img_Name) as alias--Passing of a file reference to the new image to the next action
end run

When using a Run AppleScript action, understand that the variable input is always a list of whatever is being passed from the previous action. In my case, it is a list of 1 folder. I simply grab the last file in that folder, set the name of the file I had already re-named in the first action of the workflow to the next number in sequence, and then pass that file to the next action using the return command. The next action is my Move Finder Items from the original workflow, unchanged:

Figure 4: Adding an AppleScript to Rename & Mover.

Yes, this turned out to be a bit more complicated than I had hoped, but it was still easy, and it works great. It is designed for a single image; I selected Name Single Item from the combo box list in the Rename Finder Items action, and indeed, it chokes nicely when more than one image is dropped onto the droplet icon.

Some Things to Remember

Whenever you create a droplet, even for a specific purpose, take a moment to consider which action is going to be first in your list. It is that beginning action that will initially process the file(s) or folder(s) that you will be dropping there, and you cannot assume that Automator will read your mind and simply know what to do with your drop. Take this workflow for example:

Figure 5: Workflow to Extract Docs from among Dropped Files.

All I want is a document with a list of filenames and paths of any and all files that end in .doc. It works great if I drop a group selection of single files onto the droplet, but if I drop a folder, nothing gets processed, because Automator sees the folder as the dropped item, not the contents of the folder. If I add the action, Get Folder Contents to the workflow, like this:

Figure 6: Workflow to Extract Doc Files from Dropped Folders.

It now correctly processes the contents of the dropped folder (and any subfolders, if that little box is checked). The same workflow will also correctly process a group of dropped individual files, even though they are not contained inside of a folder. Each file is treated as a separate dropped item, and even though those items are not folders, they are passed onto the next action, and are subsequently properly filtered.

Conclusion

Automator is not going to kill AppleScript by any stretch of the imagination. If anything, Automator gives those persons interested in AppleScript another outlet for their creative muse. Personally, I enjoyed the generation of the second workflow presented here, because of the opportunity to use a few canned Automator actions together with a little custom mini-action that I wrote myself. Since I do not create droplets every day, this is an easy method to produce a functional droplet in a short period of time without needing to resort to my previous article, or examine other droplets to refresh myself about the necessary code involved.

Man you have some mad skills… I don’t have kids but as a son I’d love to have my father build me an application like this :stuck_out_tongue: I’m going to try to replicate this and I’ll report back.