Saturday, August 23, 2014

#1 2008-01-21 06:00:04 am

Kevin Bradley
Administrator
From: Independence, MO
Registered: 2006-03-13
Posts: 548
Website

Working With Images (Redux)

"Forget All That Other Stuff I Said" - Kevin Nealon
If you're human (and who isn't?) you've certainly had the experience of spending perhaps a large amount of time and effort doing some kind of work only to find that there was a much simpler answer - but only after you finished doing it the hard way.  I recently had that experience because I made the mistake of opening Apple's Automator.  Today you get to see me eat my own words, and I won't even use ketchup.

As a long-time Applescripter, I guess I regarded Automator as a "lobotomized" version of Applescript.  After having glanced at it when I first installed OS X Tiger, I saw (or thought I saw) that it wasn't very flexible, the pieces being prebuilt and all.  It was just "scripting for dummies."  After all, it couldn't do what a scripting guru like me and Applescript could do, right?

Wrong.  I've taken some time to get closer to my friendly-neighborhood Automator robot, and found that, despite my first impression, Automator, used properly, is very powerful.  As an example, I'm going to show you how to re-do two of my more advanced scripts, Scale Images and Convert Images from my Image Events article.  Once you see how easy it is to assemble the correct pieces in Automator, I hope you'll be much more likely to try out our little robot buddy!

Six 2x4 LEGO bricks of the same color can be put together in 915,103,765 ways... - from the Wikipedia
Much like LEGO bricks, Automator contains prebuilt parts from which you can make your artistic (or scripting) masterpieces.  This time, let's work backwards from my previous article and start with duplicating the function of the Scale Images script first. We'll talk about the Convert Images script re-write later on.

First, fire up Automator in your Applications folder.  When it opens you'll be presented with an Automator document window.  Automator document windows are a bit more complex than a Script Editor window.  The window not only has an area to the right for assembling your "LEGOs," but also contains lists of the applications and actions you can use and a small corner window that gives information about the particular action you're looking at.

First, let's find some actions that work with images and see if we can find one that will scale them.  Type "image" in the search box while the Applications folder is selected.  Scroll down the list and you'll see this:

 - Click to enlarge

Search for Image Actions.


The gray bars to the side of some of the actions show which ones Automator thinks are most useful.  Drag the "Scale Images" action to the workflow area and Automator will ask you whether you want to add a Finder action that will copy the image files so that you don't clobber the originals:

 - Click to enlarge

Asking to Add a Finder Action


That sounds like a good idea, let's tell it to add that action too.  So far we have this:

 - Click to enlarge

workflow1.png


Notice that I've opened the dark "options" triangle in the Scale Images action so that we can see what options it has.  There's only one, Show Action When Run.  I want to be able to decide the size I will scale the image files to when I run the workflow, so I'll check that box.  When the workflow runs, it will pause here and ask for my input.

Also notice that each action can pass information forward to the next step.  This is important, because we currently show a red "Files/Folders" input in the "Copy Finder Items" action that must be addressed.  What Automator is telling you is that while it will be happy to copy some Finder items, it doesn't know which ones yet.  We'll have to tell it something.  Often when working with images I create a new folder for the images I'm working with.  It makes sense to me to use the Finder action "Get Folder Contents."  Find this action by clicking the Finder application in Automator's list and drag the action to the work area above the first step.  The other steps will move down so that you can drop it.  You can select the "Repeat for each subfolder found" option if you often use nested folders.  I'm leaving it unchecked here.

Notice that you still have a red "Files/Folders" input on that action.  We still need to tell the Finder what folder we want to use. There are a couple of ways to do this listed in the Finder actions.  We could use "Get Selected Finder Items" or "Ask For Finder Items."  The first action uses the current Finder selection, the second presents an "open file" dialog box.  I usually like to select the items I want to work on and then select what to do with them, so I'll use "Get Selected Finder Items" for this workflow.

Here's the workflow we've built so far:

 - Click to enlarge

4 Actions in the Workflow


To finish up, let's change the "Copy Finder Item" action so that it saves the copied/scaled images to their own folder on the desktop.  We'll call it "Resized."  Then I'll set the default size in the "Scale Images" action to 768, the height of my screen, since mostly I'm working on desktop pictures and that's my screen height.

 - Click to enlarge

Setting Folder Name and Scaled Size


OK, we've built it, let's test it!  When saving a workflow, you have a few choices.  You can save just as a workflow file which is runnable from the Applescript menu just like a script.  You can create an application, just as you can with an Applescript.  Or (and here's one advantage to Automator) you can save it as a plugin that will show up in the Finder's contextual menus!  If you've ever right clicked on an item and seen the "Automator" submenu, it's there for just such a plugin.  So we select "Save as Plug-in..." from the "File" menu and it will ask what application we want to use this plugin with.  "Finder" should be there by default, if it isn't, select it.  Call the plugin "Scale Images plugin."

Now, find a folder of pictures and right click on it.  Go to "Automator" and select your new plugin.  In the menubar you will see Automator stating each action as the workflow runs.  The red stop sign allows you to stop the workflow at any point during the run.  If we had saved our workflow as an application we would have to alter the workflow so that it asks us for the picture files, since the act of launching the application could change our selection in the Finder (unless your app is in the Dock).

The original Scale Images script was 42 lines long and four of those lines were white space between handlers, so there were 38 working lines of code.  We've duplicated the functionality of that script with only 4 Automator actions!  Quite a time saver, eh?

You Can't Get There From Here
While Automator worked very well for the Scale Images workflow, we may have problems with the Convert Images workflow.  Why?  Because it has to decide at some point (or you do) what type of image results you want - JPEG, BMP, etc.

In short, Automator by itself can't make decisions and take different paths.  Workflows only flow in one direction, down, and from one step to the next step.  In programming lingo, we call the ability to change directions "conditional branching," and as a general rule Automator can't do it.

I say, "as a general rule," because in our peculiar case, we can.  It so happens that the "Change Type of Images" action allows choosing the image type at the time the workflow is run by selecting the "Show Action When Run" checkbox.  And conveniently, this action is the last one in our sequence, so we don't have to make any other decisions based on the image type.  If we had to move TIFFs to one folder and JPEGs to another we'd be in trouble!<sup>*</sup>

 - Click to enlarge

The Convert Image Workflow


Again, we've reduced what was a moderately-sized Applescript to 4 Automator actions.  So rather than being a "lobotomized" Applescript, Automator is a world of its own, with it's own nuances and strengths.  I hope you'll take a look at Automator and play with it a bit.  There are more actions you can download both here at Automator Actions and sites like Macupdate and Versiontracker.

'Til next time, have fun automating.  Go with the 'flow!

<sup>*</sup>Actually, Automator's Finder actions include a special case ("Filter Finder Items") that would handle this one, too, but other forms of branching don't exist.  You get the idea...

P.S. - As a "proof of concept," all of the images used in this article were scaled and converted from Grab captures using these two workflows!


Nitewing '98
--
I distrust morning people, largely because I suspect them of getting together early one day while the rest of us were asleep and setting up the rules of civilization.

Filed under: Images, Automator, Bradley

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.085 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)