Leopard Innovations • 1:09:29
Automator actions are the foundation of creating workflows that use your application. Go hands-on with Apple engineers as they guide you through the creation of basic actions using AppleScript Studio, Cocoa, Objective-C, and other scripting languages.
Speaker: Sal Soghoian
Unlisted on Apple Developer site
Transcript
This transcript has potential transcription errors. We are working on an improved version.
Thank you very much. Thank you for being here. Thank you for taking the time to come in and learn about Automator and creating actions for Automator. Can I switch to the demo machine for just a second? We have to call an audible here. Is that okay? Yes. So some of the project files might not be at the URL that was in the precious slide, so I put an extra copy up on Automator.us. If you go to Automator.us slash Leopard, you'll get this page here.
And if you click the examples link, you'll see at the bottom there's a link there for WWDC Session 206 Project Files. If they're not on the Apple Developer site, you can download them there while I'm going through my little ten minute slide thing. Okay? Can we go back to slides? Thank you.
So I'm going over here because the slide's over here. I'm an analogue person stuck in a digital world. Okay. Welcome to Session 206, Creating and Building Automator Actions. And this is going to be a hands on session. Today, you will actually create actions yourself. You will build them here, live, in multiple languages, giving you more power than you ever thought that you had.
So what you'll learn today is you'll learn basically how to build Automator actions and how to deploy Automator actions. It's very simple. So, what is Automator? Back in Tiger, we came up with a very interesting and powerful technology that allows users to create automations, kind of like recipes for doing things over and over on their computer. And customers often think of it in there terms as visual programming.
For example, can you imagine being able to walk up to the computer and say okay, computer, I want you to go to a certain webpage, I want you to find all the images on that page that are linked from the little thumbnails, I want you to download those to my, oops, it's giving me extra stuff. How do I get back to there? How do you do button? Button. No. Back. Button. How do you use this thing?
( Laughter )
I don't want slide preview guy, I want actual slide. Escape key. There's no escape key on here. There is on here. Interesting. Okay. So I'm talking to my computer. I say computer I want you to go to a web page, I want you to find any linked images on there and I want you to download them to my hard drive and then I want you to put them into iPhoto.
Wouldn't it be great if you could just talk to your computer and quickly tell it what you want it to do for you? Well, you can. Because in Tiger we created this incredible application called Automator. And what it lets you do is create a recipe that each step of the recipe becomes an individual little task that gets done by the computer.
And these tasks are called actions. The whole recipe itself is called a workflow. And when the workflow is run by pressing the run button, things happen. Automator executes each step of the workflow, or each action in the workflow, passing the results of one action one to another and then what you want done gets done.
In this case, it's copying images from a website into your iPhoto library. And that's the power of Automator. And it's been very popular and very successful. As a matter of fact, a lot of people have noticed it. And it won the Eddy for Best Productivity Software from MacWorld, which is really great.
( Applause )
Golf clap. Golf clap. That was really good. So Automator really derives its power from the actions that you have on your computer. And naturally, the more actions that you have, the more you can do with Automator. And that's why we're here today. Because we want you, Auto wants you and I want you to develop Automator actions. And we think that when you see the basic principles involved in creating an action and actually do it yourself you're going to find that hey, this is fun, it's powerful and it's a great way for me to express myself as a developer.
So let's look at how you create Automator actions. Well, Automator actions are Xcode projects. They're built in Xcode just like all other applications are built in Xcode, all the good applications are built in Xcode. And all the various projects that Automator uses, which are like actions, conversion actions and definition files are create within Xcode. And because Automator actions are created in Xcode, they can use any language or framework supported by the operating system can be part of your project.
As a matter of fact, you can take all that stuff and just kind of throw it in there if you want. It's great. It lets you be flexible. It lets you do what you want to do. If you have a skill set that involves writing objective C, Automator actions are going to be a great way for you to express functionality of objective C. If you use AppleScript, if you use Ruby, Python, Pearl, Shell, whatever you want to put into your Automator action, it's available to you. And it's done through three different templates that Xcode presents. The first template is an AppleScript based action whose run code is simply an apple script. The next template is a Cocoa based action whose run code is the .M.H Class stuff that Cocoa lives in.
And the third template is a shell based action whose run code is a shell command or shell script. And using each three of these templates you can access other languages. For example, within AppleScript, you can use the call method command to talk to frameworks, or you can use the do shell script command to talk to the command line. Or within shell you could use an OSA script command to send an Apple Event.
And you've seen in previous sessions some remarkable new things you can do with scripting bridge that allows Cocoa and Ruby and Python to be able to send Apple Events to other applications. So these three templates will provide you the tools to be able to express yourself dynamically using multiple languages.
So, the interfaces for your action, or the little bit of UI that you present to your users that have the parameters and controls on them are built using interface builder, the same application that you're used to building native Mac OS X applications and use standard nibs. You can add custom controls if you want. And you can also take advantage of Cocoa bindings. So you don't have to write any kind of specialized code to be able to connect your interface into the code of your Automator action.
Some of the actions like AppleScript and Shell use this parameter object and you declare your various parameters that you want to use. Then you select the control and then identify which parameter is bound to that particular control. At run time, whatever your user settings have been, whatever the settings your customer's set in the workflow get passed your run code automatically. You don't have to do anything special to get that.
Now, how to use power actions? What are some of the ways that actions can be designed? What are some of the philosophy behind creating actions? Well, one of the easiest ways to create an action is to use Apple Events. Scripting. It's probably the most used method for creating Automator actions because it's simple and fast, very light weight in its inter-application communication.
If your action wants to talk to File Maker Pro and it wants to talk to InDesign and it wants to talk to Aperture, well, Apple events is a great way to communicate between those three different applications and you could put that kind of code within your Automator action. Whether using an AppleScript action or whether using an objective C action that has the scripting bridge code in there.
So that's one philosophy for creating an Automator action. Use Apple Events. Another way is to call external code sources. I mean, there's a lot of code hanging around in the OS that you can rely on and your action can call that code and use, take advantage of, what's available.
Like you might call a framework, like the address book framework, or you might call a command line utility, like it is here, so your action could basically just be a little bit of code that executes stuff that's already in the OS and you don't have to write any backend code for that. It depends o what you want to do. So that's a second way.
Here's another philosophy. You could add stuff into your own action. You could have your own code sources within your action. So if you have routines and functionality and libraries that you've created of your own special stuff and your own special sauce, you want to mix that into your action, you can. It's an Xcode project. And you could have other applications living in your action bundle. You could have scripts living in there and you could even have command line utilities in there. It's a bundle. Whatever you want to put in and call internally, you could do.
So there's three different ways that we just saw. You could use Apple Events as a way of communicating to other applications or accomplishing things. You can call external code sources, you can call internal code stuff or a combination of all of those. These are just three ideas and philosophies that you can think about once you start designing Automator actions for yourself.
It gives you a great way to think about how to solve a problem. With this action, I might use some Apple events. With this action I want to do a little but more interface, I want to use some objective C. Now, once you've created your actions, how do you install them? Where do they live? Well, Automator actions live in a couple of places. The first is in libraries.
Now in the system library folder there's a folder called Automator, and that's where Apple puts the Automator actions that ship with Automator. Now you don't want to put your stuff in there because you don't know what we're going to do to it once it gets in there. So you're much safer putting it at the top level library in an Automator folder. And you can also put it in the user's library Automator folder as well. Once the actions are in there, when Automator starts up, it'll recognize them, see them, and put it in its list of Automator actions for the customer.
The next place is you could put Automator actions right within your own application bundle. A lot of people don't know this. But it's a great way to deliver stuff without having to have a specialized install. Now you can still do a drag and drop install, but also still be installing Automator actions.
What you do is in the contents folder, you create a folder called library and then within that folder you have a folder called Automator Put your actions in there, launch services will recognize that when your apps installed and then tell Automator hey, you've got a new set of actions. That's the same process that Aperture uses, that Soundtrack Pro uses, the barebones BB Edit uses. A lot of companies put their Automator actions within their own bundle.
Now if you're a third party developer and you don't have an application, but you want to deliver a collection of all your Automator actions that's easily updatable and definable, you can create your own little bundle that's called an action pack. Now there's a template for this on Automator.us, click the developer link. There's a little link for one that's already pre-done. But it's basically a small application and it has a library folder within the contents folder and within the library folder's an Automator folder.
You just take your actions, put it in that collection and then the user takes your little action pack and keep sit on his hard drive some place. Launch services scans it, sees it and as long as that little bundle is on the computer, Automator will see those actions.
The nice thing about an action pack is to get rid of it or update it, the user just takes the pack and puts it in the trash and then downloads the latest pack. And the action pack template that's on Automator.us has an automatic mechanism that will go up to your website and check for a new version anyway. So that's how you deploy Automator actions.
To summarize, Automator actions use any language or framework supported by Xcode. There's three different templates. There's an Apple template, an Objective C template and a Shell Script template. And for design strategies, you can use scripting or Apple Events, you can call external code sources, you can call internal code sources or any combination of those. So that's pretty much correct. Guys? Come on, nod. Okay. That's my team over there. They're going huh? What? I didn't know that.
So we're going to build stuff. Instead of talking about it we're going to actually build stuff. Not quite yet. A couple of more slides. I appreciate your thought there, though. So we're going to build stuff. And what we're going to do is we're going to create some Automator actions, three different actions, for a image processing workflow. Cool. And each action, the workflow will start, we'll take some images from my Mac Mania 4.5 trip to the Mediterranean and then the next action will find images that have been key worded with a certain spotlight key word.
And then the next action we're going to create is called select images that'll support a wonderful, beautiful image kit kind of window with floating pudness and you'll be able to select the images that you want to process in that window and then we're going to create an Automator action in AppleScript that will convert the images to a letterbox format. So instead of being your standard four by three it's going to be your 16 by nine. And then it will open up all of the convert3ed images into preview.
Pretty cool little workflow, right? So the first action we're going to create is called find images by keyword. It will be a shell script action. You like my Nabiscoesk kind of thing here, right? My little touch. The next action will be called select images. It will be a Cocoa based action and it presents this nice little UI that you've seen before.
And then the third action is an Apple Script action where the user has a choice of whether they want to make their image a letterbox format by padding it or by cropping it. And the final result will be wonderful images like this of Corsica, that's actually named sunset, but it's four o'clock in the morning. That's sunrise. Apparently I was messed up so I thought it was the sunset. Let's not talk about that. It's like a month of therapy. Okay. So let's go over here and build some stuff.
By now you've gone to the Automator.us, thank you, you've gone to the Automator.us site or the Apple site that was listed in the URL and you've downloaded your little set of files called WWDC Session 206 projects. And within the folder that you downloaded there, you'll notice that in red there are three projects. And in green there are the same three projects, but they're done.
So, if something goes wrong and we can't create them, we've got backup. And in addition there's a folder that says put contents in user library. That's in case my backup goes wrong, these are the completed actions that we just shove off in there, we look like we're heroes.
There's also a zipped file called demo images. I'm going to copy that to my desktop here and I'm going to unpack that. And there's a folder called demo images and if we open it up and set the viewing preferences here to like that, and they go like this, then you have a nice little, let's make it arrange by name, very good, okay, then you have a set of demo images that we're going to be using today to do some processing. So everybody ready to begin? Okay. Is this semi interesting? All right. Good. I just want to make sure you're there. So we're going to open up the first project. It's called find images by keyword.
And you know what? I'm going to set this thing to be globally, use that as the default because it just makes it a lot easier and neater to do it that way. And while I'm at it I'm going to grab a little cheat sheet here. Yes. You're seeing Sal use a cheat sheet just in case I can't remember the name of a variable. Okay. So there's our project file. Let's double click that. And this opens it up in wonderful Xcode, who we love. It's getting better with each incarnation. We really enjoy using it more and more every day.
That was a nod to Tim, the project manager. Now let's look at what's in an action project. So over here under action group and files, you'll see the first folder is one called scripts. Now this scripts folder will exist if your project is an AppleScript based project or a Shell Script based project, you'll have this folder. Now if it's a shell based project, you'll see this thing called main command and that's your, basically your shell command, and that's what it looks like right there. And we're going to go over that and explain it.
Then you'll see that there's a resources folder that contains your nib. And info p.strings file, and this file contains the localized descriptions that go along with an Automator action. You can pretty much field out most of this kind of stuff because it's boring, but I make it easier for you.
And then down here, there's a target and this will be the final Automator action once we complete building the project. Now let's take a look and we'll look at this code here first. And I'll start there and just kind of walk you through what this is about. Now since this is a shell script based action, how many in here are shell writers? Oh, the cool people.
And how many in here are the Objective-C guys? And how many in here are AppleScripters? Say it loud, say it proud. Okay. So we have a nice mix in here of everybody. So I don't expect you, if you're not used to shell, to understand what it is, but I'll walk you through this command up here and just explain what it does.
So the way that information is passed to an action is Automator takes the results of an action that precedes your action and passes that to your action. In this case, we're looking for references to files or a folder. And that will be passed to this action by Automator. This reference will be in the shape of like a POSIX path.
So the first thing we'll be doing in this command, in this little script here, is to iterate the list of paths that is passed to it. Now this comes from standard in, for those that know shell, it will be in standard in. so you'll see a command here while read, and then a variable called folder path do and what that's doing is it will go through each paragraph of the list of folders that's passed to it and do this process with that list.
MD find is a shell command that was introduced in Tiger. MD find is for Meta data. It's the spotlight command line tool. And so it's going to only look in the fo9lder that is passed from the previous action. And then it's going to perform this little weird looking thing. This is called, right here, where item content type is a public JPEG. All that's saying is I'm looking for anything in this folder that's an image, that's a JPEG or it's a TIFF or it's a PNG file.
In addition to that, plus, plus, I'm looking for an image that has a key word and this is a way of indicating a parameter that will be passed to it. This is a variable that will come from the environment variables. So this is what we're going to get from the user when they enter in the name of their keyword. It's going to be replaced automatically during runtime. This dollar sign bracket keyword string is going to be replaced by whatever the user has typed in there. So that's our basic run code script.
A simple, single line, but a very powerful command that invokes the entire spotlight architecture to do it's magic for you. Oh, that was good. I liked that one. Okay. So the next thing that we're going to do is now that we've seen what the run code looks like is we're going to create a user interface that appears in the Automator workflow area when the user drags this action into the workflow. To do that we're going to double click the main.nib in the resources folder from the groups and files on the left.
Doing so, grab the peanut butter with your right hand, with your left hand turn the jar counter clockwise. So what this does is it launches Interface Builder, which has a totally new, redesigned look to it, a different kind of functionality. I'm getting used to it. I'm starting to like it.
It's that kind of a thing, you know, we're creatures of habit. So you'll see I just selected over here is your document window with the various kinds of instances in it. You'll see there's a parameter object up there and there's also a view. And this is the view.
The view is the action view as UI to the user. And on this view you place your controls that you want to interact and present the various options to your user. So since we're doing an action that demands text input, we're going to have a text field on this view that the user can type in the keyword that they want to use. Now if I type in here like text, you can see that it shows up a variety of different kinds of text fields. There's your basic text field, here's a secure text field, here's a text field cell.
But one of the new features of Automator in Leopard is that we now have this thing called workflow variables, where the user can take these either predefined or user defined variables and drag them into text field so that that content changes dynamically as the workflow is run. Now those aren't your standard text field. They're called token fields. So I'm going to go down to my little search here and type in the word token.
And you'll see I have two options. The one on the left with the little gel in it, that's a standard token field. You'll find that in like the date and time system preference for when you can drag in the formatting kind of things you want. That's only takes tokens. We want a special kind of text field that will take either text or tokens and it's to the right and it's called an Automator token field.
And we can't really afford a description yet. We're hoping that we can get some resources from above to be able to put in a description. So we're going to take that and add it to our view by just simply dragging it over and letting it go on the view.
Now once you've done that, you can bring up the inspector for that, the attributes inspector by just typing command one or by just selecting it from the tools menu like I'm doing now, and this brings up this new, unified palette for getting all the various information about that particular text field, being able to set it's various attributes and qualities. And one of the things that you'll notice here is if you select the ruler that's your size inspector, you'll notice that the control is already preset for small.
By default, Automator guidelines suggest that you use small controls in designing your Automator actions, and that's because vertical space is at a premium and you want to be able to use up, you don't want to cram it in there so it's ugly, but you want to be able to be conservative in how you use vertical space in designing your action. So we do it for you by presetting it to small. Now watch this.
You can see the wonder of Interface Builder in my shaky hand that as I move it over here closer, it lines up on the baseline and then once I get a certain distance from the label, it stops, there's a little vertical line that appears so that you're following the interface guidelines for the distance between a text label and the control. And then we'll take our text field and expand it out to about the edge of there. I think that's big enough for a keyword.
So there's our user interface. We have a little text label with a text field and now what we need to do is declare a parameter that can get bound to the text field and that parameter will then get passed to our code. Now you remember that in the source code over here, you can see I have selected and conveniently left off to the right of that screen over there, wasn't that class? It is the name of our parameter. Keyword inter cap string.
That's what we're going to use as our parameter. And to declare a parameter in Interface Builder, it's really easy. You just select the parameter's object, go to your inspector palette, click the first tab there at the top and that's your attributes inspector. And right here will be a list of all the parameter keys that you want to declare. There's a plus button at the bottom. Enter that. And then your new model key will be put, entered right there. It's pre-selected for you. So type in exactly the same way. Keyword inter cap string.
Keyword inter cap string. And then click someplace. So that's our parameter. That's what we're going to bind to the text field. Now to bind it to the text field it's pretty simple. You can just select your text field. And then in your attributes, your inspector palette, the fourth one, with the little green circle and box is bindings. If you click that, these are all the possible bindings that you can do to that particular text field, or token field in this case. The first one is the one we want. Value. Select that and it will disclose itself automatically for you.
And the first thing we do is say there's a check box, we want to bind that to the parameters object and the model key that we're looking for will appear in a pop down list for you automatically. Select keyword string and now that will be bound to that text field. Here's a tip though. Anytime you're doing text fields, they don't really take what you put into them until you start to run and sometimes it gets messed up. So there's a nice little option down here. It says continuously updates value. Select that.
And that way, once the user types, it won't accidentally delete that if they tab out of it or something else, it'll get their value. So here we are. We're bound to the parameters object. Keyword dot keyword inter cap string. And we're continuously updates value. That's our user interface. We save and we can quit Interface Builder and go back to Xcode.
That's our UI. So we have run code and UI. There's one other thing that we need to do is to let our target know about all of this. Select your target, which you can disclose here with the little triangle, select that and hit the info button on the Xcode tool bar. And this brings up all the information about your planned action. General, any kind of build properties. Blah blah blah. Rules, nah, we're not interested. Properties. Properties is what we want to go to.
So the first one up at the very top is important. It's a unique identifier. It's the bundle identifier for it. And there's different formats for doing that, but probably the best way probably is com.yourcompany.automator.mushednameofaction is probably the best identifier that you cold use and it's an appropriate way.
So you want to identify that's the way that Automator keeps track of your action. It doesn't go by the name, it goes by your identifier. And then a version. If you've revved, if you've already built this one before and now this is the new improved 2.0, then you would put the new improved 2.0 there.
And down here on the pop up is where you can get various and input various information about your Automator action. And this is important. Under the general category you'll see that there's one for name and this is where you'll enter the name of the action as you want it to appear. And there's also one for application. Is there an application that you want to associate this action with? In this case we're just using preview, although we're not doing anything with preview we like associating with preview because it fits well within the photo world.
And for category, we want to us AM category photos and all that's inter capped. And there's a list of all the carious categories that Automator uses on the documentation site for building Automator actions on the Apple website. Did I get that right Emily? Okay. She reminded me about that. And then finally the icon name. Where will Automator draw the icon from? Will it use an existing app like Preview? Or if you have a custom icon you would add that to your project and put it's file name in there and it will see it.
The next thing in the list is input. Okay? What kind of data does your action expect to get? You have to tell Automator what kind of, the format that you want your information. Because Automator will do the conversion between different data types for you, if you tell it what you want.
And in this case we're saying give me just a plain Cocoa string, which is like a POSIX path. That's what I expect. And when I'm done I'm going to go to the next one and say I'm going to give that back. That's the kind of data I'm going to pass back, is another Cocoa string when I'm completed.
Sometimes actions might pass back a totally different kind of thing. Like if you're action generates an object within an application, you might pass back an object reference to that within the application even though you took a file in as input. So this is how you tell Automator how to treat your action when it's using it. Then there is your parameters.
And this is where we want to add a parameter. And guess what the parameter name is? Keyword inter cap string and there's a pop up is it's value of string integer Boolean real or other? It's going to be a string. We don't have any default value we're going to use, we're just going to leave it empty. But if you had placed a word in there that would show up in the text field automatically when Automator drew the action view for you.
So there are also settings in here for required resources. Are there any warnings you want to post to the user? Are their keywords that you want your action to be searched by? Is there a description and are there other actions that are related to this one, like if you like this action you might like this one instead? And so that's pretty much what we're going to set in there. Close that. And I think we are ready. Nervousness. Did I kill the goat this morning? We're going to click build and go.
Na na na na na na na. Na na na na na na. Na na na na. Op, there he is. Okay. And then Automator will launch. What build and go does is it secretly puts the Automator action into a place where Automator can see it and it loads it up without actually installing it. It's kind of tricky. So Automator starts up and the first thing you see is that we have this new wonderful starting points feature.
For people that aren't used to using Automator, it's a great way to get started. It guides you to creating your first initial step in the work flow. Like do I want to work with files and folders? Do I want to work with audio things? And we're working with photos. We're just going to click custom and go choose. And it will just leave us a blank workflow.
And for our purposes, we're going to turn that off by going over to the Automator menu and choose disable starting points. Since we're going to be using this in testing, we don't need to keep doing that. Now, let's see if this will actually work. I'm going to go to the finder, to my desktop, I'll actually open up a new finder window to do this, and drag the folder demo images into the workflow area.
Yes. Just wantonly drag it into the workflow area and Automator will automatically create a new get specified finders action containing our dragged in item and the result of this action will be a path to that particular item. We don't care what format that it's in. it might be an alias reference. It might be a finder item reference. We don't particularly care. Automator will handle the rest.
Let's go up to our search field here and type in keyword. I barely got through key and it says find images by keyword. Drag that in and let go at the end of our workflow. So here's our newly created action. It gets us an input. A POSIX path from the previous action identifying a particular folder. And then we're going to just enter in here the word Mac Mania.
It's, for those that don't know, okay, it looks like that. M A C capital M A N I A. macMania. That's from geekcruises.com. there's my shameless plug. Okay. So now we're ready to run our workflow. Click the run button and something happened. Where do we see the results? Well we have these new, wonderful inline data views within Automator actions. There's actually a result gel right here at the bottom of our action view. You can click that, um, okay. This is where we switch to plan two.
All right. It would return a list of all the various images in there that have been key worded with macMania. Not to fear we know it'll work. So just quit Automator and go to the finder. Choose new and let's go to the user library folder. And within this folder we're going to create a folder called Automator.
New folder. Automator. And you'll see it appear up here and we're going to open that up and then go back to our files Let's go back up here. Session projects. And I'm going to open up put contents in user library. Automator. And drag in the find images by keyword action into the workflow area.
Okay. Now let's launch Automator again. And what is the shortcut for doing that? Let's just go to applications and Auto's right on the top. Let's keep them in the dock. All right. Now let's try this whole thing again and see if it'll work. So I'm going to drag in the folder, then I'm going to go keyword, find images by key word. And then type in macMania.
And run it. And then check the results. Okay. Backup plan's not working. All right. Well, we'll continue on and find out later what's going on here. Let's make sure though. Let's check our script. Let's look at the code. Keyword string, I'm going to copy that just to make sure I did it right.
Let's go over here to my nib and select my parameter, type command one and make sure that that's what I entered in there. It is. I'm going to select this up here and I'm going to choose my bindings and value parameters, bind to parameters keyword string is correct. Continuously updates results. Save. Okay.
And quit. And I think we're done about everything except for go over here and select that. And choose our parameters, keyword string looks like it's there. It looks fine. Yep. That's it. String. Oh, the images, I believe, are still key worded, unless they got munched in the download, which is possible. Okay. Let me try this again.
I'll try one more time here just in case. In Auto, you're running. Let's go to finder. Drag in my images folder. And then go keyword. And type in macMania. Hi a return on that, just in case. Make it work. Go. All right. Results. And nothing. Okay. Well enough of that. They're key worded in the image itself. And if you get the info window here, you'll see that more info.
Okay. How did the keywords get taken out of the images? Okay. Yes. Good idea. Thank you. And let's look for the word macMania. Ah! Okay. Well then let's do the next best thing. So we actually were right. Let's select all the images and select this, go to tools, inspector, sorry about this. Click on that. Op, they are key worded. Spotlight doesn't know about them. Why not? Ah.
Let's take these guys and duplicate them. Now let's see if Spotlight recognized them. Sorry about that. That's a bug, isn't it? No. okay. That's okay. We can bypass this action anyway. it's all right. But you get the idea. We didn't do anything wrong. It's not our fault. That's the important thing I've learned in product management. It's not my fault.
(laughter) okay. So, that's building an action with the shell command. Let's hope that it gets better when we go to Objective C. so the net action we're going to build is going to take whatever gets found and then present it to the user so that the user can select which images can be processed. And to do that, we're going to cross our fingers and open up the selected images project. At this point, I will drink water.
It happens to the best of us. Double click and open up this project. And you can see that this is a little bit different. Instead of a script folder over here on the left, you now see that there's a classes folder because this is about writing objective C classes and it has a header file.
It contains some stuff. And it contains then a dot M file, a main file that constrains some code that I already have entered in here for you for those that didn't know how to write objective C, I didn't want to go through the process of giving you each on of these lines.
And then basically you have some code here at the beginning that does your setup when the action runs and it will do the tear down for the memory when the action closes. You have a new kind of run method within Leopard where things can happen dynamically while your action is executing. There's a section here that creates the photo browser and then finally this is for presenting a dialogue.
And there's a little disposal routine down here. Then responders for the close and open buttons. I'm not going to get into the specific code, but basically that's what I'm looking at. Over here we have a resources folder, just like the previous action, and we have a select images nib. We're going to double click that to open up an Interface builder.
Our view. Now this view's different. This view uses one of the new hud look things that's in IB3. so this window has a translucent effect to it. It has a cancel and an okay button. And you'll see that we still have a document object over on the left and we have our library controls over here on the right. Now what we're going to do is connect the various controls up to our files owner, which is our document, our Xcode project. So we want to use an image browser. I'll search over here in IB for browser.
And you'll get one right here on the right. This is part of the new image kit and it's an image browser. It's really simple to use. You just drag it over here and let go on the window. Now we want to kind of push it up to the top and the left and it'll snap into place for you automatically.
So it'll fit right in under the title bar. Then grab the bottom right grow handle and drag it down toward the buttons and the right. You'll see it'll snap right where it's supposed to be for the buttons to be there like this and so it'll sna0p into place.
And by default it will display images in this manner, at this size and with no names or anything else. We can change that by opening up our inspector window here. If you don't have it open, just choose command one for attributes inspector. And you'll notice that there's lot of things that we can change about the way that it displays images. The first is we can have the images titled so you can turn that off and on, and we want them outlined. It'll place a little crisp outline around them. We want to use that too, as well.
And then you can control the scaling of the thumbnails. Give it one nudge up to like five six. It think that's a good size. I think that will work perfectly for us. So that's, we've dragged out this new image browser into our window and now we want to wire that up to our code. Well since we're using Objective-C in Cocoa, you do it slightly differently than what we did before.
Holding down the control key, click on the files owner and drag that, you'll see a string come off of it, drag it over on top of the image browser and let go. A little pop up will come up here that'll say outlet, what outlet do you want to use? That relates to the chunk of code in our project that will control this browser.
There's only one option, underscore browser, select that and that will do the attachment for us. Now to indicate to the browser, that just indicated to the code that we have an image browser we're using. And then to indicate to the browser what source are you going to use for your images to display, command click on the image browser itself.
Oh, I'm sorry, no, don't do that. Control click on the image browser and drag the little string from that over to the files owner and let go. No, and that didn't work. Command click on the browser. And what are we missing? Something. Oh, I'm sorry. Okay. Let's do this again.
Finger, control key, click on image browser, this window wasn't open all the way. That's what we want. And we're looking for source. And we're not seeing source in there. Data source. Hmm. What's up Kerry? Why am I not seeing data source there?
- I think it's a bug.
- Okay. I've just been informed that that's a bug. That on some of the demo machines it appears and on some it don't. some of you might have it and it's called data source, in which case you would grab the thing next to it and take it back over to the files owner and let it go.
I'm not seeing data source over here, but that's okay. We always have our backup option. Once you wire up the data source, you have to wire up the buttons as well. Control click on the button and then you'll get your pop up hud. You'll see one under sent actions selector.
Grab the little selector circle and drag it over the files owner. When you let go it'll give you two options. Is that a cancel button or okay. Choose cancel for that one. And then control click the other button, grab the selector from that and go over to your files owner, let go and choose okay. So both of those buttons have now been wired to the code that's back in the project and we're done with our interface.
And I'm going to save and quit. Of course, this is not going to work for me. It might work for you if you did have the other object there. So instead, what I'm going to do is go back down to the finder, go to my session projects folder, my put contents in, grab that select images action and drop it in there. And now let's launch Automator and see if it works. Okay. So this time I'm going to select the image files themselves. I'll select it first. Let's see. I don't need the copies here. Go through and do a little clean up.
And we're not going to copy. Okay. I'm going to grab my six images, drag them into the workflow. And now we'll get a get specified finders items with those images and then let's see if we have, in Automator over here, select images, there's the actions, we drag that into the workflow area. And now we're ready to run. So what should happen is, ah! It showed up. Well it didn't show up pretty, but it did show up.
That's good. It is a beta. So this is, you'll get a nice dialogue that actually looks good in real life and the user can select what images they want to work with, so I can pick a couple of images like those and then go okay. If you click the results gel in the action, you should get a picture of a bunch of images in there as the result. And it's not showing that. Okay. Quite nice. Very good. Okay. So we're done with that one.
Phew. All right. And who used this machine before me? Was it Nebble? Oh, I found my scapegoat. All right. So the next action we're going to create, it actually works better than this, boy is this good salesmanship, or what? Yeah, it really works better than this. It's so exciting. We just watched him fumble for like an hour. It's wonderful. Okay. They last action is going to be convert images to letterbox format.
And this project is an AppleScript project using AppleScript. And that'll work. Yeah. It's duct taped. It'll work, I'll tell yeah. So let's take a look at the project. Just like the Shell script, it has a scripts folder and if you select the main you can see, and click editor, you see that here's the actual AppleScript.
It also has a resources folder that has a nib and it has a target just like the previous one we looked at. Lets take a look at the script first by selecting main.applescript and I'll explain what this does. So here's your basically the running routine. By default, Automator will pass in this variable called input at the top. A list of file references in alias format. So it'll take the POSIX paths, convert them into an alias reference that AppleScript understand and pass them to the action.
Then there's another thing that gets passed to the run handler at launch and it's a parameters record. This variable called parameters will contain a record of all the different controls and their values. So if it's a pop up, it'll have this value. If it's a text field it'll have that value.
And then what you do in your script is you extract from the parameters record whatever value you're looking for and place it into a variable that you can use in your run handler and there's just a simple repeat loop where it checks to see if the method indicator is zero, it must be padding, therefore I will pad the images if it's one then it must be cropping and I will call these sub routines. Using the image events built in image events architecture scriptable image manipulation architecture here, it just calls a sub routine that opens up an image and crops it and scales it.
And the same down here, I mean this is the cropping one down here. And then this is the padding one. So that's all the code that there is for this. Let's double click our nib to work on the interface. So we open up the nib and we have our view.
I put a little label there called conversion method. So what we're going to do is present the user with a little pop up where they can choose whether they want to have the image converted to letterbox format by padding or by cropping. So to do that we need a little pop up menu. Just choose pop up as our search word over here and we have a couple of choices.
The middle one is a pop up button. It's your standard button pop up. There's also another one called Automator path pop up button. And this is a specialized control for Automator that just deals with. We just want a simple control for giving the user either or so we'll select a pop up button and drag it over here. A simple control for giving the user either or so we'll select a pop up button and drag it over here. Place it on our view.
And in our inspector window, we'll click on the size control, which is the third tab and choose to make it a small control. Remember I said Automator UI guidelines want you to use small because we want to conserve vertical space. So now we've made it a small control and let's fill it out by just double clicking the pop up. It will expose its menu. Double click the first item and make it pad.
Then double click the second item and make it crop. Click the third item, go to the edit menu and choose delete. Now to make the default as it appears, just select pad and then click somewhere else in the view. Now here's a tip, if you want to make your control fit the menu items it will display, select a control and type command equal, and it will shrink it to the relevant size for you. Just a little tip.
Now let's take our control, line it up with our label and move it over until it clicks into position. Now we want to resize our window until its 10 pixels from either side of the controls. So I'll select the control, hold down the option key and you can see that instantly Interface Builder pops up these little windows with the number of pixels left to go. I'm going to grab that and size it down until its 10 pixels from the edge and 10 pixels from the bottom and there's my officially approved interface. Save that.
Now, unlike wiring up with Cocoa, AppleScript uses the same method for passing information to the run script as shell scripts do. So we need to click our parameters object over here, click the first tab and let's make a new parameter. We're going to call it conversion and then method inner capped, conversion method inner capped. And I'll just copy that to the clip board because I'm suspicious today.
Now let's take this parameter and bind it to the control. Just select the control, choose bindings which is your fourth tab, and now you have a series of options. You have content, selected index, selected objects, selected tag, selected value. We want to use selected index. And what that will do is when the user selects the control they'll be returned a number indicating which menu item was selected 0 being the first, 1 being the second, 2 being the third. So, put bind two and then under model key path if you click you can see our new parameters right there select that and choose save.
So we've now created our interface, we added the control we bound the control we declared a parameter. And now we're ready to go back to our code. The only thing we have to do now is in the code we place those series of X's with conversion method. And you'll notice that it's placed within pipes. That's to indicate to the script that this is a parameter, just like that. Otherwise it thinks it's a variable.
And, let's go down to our target, select our target, click the info button to bring up our wonderful parameters and properties interface, we've already given this an identifier. We've indicated that here's the action name, it belongs to preview, the category was AM categories, AM category categories. Category photo's, I'm doing this live with no net.
Now, for input we've already declared that this is the kind of thing we're looking for, we're looking for an AppleScript object, and not just AppleScript, we want AppleScript dot alias dash object com dot apple dot apple script dot alias dash object. And I will copy that and also use that as our output. And this is an alias reference in AppleScript format for a file. And let's go to parameters and we need to add our parameter conversion method. And this time it's not a string it's going to be an intriguer. And its initial value will be 0.
So we've declared our parameter in both the code and in our target. We are now ready to build and go he says breathlessly waiting. Yes, save everything I have and let's watch the fun begin. Alright, for this one I'm just going to check and see if it's there. Let's see, so I can type in like a word like letter box, it showed up. Will it actually join? It shows up.
Okay, that's all we need. Quit. Alright, and I'm going to go down to our project, I'm going to open the build folder and show you that we can go right in and drag our action copy of our action into our Automator folder that's in the user library. So now we have all three actions installed. We can try this and see if it'll work just for the heck of it.
Let's launch Automator and see if our work flow will actually happen. We're going to choose the demo images folder. And then we're going to search by key word, find images by key word. If you hit a return key it will select it hit the return key it will add it.
Then once we found those images by key word we're going to go select images. And there it is at the bottom. Hit return it joins the list. Use the up and down arrows to get to the action you want. Hit the return it will add it. Then once that's processed then we're going to crop it to letter box.
And then finally type in open and then preview and there it open images in preview. So, ostensibly that's our work flow. We'll see how good it actually works. Here we go. Key word Mac mania. And I want that. You know what? I actually want to see this when it runs.
Let's go to our options on the find Images by key word, click the options jell and we're going to choose to have this action UI appear to the user whenever the workflow is run. And we're going to do the same for the convert images to letter box format.
Show that one run. And we'll make our choices dynamically. I'll run the workflow. I'm looking for the word Mac Mania. Continue, and then I have no images so it didn't work. But, that's not all lost. I'm going to delete these actions here and then select all my images, drag it into the work flow. I will make this work I don't care.
Alright so now I bypass the search stuff. It's going to show us the images, we're going to choose the ones we want and what we're going to do to them. Ooh, so now, let's see. Do we want to crop them first? Yeah let's do crops. So here's a picture of Monaco, this is the coast I was on the ship right off. I took that picture.
I was I the Riviera. I took that picture. That's Monaco. And we're going to select that and the sunset at, the sunrise at Corsica. And we're going to crop those to letter box format. We're not going to pad them. We're going to crop them. So, watch what happens.
We're going to go okay, and then it's going to bring up the next and we're going to choose crop. Click continue, so you can see it really like took the center out of the image. It works though doesn't it? That would look great on a wide screen TV. And look at that. Ah, ah, it captures it.
( Applause )
Through the magic of chemistry and I'll see you at the bar across the street when we're done, not that I drink, okay. So, now let's try the other approach, right, alright let's run our thing again. And this time we're going to choose the other images. We're going to choose door knocker, goblet, leaning tower. I actually stood before the leaning tower. I didn't have the guts to go up but I did stand before it.
And the Western, this is where I stayed. I stayed at the Western Excelsior I Florence on the Arno River. Used up every hotel point I'd been saving for ten years but it was absolutely glorious. Florence is wonderful they have real tomatoes. Okay, back to our demo. So, I've selected these images choose okay and this time I want to use pad instead of crop. Click continue, and voila it padded all of the images and presented it to us in preview. So aside from the little bugaboo's that happened today what you saw and what you did is you saw that creating Automator actions is very flexible.
You can use a variety of approaches. You can us a variety of languages. If you're a shell person creating a little interface and then wiring it up so that the stuff comes in through environment variables is very easy to do. And very powerful. You can create all kinds of work flows that you can just drag out and then save and run whenever you want.
If you like objective C, I hope that you have better luck than I did today. But you can see that it's really with a new IB3 it's really drag and drop to wire up to the various parts of your code to the interface elements. And finally if you use AppleScript it works all the time, so ha.
( Applause ) Can we go back to slides now? Thank you. Slides, isn't that beautiful? I actually was there, blows my mind. So, if you want to learn more about this whole process there's lots of documentation. If there's one thing there's plenty of great documentation for Automator on teaching you how to do this correctly and easily. That's me up there.
I am, today I'm the automation technology evangelist. Well okay, I'm normally the product manager for automation technologies. But I also do that as well. And Kerry Hazelgren is the engineering manager for Automator. Raise your hand. That's him over there. It's his fault that everything messed up for me, him and Chris Nebel that was the problem.
And then for documentation there's the Apple development stuff. But Automator dot US is a great jumping off point because it has links to all the developer stuff too. And you saw it earlier, just go to Automator dot us if you want and see about leopard Automator dot us slash leopard. Then there are other great sites if you want to watch videos about Automator, Automator dot TV. Then there's Automator actions dot com. And there's a great online community there and they have thousands of actions that you can download and talk to other people about and use.
And then there's the wonderful Automator world blog that gives you the up to date scoop on what's happening in the world of Automator. Now we have a little bit of time we're going to do some Q and A here so you can ask questions and the first answer is I don't know what that went wrong it wasn't my fault, okay.