Configure player

Close

WWDC Index does not host video files

If you have access to video files, you can configure a URL pattern to be used in a video player.

URL pattern

preview

Use any of these variables in your URL pattern, the pattern is stored in your browsers' local storage.

$id
ID of session: wwdc2009-404
$eventId
ID of event: wwdc2009
$eventContentId
ID of session without event part: 404
$eventShortId
Shortened ID of event: wwdc09
$year
Year of session: 2009
$extension
Extension of original filename: m4v
$filenameAlmostEvery
Filename from "(Almost) Every..." gist: [2009] [Session 404] Using Autom...

WWDC09 • Session 404

Using Automator and Services to Integrate with Mac OS X

Mac • 45:05

Automator has been enhanced to enable creation of system Services in Snow Leopard. Learn how to build Services, create custom Automator actions and extend the flexibility of your application using Automator workflows. See how your application can become more integrated into your users' daily lives, enhancing the power of the entire system

Speakers: Emilie Kim, Itai Rom

Unlisted on Apple Developer site

Transcript

This transcript has potential transcription errors. We are working on an improved version.

Good afternoon. My name is Emilie. I’m an Automator Software Engineer. And I’d first of all, I’d like to thank DJ Jason in the back for the great music that you heard before this session. Thanks, Jason. Great way to start off the last session of the week. So for those of you who maybe are just sitting in here looking for a place to check your email before you catch your flights home today, the first thing I’m going to talk about is what Automator is.

So we’ll catch you guys up. And those of you who already familiar with Automator can just kind of sit back and relax a little bit. Though, I will be talking about what’s new in Snow Leopard, so maybe you should pay a little bit of attention. After that, we’ll be talking about how you can take Automator workflows and add them to your application. Therefore, extending your application’s functionality and enabling your users to customize your application. After that, we’ll be talking about how you can take your application’s functionality and put them into Automator Actions, and then use those actions to put your application into the rest of the system.

So first, let’s go back and talk about Automator. What is Automator? Aside from arguably being the coolest icon on the system, Automator is an application in that first shipped with Mac OS 10.4 Tiger. And it’s an application that you use to automate repetitive tasks. Once you’re able to automate these tasks, you can then customize your system to pretty much do whatever you want it to do. The terminology we’ll be using in this session is workflow. An Automator workflow is the process of actually accomplishing this task, and you do this by stringing together a bunch of Automator Actions. This is kind of similar to UNIX pipes.

How you have a bunch of commands and you string them together using pipes. So let’s take a look at Automator. This is a screen shot. On the right side of the screen, you’ll see that there is a workflow view and that contains a workflow, which is, if you recall, the act of actually accomplishing this automated task.

And then you have a bunch of actions that are strung together in the workflow. Now, once you have an Automator workflow what can you do with it? Clearly you can run it inside of Automator, but you can also run it in many other places as a stand-alone application, in the Finder, as an iCal plug-in, or maybe even a print PDF plug-in, so when you save as PDF you can actually run a workflow on that.

Also in Snow Leopard, we’ve added the ability to make workflows into Services, which you probably have heard a lot about during the week and we’ll talk more about that. So to create these different kinds of workflows we’ve added a new Template Chooser in Automator as you can see here enumerating the different kinds of workflows that you can create. And I’d like to draw your attention to the Services one for just a second.

If you recall, Services in Leopard and prior versions of Mac OS X were kind of not so great to use. They’re supposed to be very contextual and relevant to what you had selected. For example, if you had the Text Creating Service, this is so easy even a manager can do it.

To run a service on this you would have to go. I see we have a lot of disgruntled employees here. You’d have to go to the Services menu in the TextEdit menu and you click Services. And then this huge list of services comes up, most of which don’t have anything to do with the text you have selected.

Calculate MD-5 checksum? Yeah, I’m not so sure about that. So we’ve cleaned this up a lot in Snow Leopard with the help of the AppKit team. And so I’d like to show that to you in a second in the demo that we’re going to do. So first, I’m going to go to the demo machine and I’m going to give you a little tour of Automator and what Automator can do. And then we’re going to create workflows, and then use those workflows and create services.

And along the way, I’ll show you the improvements we’ve made to Services in Snow Leopard. So over to the demo. So first, we’re going to launch Automator. Again, the coolest icon. We’ve made a lot of improvements to Automator on Snow Leopard. We have better performance. Reduced memory usage.

We’ve rewritten a lot of the actions including the Find and Filter actions. So those of you who are familiar with Automator I highly encourage you to check it out again. We’ve also, as you can see, added this new Template Chooser so you can select the different kinds of workflows that you want to create. In this case, I’m just going to choose a regular generic workflow.

I’ll click Choose. So now, you see I have an empty workflow view on the right, and I have my actions on the left. So out of curiosity, how many of you in the audience use Twitter? Show of hands. OK. That’s a lot of you. So what I want to do is I want to create a workflow that’s going to allow me to update my Twitter status. Now, this seems pretty easy.

And if you recall from earlier I said an Automator workflow is the process of automating a task. So what we’ll do, the pieces of this task is we’ll take some text and then we’re going to upload that to Twitter. So first, I’m going to build a workflow and then I’m going to drag in what I wanted, which was GetSpecifiedText. And this is just an action that has some text in it. And this one happens to say, Really excited to be at the Automator and Services Session.

After that, we’ll add an update Twitter status action underneath it, and this is just a custom action that we wrote for this session and it’ll update my Twitter status. So that’s all I have to do. I have the workflow, which is updating my Twitter status. And I have the actions in the workflow, which is getting the text and updating the status. So I’m going to go into the upper right-hand corner and run the workflow. And you can see that the workflow ran.

And now let me check my Twitter page and see if it actually worked. So here is my Twitter page and my status actually updated. So you might be asking yourselves though, this isn’t really all that interesting because, you know, this has just basically turned Automator into my favorite Twitter client. So what can we do?

If you recall I said that workflows can be run from all over the system, not just in Automator. So what we’re going to do is we’re going to create a service to actually do this workflow for us so that we can run the workflow from anywhere in the system and Tweet from anywhere. So I’m going to close this and we’re going to create a new workflow and it’s going to be a Services workflow.

If you recall the screenshots I had of Leopard and how the Services menu looked very cluttered, here I’m happy to show you the same text in Snow Leopard. And if we go to the Services menu, you can see that it looks a lot nicer. And we’ve cleaned it up a lot and added some nice icons and only show you the very relevant services.

So what I’m going to do is I’m going to go into Automator and create a new service. And as you can see, now there’s a new header view. And if I zoom in, this specifies where my service will be running and on what items it’ll be running on.

So here, I want to say that my service runs on text, because I’m Tweeting something, in any application because I want to be able to do this from anywhere. So if we zoom back out I’m going to add. This is going to be a very similar workflow to what we already created, so this is going to be an AskForTweetText action, which at runtime when I invoke the workflow will bring me a dialogue so I can type in what I want to talk about. And then I’m again going to update my Twitter status.

So here, I’m going to save this and it’s going to be called TweetThisText, and just going to save it. And then we’re going to quit Automator because Automator doesn’t even have to be running for Services to be running. And then, so now we’re going to back into TextEdit. We’re going to select the text. Let’s say I want to Tweet this.

So now, if we go to the Services menu you can see that TweetThisText is now a menu item in the Services menu. This is great, but it’s kind of annoying that I had to leave TextEdit to go to the Services menu to do this. So wouldn’t it be great if I could just say RightClicktheText in TextEdit and Tweet it? And there we go. Services are now in contextual menus, so I can just click on.

Thank you. So if I just select TweetThisText you’ll see that now this is the dialogue I mentioned that comes up at runtime, and it has the text that I have selected already. And if I can direct your attention to the menu bar, in the left side of the menu bar there’s a spinning gear icon. This indicates that a workflow is running in the background.

And if I click it, I have the ability to say, StopThisWorkflow. I don’t want to stop this, though. I want to Tweet this, so I’m going to just post this. And you’ll see now that the gear menu has disappeared. And if I go back to my Twitter page, now my new text is there, so this is great.

Another example of how you can use this though, is if you recall I said this service is going to run in any application. You saw it running in TextEdit, but now I want it to run in Safari. So I’m going to go to my favorite web page of all time, which is the Automator Programming Guide. And I think that everybody should read this web page. So of course, I’m going to Tweet it because that’s what you do.

And so I’m going to take the URL, and again you can see that it’s in the Services menu. TweetThisText. But it’s also in the contextual menu, so I never even have to leave Safari to Tweet. So I’m going to say, TweetThisText. And again, the gear menu appears and we’ve built in shortening.

Or URL shortening into this action, so I don’t run into the 140-character limit. And because I made this editable, I can even say something about this. So, Everyone should read this web page. And now if I post this you’ll see that the gear has stopped running. And if I again check my web page, Everyone should read this web page. And if I click it, then that takes me to the Automator Programming Guide, so.

[ Clapping ]

So that was an example of a service that runs on text. You can also run Services on files. So I’m going to do again a very similar demo that runs on image files that I have selected in the Finder. And if I take an image file, I can run a service on it that will upload that image to my favorite Twitter’s photo sharing service, and then Tweet that new URL. So that’s what I’m going to do now, is again launch Automator, create a new Service workflow. And in the header view this time, I’m going to choose an AnyImageFiles that are selected in the Finder.

And I’m going to add in the action that’s going to upload the photo. Again, ask for my Tweet text. Update my Twitter status. And if you recall, I said that Automator’s an application that you use to automate repetitive tasks. Now, I don’t know if many of you noticed, but I kept having to go into the dock and reopen my Twitter page every time I updated this just to make sure it worked.

It would be great if I could just do this in Automator and have Automator do this every time I run this workflow. So I’m going to add a GetSpecifiedURLActions at the end of this workflow, which contains a URL to my Twitter page. And then I’m going to say, DisplayWebPages. And so now I’m going to save this as TweetThisPhoto and I’ll save it. And again quit Automator. And so now, if I go into the Finder I’m going to select some photos.

I have a hangar, a mausoleum, a rhinoc. Or a zebra and a puppy. So if I select this puppy and I go into the Services menu, I see TweetThisPhoto. And notice how you don’t see TweetThisText, because TweetThisText is no longer relevant. I don’t have text selected. I have a file selected.

So I have TweetThisPhoto. Again, it’s also in the Finder contextual menu. So for those of you who had applications that were maybe sticking yourselves into the Finder contextual menu, this is how you can now do it. For those you who have sharp eyes in the audience you’ll see that there’s a ServicePreferences menu item. So if I actually click that, now you can see that we’ve added a keyboard shortcut, a Preference pane into Snow Leopard so you can enable and disable your favorite or least favorite services, as well as set keyboard shortcuts on them.

So here, I have my TweetThisPhoto shortcut. And I’m going to set the keyboard shortcut for ControlOptionCommandA. And so now whenever I hit CommandOptionControlA and I have an image file selected, it’s going to this workflow for me. So here, I’m going to select puppy. I’m going to hit my magic shortcut and the gear menu appears. The dialogue appears. This is the new photo URL that I have.

And I’m going to say, I really like puppies, and so we’re going to post that. And again Automator now automatically opens my web page for me and you can see my new post. And if I click it, now I have my photo up there. So that’s just a brief example of how useful Automator can be. Where did I? There it is. Of how useful Automator can be. And just, that was just a few of the great services that you can create using Automator workflow.

And so I highly encourage you to check this out and just play around with this and see what else you can do with Automator and Services. So what we just learned. We talked about what you can do in Automator, what Automator is, and how you can create workflows using actions. And then using those workflows to create services. So Automator is really powerful. But wouldn’t it be great if you could take all the power of Automator workflows and put them into your application?

So we’re going to talk about how you can run workflows in your application now. There are many applications that run workflows, and your application can now join the party. And you might be asking yourself, well, why would I do this? What is the value of the running workflows in my application? What you do when you allow users to run workflows in your applications you empower your users. There’s absolutely no way you can anticipate all of the needs of every single one of your users. Let’s say for example you had an application that was similar to Photo Booth.

And you have some pictures and you’re deciding, Well, I’m going to add some functionality so I can take the selected photos and upload them to different service for the user. So it’s easy enough to hard code and maybe uploading, or to import photos into iPhoto. Or maybe uploading them to MobileMe or Facebook. But you can’t possibly code in every single photo sharing application ever. I mean what if your users use Flickr? What if your users use SmugMug?

Or maybe they only use Twitter photo sharing services. Who knows. But you can’t possibly hard code all of those in. So if you allow your users to instead create workflows that do the uploading to Flickr or uploading to SmugMug, and all they have to do is save the workflow and then import that into your application, and then your application will run that instead.

Then you can focus on the more interesting things like the good user experience you want to provide for your users. And because your application does exactly what your users want it to do, they’re not going to look anywhere else for another application. Sometimes I’m told I speak too quickly.

I don’t know what people are talking about.

[ Clapping ]

[ Cheering ]

So, what do you have to do to get this great functionality? If you just want to enable the Services menu in your application, you don’t actually have to do anything. If you use just standard controls like an NSTextView or WebKits Web View, you get this functionality for free.

If you have custom controls in your application, like maybe you want to add the contextual menu to show services for your custom photo application, I encourage you to watch the Pasteboard Services and Interoperability on Mac session that James Dempsey and Peter Ammon gave yesterday which was a great session. And they talk about how you can actually use Services and put the menu into your custom controls. However, if you want to just run Automator workflows in your application, all you have to do is write one line of code. Not even kidding.

One line. And you do this using the Automator framework. Here we have AMWorkFlow. AMWorkFlowController and AMWorkFlowView following the stater [assumed spelling] MBC model. We’re going to focus on, in this session on AMWorkFlow. So all you have to do is call the class method on AMWorkFlow called RunWorkflowAtURLwithInput&error. And if your editor is more than 50 characters wide, this truly will be one line of code. So if you want more control over your, running workflows in your application. Like you want to know when actions are finished running or when the workflow finishes running, then you want to take a look at creating an actual AMWorkFlow object and then instantiating the Controller for it.

But there’s plenty of documentation on that. So for now we’re just going to focus on this one line of code that you need to run workflows in your application. So to prove this, I’m going to bring Itai up on the stage. He’s another Software Engineer in the Automator team. And he’s going to show you how you can take the Automator framework, and using this class method on AMWorkFlow just with one line of code run workflows from your application. So, Itai.

[ Clapping ]

OK. So, what I’m going to show you is hopefully pretty much exactly the same thing Emilie was just talking about, but at a much slower pace. So what we’ve gone ahead and done is coded up this hypothetical Photo Booth application that Emilie was talking about.

And we call it Picture Booth. You’ll notice the similarity to the Photo Booth app that ships standard on Mac OS X. There’s an image browser view up here with a bunch of photos that we’ve taken. There’s a little camera button. I can click on that. It brings up the standard picture taker.

I could take a picture of myself, hopefully if this works, and I could add it to my app and there we go. So that’s really easy to do. That’s just using the standard image kit framework. But if you’re like us, what you really want to do once you take these silly photos is share them with all your friends online. And you want to be able to do it really quickly so that you can get back to all the important work you were doing before you got distracted with taking silly photos of yourself.

So we’ve added this little pull-down menu down here and I can zoom in on that. Guess that’s hard to see. Now, if you were paying attention to Emilie’s previous demo, and you’ve read these menu items very quickly, you might recognize this TweetPhoto menu item. It’s actually almost an identical workflow to the one that Emilie just showed you running as a service. Actually, all three of these are workflows.

And when the user selects one of them, all that’s going to happen is the photos that they’ve selected in the image browser above will be fed into the workflow and the workflow will perform the action that the user selected. Now, actually, if I click one of these right now nothing’s going to happen because I haven’t actually added the one line of code that you need to do in order to do that. I’m going to throw caution to the wind and just write it up here on stage so you all can see just how easy it is. So let’s click that.

Open up the Picture Booth application. Scroll over and that shouldn’t be there. OK. Pretend you didn’t see that. So there’s a bunch of code in this application. None of it is really that interesting. It’s all the code that you would have had to write anyway if you were going to write this app. However, this ExportPhotosWithWorkflowURLMethod, that’s what it gets called when the user selects an item from the pull-down menu. And currently all it’s doing is getting the selected photos out of the image browser view above.

What we want to do is feed these photos into our workflow. So with one line of code, which you’ve never seen before, I’ll just paste that in and that’s all we have to do to run these workflows. This one simple method on AMWorkFlow. Now, you can see I’m actually not paying any attention to the output of this method. My application doesn’t need to do anything with it. If yours does, you know, you’ll get the output back and you can do something cool with it. And I’m also not providing an error parameter here.

Now, I’m being a little bit lazy. In a real shipping application, you’ll want to prove an error parameter to catch the error and display an alert. This works just like in most other Cocoa APIs that have error parameters. So at this point I’m basically done. I’m going to clean my project. And now I’ll click the hammer icon up here to Build and Run, so this is going to take a few seconds.

I should have switched to Clang but I didn’t. That would have been much faster. OK. And there’s my application. Now I can hide Xcode. I can select a photo. And I can bring down my pull-down menu and just choose TweetPhoto, and you should see exactly the same thing you saw when Emilie ran the service.

But this time it’s running in my own application and I can just say, This photo is really cool. Whoops. If I could type. And post it up to our Twitter feed and there it is. And here’s the link. And just like that, in one click I can upload a photo to Twitter out of my application.

[ Clapping ]

Now, some of you in the audience are probably thinking, OK, you know, that’s kind of cool. But I could have just written code to do that. Twitter’s API is really simple, you know. Let me bring up this menu again. Address Book is scriptable. iPhoto is scriptable. I could have just hard coded this into my application.

But before you go down that path, what we’d really like you to think about is, you know, what you’re signing yourself up for. If you wanted your app to support the ten social networking sites that have undoubtedly launched since the beginning of this demo, you would have to release an update every day. And, you know, that’s going to be a lot of work. You’re going to have a lot of lines of code to write. You’re going to have a lot more code to maintain.

A lot more bugs to fix. Longer development cycles. You’ll have trouble keeping up with what your customers want. And AMWorkFlow can help you solve all these problems, so let me show you how real quick here. If I was a customer of this application and I liked to use Flickr to share my photos online, I wouldn’t be able to do it with this.

And I’d probably want to switch to a different application that did support it. However, since this application supports AMWorkFlow and there are Flickr actions available for Automator, I can just download those actions, make my own workflow, import it into this app, and I’ve just added the feature I need.

So let me show you what that might look like. I’ve got a workflow ready-made here that does exactly what I want. Now, I’m a little bit weird. I like to upload photos with sepia filters and big black border around them, and then tag them with a couple of things when I upload them to Flickr.

So as you might imagine, you as an application developer you might have had a really hard time, you know, expecting that your customers might want to do this. But Automator makes it all possible. So I’ve got this workflow ready. All I have to do is go to my app here. We’ve added a little ImportWorkflow menu item. I can choose that and navigate to my workflow.

And now, I’ll click ImportWorkflow. All this is doing is copying the workflow into a folder that my app is monitoring. It’s not really doing anything special. Now I can choose my photo again. If I go the pull-down menu, my workflow is there, so I’ve instantly added the feature I need. I can select it.

The workflow will run. You’ll see the gear menu up in the menu bar. And Flickr will open up and there’s a toenail of the image I’ve just uploaded. There’s a thumbnail of the image I’ve just uploaded and I can edit the description and click OK, and now it’s in my photo stream on Flickr. So just like that, I can add a feature to the application that you’ve shipped.

And this is going to be really powerful and make your customers very loyal to your app. So that’s it for AMWorkFlow. Now, I’ll invite Emilie back up onto the stage to talk about how to write Automator Actions.

Thanks, Itai. What Itai showed you in the demo today was how you can run workflows from your application using one line of code using the Automator framework.

And you saw how this could be really useful for your application and allow your users to decide exactly how your application is going to work for them, making them happy customers. So now that we’ve talked about how you can take the power of Automator and Automator workflows and put those into your application, now let’s take a look at how you can take your application’s functionality, and by creating Automator Actions, put your application into Automator, which then can put your application into the rest of the system.

So now, let’s talk about creating Automator Actions. You saw in the first demo how just with a few Twitter actions I had the functionality of Twitter all over the system. We had it in TextEdit. We had it in Safari. We had it in the Finder, and just because Twitter had a couple Automator Actions.

Now there are many third party applications that also ship with Automator Actions, and they too can be put into the rest of the system. So wouldn’t it be great if your application also had Automator Actions and could go into the system just like everybody else, making your functionality essential to everyone? So how do you do this? Using the same great tools that you use to create Desktop applications as well as iPhone applications.

Xcode and Interface Builder. And for just a small fee, I too will plug your application into my next slides. So using Xcode, if you go into the New Project Chooser, you can select Application Plug-in on the left side of the screen and then you’ll see Automator Action and you’ll select that.

And you’ll be presented with an Xcode project that looks very similar to any other Xcode project complete with source files, a NIB file, and your Info.plist. These correspond to the Automator Action components of source files, actions user interface, and action properties. And we’re going to talk about each one of these components in more detail now. So first, let’s talk about the source files. Automator Actions can be written in three different ways.

The first of these is AppleScript. Now, I want to take a little moment here and just get rid of the common misconception that Automator is simply a front end for AppleScript. This is not true at all. Like I just said, you can create Automator Actions in three different ways, and AppleScript was just one of those ways.

So for any of you who previously thought that, this untrue. If you were to write an AppleScript, action on Leopard this is what it would have looked like. And in Snow Leopard, we’ve actually deprecated AppleScript Studio in favor of AppleScriptObjC, which looks more like this. And if you were at Chris Nevell’s session earlier this morning, or you can watch it later, you can look at the AppleScript in the Cocoa Bridge section to learn more about AppleScriptObjC and how you can write Automator Actions with this. The second kind of Automator Action you can create is with any scripting language of your choice. Whether you want to write an Automator Action in Shell, PERL. I don’t know. Python, Ruby, Tickle. Whatever you want to do, and I did verify the Tickle action.

You can create any Automator Action in any scripting language that you want. The third kind of Automator Action that you can create is in Objective-C. And there are a few caveats you’ll want to know. If you have any existing actions written in Objective-C you’ll want to make sure that you change them so that they build 64-bit ANGC. They are not going to load in Automator if you don’t do this, so you want to make sure that you convert any existing actions you have. Additionally, we’ve added new APIs to AMAction to determine its progress as an action is running in Automator.

So if I can direct your attention to the lower left-hand corner of this action UI, you’ll notice that there is now going to be a progress indicator that shows up if you implement this API while your action is processing data. And this is. Provides a better user experience for your users because they can see how the action is running. So, and that’s pretty easy to do.

It’s just ProgressValue and SetProgressValue in AMAction. So now that we’ve talked about the source files of an Automator Action, let’s take a look at the user interface. Using Interface Builder, which is the same application you’d use to create any other user interface on Mac OS X or the iPhone, you just simply drag in the elements, or the controls, into the view. And then you hook them up to your code using bindings.

It’s really important that you create great user interfaces for your actions. Your users are, first of all, going to be able to need to use this action. And if you find that this action’s becoming too cluttered, maybe you need to separate the functionality of this action into smaller actions so your users can use them more easily. We’re going to go over a few design guidelines very quickly. First, keep your actions short and simple.

Automator stacks things vertically in the Automator workflow view, and so you want to make sure, that your user can see as many actions as they want to. You want to use NSSmallControlSize for all of your actions in line with following with making just smaller, tighter UIs. You also want to keep 10px borders around your action. And aside from that, then follow all the rest of the standard Aqua [assumed spelling] guidelines. And then you’ll want to be sure that you give the user visual feedback.

For example, if you had an action that prepended text to the beginning of a file name, you want to make sure that then you show an example of what that might look like in your action’s UI for the user before they even run this action to find out what’s going on. There’s more design guidelines in the Automator Programming Guide, which you all should know URL for now, and I highly encourage you to read that. So, now that we’ve talked about the source code and the user interface of an action, let’s take a look at the action properties.

Action properties are really important because they help define more aspects of your action’s functionality with respect to the rest of Automator as well as the right user-visible polish such as how your action shows up in the Library, or what kind of name or icon your action has. So first, let’s take a look at parameters. If you recall, like I mentioned on the Interface Builder part of the talk, you hook up your UI controls to your code using bindings.

And you’ll want to define your parameters in the Actions Target Inspector, and so that’ll be how you talk to your UI elements in your Actions User Interface. Input and output types are also very important because as you can see, if you recall the UNIX pipes metaphor, it’s like the input and the output of your action. And so that’s how your action will know what kind of data it can receive and what kind of data it’s going to output to the next action.

So you want to be sure you defined your input and outputs types properly, which we’ve also documented in the Automator Programming Guide. You can define category and application that your action shows up in. This defines where it shows up in the Library. It would be a tremendous waste for you to create an Awesome Automator Action, and then it turns out your users can’t even find it because it’s stuck in the other category and it has a pipe icon and who knows what.

So you want to make sure that you’ve defined a good category, application, and its name for your action as well as icon. And a good description. So once your user finds your action they’ll know that this is exactly the action that they want to complete their workflow. Additionally, you’ll also want to define some keywords, of course, so people can search because the new cool thing these days. So there’s also many more action properties you’ll want to look into setting. Again, Automator Programming Guide. And so with that, we’ve talked about the source code of an Automator Action.

We’ve talked about the user interface for an Automator Action, and setting action properties to make sure that your action plays well with others and looks great in the Library. So that was kind of a lot of how you can create an Automator Action, so I’m going to invite Itai back up onto stage and he’s going to show you how you can create an Automator Action from scratch. He’s going to show you how you can create the project in Xcode.

How you can add source files to the projects as well as design a great user interface. He’s then going to walk you through how you can set some of the action properties, and then show you how you can build, run, and deploy this action, install it. And then, once you’ve done all that, it’s much easier than it sounds. I promise. Then how you can take that action and put it in any Automator workflow.

Therefore, putting it into the rest of the system. So, Itai.

[ Clapping ]

OK. So let’s go back to the demo machine here. Now, hopefully you all liked that Flickr action I showed you earlier. I didn’t actually mention that it was a custom action, but it is, and I’m going to show you exactly how to write it from scratch right now. But before I start, just to prove that there’s no sleight of hand here I’m going to delete the existing action. And I’m going to empty the Trash.

OK. So we’re going to start from scratch. Close that project. I’ll create a new project in Xcode. This is the Template Chooser that Emilie already showed you. In this case, this is a Cocoa action, so I’m just going to chose Cocoa and hit the Choose button. It’s going to ask me what I want to call my action, and it’s called UploadPhotostoFlickr, and now I can just click Save. And this is going to create an Xcode project just like the standard Cocoa application projects that you’re all probably used to.

You can see there’s a bunch of source files in here and resource files. That’s all what you’d expect. Now, what we’re going to start out doing is modifying the source code for our action. You can see here you got a basic template with, you know, the one method that you need to override at the very least.

So I’m just going to paste in some code. Now, I’m not going to talk about this code in detail. This is a lot of Objective-C code that interacts with the Flickr API. The only thing I want to emphasize here is that while we all love AppleScript, you’ll note this is Objective-C code. And Objective-C is a first-class citizen in Automator. You can write Automator Actions that can do pretty much anything that you could do from Objective-C. And in this case even interact with the web application that has a RESTful web API.

It’s not even scriptable or any of that. OK. Next, I’m going to go my RunWithInputMethod and I’ll just fill in the body of my action. This is the code that’s actually going to iterate over the items that are input to the action, upload them to Flickr and get back an ID and store that away. And then the last thing, we degenerate the output of our action. And in this case, Flickr gives you this nice URL that you can direct your users to. It shows you all the photos you uploaded.

If you recall, previously that was the page that had all the thumbnails that I had uploaded from my, from Picture Booth. OK. So that’s most of the code that I need to write for my action. One last thing is to use this new Progress API that Emilie told you about, so I’ll add a couple lines to do that. It’s very simple. Basically, on each iteration through the loop, we’ll just increment the progress value, and that’s all you have to do. So that’s it for the source code.

I’m going to save that, and now I’m going to open up my Actions nib. I’ll hide Xcode. And now we can work on the user interface for our action. So you can see the action setup in Interface Builder just like any Cocoa application would be. You get a default view.

And if you recall the UI from the Flickr action that I showed earlier, it just had a little text label called Tag, so I’ll create that. I’ll set that aside for now. Another thing that you might have had trouble noticing from the workflow I showed you before, is that the text field inside this action is actually kind of a special text field. It had this purple lozenge thing inside of it, and that’s called a Variable.

Variables are a new feature we added to Automator and Leopard. And if you want to support Variables in your action, then you need to use a special type of text field. It’s not just an NStext field. It’s called an AMTokenField and it’s available right in Interface Builder’s library.

You can just drag it right out to your action. And, eh. Line it up using the interface guides in Interface Builder. OK. Now, there’s just a couple more things that we need to do before we’re done with this. As you’ll recall from Emilie’s slides, you really want to conserve space inside of your action, so it’s very important that you follow the guidelines and only have 10px borders around your action’s user interface. Now, the easiest way to do this I found is to select everything in your action’s UI.

Hold down the Option key, and then Interface Builder’s going to give you some really handy guides here that show you the distance from all of the borders of your view. And now you can just use. That was very fast. You could just use the Arrow keys to nudge your UI over to where it belongs. And I’ll do this very carefully, because if I get it wrong Emilie will be very upset.

Some people take this very seriously. So that’s it. My UI’s almost done. There’s just one other thing I have to do. This parameter right here that users can type into a text field, my source code in my action needs to know how to access that somehow. And typically, that’s done via Cocoa Bindings technology now.

At this year’s WWDC, there were no Cocoa Binding sessions, but there are sessions available on iTunes from past years. And there’s plenty of documentation available online if you’re not familiar with this technology. But if you’re not familiar, you can just follow along with what I’m doing because this is going to be a really straightforward binding. If you go in here to the TokenFieldBindings, and I’ll zoom in because it’s kind of small, and Interface Builder’s Inspector, it’s the fourth tab over.

Choose Value. I’m going to bind the value of this field to the parameters dictionary that’s inside of my, inside of every action. And all I have to do is type in the key path I’m going to use to access that value. In this case, it’s Tags. I’ll hit the Enter key, and I want the value to update continuously so that my action gets the newest value at all times. Now I can zoom back out. My UI is done, so I’m completely done with Interface Builder.

I can close it and return to Xcode. Now, our source code is done. Our UI is done. There’s just one other little task that we have to take care of before we can, you know, start using our action. And that’s setting the action’s properties. If you expand this target disclosure over here on the left in Xcode, you’ll see the UploadPhotostoFlickrTarget.

You can select that and hit the Info button up in the Toolbar and that’s going to bring up the standard Xcode BuildPropertiesInspector. For Automator Action targets you’ll see there, a new tab that you might not be familiar with here called Properties. And this is where you can set a whole ton of properties on your action.

We’re not going to go through all of them. Just some of the most important ones. So first of all, in this General tab here, you’re going to want to assign a category to your action, as Emilie said. This is what’s going to help users find your action in the Library. Now, these categories are just standardized strings, which you can find in the Automator Programming Guide. In this case, this is basically a web-based action, and I’m going to put it in the Internet category.

I can specify an icon. I’m just going to give it Safari’s icon which sort of our default web-based action icon. That’s all I’m going to do there. Now, when I bring up this pop-up, there’s a whole bunch of other options available. We’re going to go through some of the more important ones here.

The input, this is a UTI type. By default, it’s set to com.apple.cocoa.path. This represents the type of data you want your action to receive. Automator’s going to try to convert whatever is getting past your action into this type. Now, since this action takes image paths as input, I can just leave that as is and move on. For the output, however, you’ll recall that we’re outputting a URL that points to Flickr. So we want Automator to know how to use that.

We’re going to change this to Cocoa URL. And the last thing I’m going to do right here is actually specify my action’s parameters. These are sort of the basic things that you at least have to do before you can build your action. So I’m going to add a parameter to this, and now I just name it Tags, as you’ll recall, from the user interface. I can choose different types of parameters. In this case, it’s just a string. And I can also specify Default Value. I’m just going to leave that blank. And now I can close the Inspector. I’m all done with it and my action’s actually ready to build and run.

Now, when I click BuildandRun in Xcode it’s going to build my action just as it would do with any project. But it’s also going to then launch Automator, if I choose Workflow and go to the Internet category, you’ll see there’s my action. It’s going to get loaded automatically for me so I can test it and debug it very easily. Now, if I expand this description here you’ll see. Maybe zoom in on it. You’ll see there’s a lot of boilerplate text in there.

That’s the other stuff you could have filled out in the ActionPropertyInspector and we really recommend you do that before you shape your actions, because it helps your users learn how to use your action. And then it helps them to know what to expect when they run your action. But you don’t have to do it just to test. So I’m just going to test my action out.

I’m going to drag it over into the Workflow. I’ll add a tag and it’s just Tag. Flickr. This is the tag my photos will be tagged with. Now, I also need to provide some photos to this, so I’ll just hide these for now. I’ve got some photos here on my Desktop. Done with two. Emilie really likes to eat, so I want to share these photos with all my friends. Ah. I didn’t want to do that.

OK. Go back to Automator. All I have to do is drag these photos onto the WorkflowView and they’re going to get added to Automator as a new action. Let me just clear those out, from the Desktop. This action just returns the paths to the items that are stored in it. So that tests the input to my actions. It’s going to get some photos.

I also want to test the output, so I want to see what it returns. And since I’m getting the URL back, I’m going to add this DisplayWebPages action that you saw earlier. It’s going to open up Safari with the URL that my action generates. And now I’m actually ready to run and test this.

However, I want you to keep your eye, I’m going to zoom in, on the bottom left corner of this UploadPhotostoFlickr action. What you’re going to see is a little bit different from what you might be used to. You’re going to see the progress indicator that Emilie showed you earlier. And if I run this that was really fast.

[ Laughter ]

Oh, yes. I deleted them. That’s what happens when things go wrong. I’ll put those back on my Desktop. Add them in there. OK. Let’s try it again. Now, you’ll see it starts out at zero. This is going to take a little while because it’s uploading images and they’re somewhat large.

But you should see it move in one-third chunks as it uploads each of the three images. So now, your users can actually see what your action is doing. It doesn’t look like it’s, you know, just sitting there. They can see how far the progress has moved along. And Flickr opens up and there are the photos I want to share. Just like that. So our action seems to work.

There’s my photo screen.

[ Clapping ]

OK. That’s great. But the next thing you have to do if you’re an application developer is figure out how you want to deploy your action. Now, Emilie didn’t really mention this. But what you need to do basically, I’ll go over it in a nutshell, is if you are shipping a standard bundled application, what you’ll probably want to do is put your actions in a folder called /Library/Automator inside of your bundle.

And Automator’s automatically going to find them in there. Another thing you can do is install your actions into either the /Library/Automator folder on the system, then they’ll be available to every user. Or you can put your actions in the ~/Library/Automator folder. And Automator’s going to find them in both of those locations.

I’ll show that to you real quick here. So first, let me go back to my project. If I’m going to deploy this, I need to go up to the Toolbar and choose Release. I want to build in the Release configuration and that way, you know, everything’s set up for deployment properly when I build my action. And I’ll rebuild. This takes a little bit longer because it’s doing a little more work. OK. Build succeeded.

I can get out of Xcode. Now, I’m going to open up my ~/Library/Automator folder, which is my personal Automator Action folder. You’ll see there’s some stuff in there already. And I’ll go to my BuildResults. Here’s the action I just built. I’m going to drag it into my Automator folder, and that’s all I have to do to deploy it on my own personal system. I can launch Automator now. Go to the Internet category, and the action is still there.

That’s the action I just built in Release mode in Xcode. Now, finally, just to put this all together in one, you know, big grand finale, we’re going to combine a lot of the concepts that we used throughout this session. I’m going to create a new service. The service is going to take image files, and what it’s going to do is very similar actually to the service Emilie showed you earlier. But it’s going to upload the selected photos to Flickr, and then share that Flickr URL over Twitter.

So we’re combining a lot of things together here. OK. So this workflow’s going to be. We’re going to start out with uploading photos to Flickr. We’re going to tag them with Service so we know our service uploaded them for us. Now, we’re going to ask for TweetText so we can, you know, provide some comments that we want to upload to Twitter.

We can update our Twitter status. OK. And now I also want to see my Twitter feed after I’m done uploading this. So I’m going to add a couple more actions to do that. This is very, again, very similar to the service that Emilie showed you before. I’ll save it, and this time it’s going to be called UploadPhotostoFlickrandTweet. Kind of a long name. Now, I can quit Automator. I can go to some other photos I have here. And just show I’m an equal opportunity embarrasser here. These are some photos Emilie took of me in my office.

And, eh. If I look in the Context menu there is the service I just created. If I right-click on my photo I can upload it right to Flickr. Zoom back out again. You’ll see the gear menu show up. The photo’s being uploaded to Flickr. There’s the URL. You know, I can type in my comment. This photo is. This photo. I’m not very good at typing if you haven’t noticed.

Is really. Hmm. Not funny. And post. And there’s my Twitter feed with the comment with the link to Flickr, and everything’s done with one click through the power of Services.

[ Clapping ]

So that’s how you can create an Automator Action. Hopefully it looked really easy because it really is, and we want you guys to ship lots of actions with your apps.

And now I’ll invite Emilie back up onto the stage to wrap us up.

[ Silence ]

Thanks, Itai. I liked how he thought I’d be upset that he wouldn’t have 10px borders around his images. Yet, he didn’t think I’d be upset by the mortifying photos he just used of me in that demo. I like food. It’s very delicious.

So what you just learned in that demo, Itai showed you how you can take Xcode and create an action project in Xcode. And then how you can add source code to your action. Create a great user interface for your action as well as setting action properties. He then showed you how you can build, run, and deploy your action.

And how then you can take that Automator Action that you just created and insert it into the rest of the system using the power of Automator. So we went over a lot today. First, we learned about the power of Automator and what you can do with Automator, especially in Snow Leopard now that you can create Services using Automator workflows.

You then saw how you could take that power of Automator and put it into your application using AMWorkFlow writing just one line of code. You can extend the functionality of your application and allow your users to do whatever they want to do with your application and allowing them to focus on what’s most important to them.

Then we took your application. And if you create Automator Actions for your application, you can then put your action’s functions, your application’s functionality into Automator, and thus distributing your applications functionality to the rest of the system. Now, for more information you’ll want to talk to Matt Drance who’s the Application Frameworks Evangelist. You can also email the Automator Developer’s Mailing List. And as usual you’re going to read the what? Automator Programming Guide.