Leopard Innovations • 37:29
Automator empowers users to create extraordinary application workflows. Learn how to create Automator actions for your application and find out how the new technologies in Leopard allow you to define variables for more flexible workflows, provide workflow editing from your own UI, and more. Discover the power of automation and create new possibilities for users of any skill level.
Speakers: Kerry Hazelgren, Emilie Kim
Unlisted on Apple Developer site
Transcript
This transcript has potential transcription errors. We are working on an improved version.
I'm Kerry Hazelgren and I am the Automator Engineering Manager and this is embracing Automator workflows with your application. I would like to start out today by introducing some new features that we've added to Automator for Leopard and then we'll go into the real session or topic of our session, which is how you can embrace, or integrate Automator into your application.
So for our, for Leopard we have a new user interface, definitely some similarities with the tiger user interface. But we've sort of refreshed it and brought it forward to Leopard. We've also added a few niceties and some tweaks like having the log integrated into the main window, instead of in a drawer and things like that.
We also have a new mechanism for organizing actions, which is by category rather than by application. We feel this is more natural way to organize actions representing an easier right, or a better flow for how you want to accomplish your tasks. We also have groups. Now what are you going to put in those groups? Actions, we got a way that if you have a workflow or an action in your workflow and you configure it, set it up how you want it, you can then drag it to the library, into a group that you created and that's now part of your library and you can use it whenever you want.
We also have smart folders, we've, we ship with three default smart folders but you can also create more as specifies criteria for them and they'll always be up to date and however you want them. So we also have an integrated results view in every action. This should make debugging a little easier. As you can see once the workflow is run you can look at the results for any action and see what they were, see if the action actually ran properly or what.
We have over 30new actions. RSS is one big set of new actions we have to deal with RSS feeds. We also have some Quartz Composer actions, and a Take Video Snapshot action which we'll feature later on. There's a built in media browser. A lot of workflows deal with media such as iTunes or iPhoto or Aperture media. So it's really nice to be able to quickly get at that media and add that data to your workflow.
We also have recording feature Now this will let you click the record button in the toolbar and Automator will start recording your actions and when you're done it will add a Watch Me Do action to you workflow. So whenever that Watch Me Do action runs it will replay the steps that you recorded. This is really handy for automating things that normally would be difficult to automate. Such as if you are working with a non scriptable application for example.
One of the biggest new features that we have are workflow variables and this lets the user customize how the workflow will behave when it's done. And the user can create variables of their own or choose form a set of default variables that we ship with Automator. You can also use variables to sort of store the output of an action to a variable and then perform some other operations and then later on read that data back in from the variables. So it can act as sort of a store and retrieve mechanism for your workflow data. As we'll also talk about later you can programmatically set and get the values of those variables as, through API's.
Now we have starting points and this is, this starting points is a sheet that comes down when you open up a new document and the design, the purpose is really to let you quickly get started building your workflow. So by making a few choices and clicking the return button Automator will add a couple, one or two actions that will hopefully provide your workflow with the data that you are going to be working with.
And then now we come to new public APIs that we have added to the Automator Framework that will be the main topic of our session today. That basically with a workflow and your application you can create an application that has more features then it would otherwise, you know you can quickly add features to you application so to talk more about that I'd like to introduce Emily Kim who is on the Automator team.
( Applause )
Emily Kim: Hi I'm Emily and today I am going to talk to you about adding workflow support to your application. Let's take a quick review of Automator. Automator is an application used to create automated workflows. If there is something you do repeatedly, you can create an automated workflow to do this process for you, great. Workflows are made of one or more actions. Actions are bundles of code that are designed to do pretty much one particular task and then you can put a UI on top of it and configure that tasks behavior.
Once you've created your workflow you can run this workflow from a variety of places. You can run it from within Automator, as a stand alone application or as a plug in for something like iCal or Finder. And now in Leopard with the new APIs we are going to provide you, you can now run workflows in you application. So this is great, you can have all the power of Automator in your application. Now some of you might be wondering well if Automator is for automating things there's nothing in my application that I need to automate, you know what am I getting out of this session.
Well I'm getting there don't worry. If any of you were paying attention in Betrand's State of the Union on Monday you would notice that he said, he said two key phrases that I want to emphasize. The first is Encapsulation and Reuse. The second is Building Fast. Now with Automator in Leopard you can do both of these things.
Automator actions are in the library and essentially they are just pieces of code that other people have written for you in a tiny little bundle, you drag and drop it into Automator. I don't know how much more Encapsulation and Reuse that you can get then that, I mean really.
And then when you come to Building Fast, all you do is add these actions to you workflow and you've got a workflow that does something for you just from drag and drop. If you can put the functionality of this workflow in your application you barely even have to write any code to do it, it was drag and drop and suddenly you have a new feature in your application. If that's not Building Fast, I mean I don't know what is. So with that in mind workflows can sort of act as plug-ins for your application. And then Automator essentially becomes your plug-in development environment, there by becoming the new Xcode. Note the sweet icon.
So let's get you a concrete example of how you might be able to use this technology yourself. Say you're a developer for a photo application, something kind of like iPhoto where you can see your pictures and they look great and then one day your boss comes to you and he says you know everyone's really excited about those new iSight cameras on their computers. Let's have a feature that uses those, so you got to say to yourself okay then that sounds like a good idea, iSight are fun.
But how do you go about adding this feature to your application? Some people may know, some people may not and even if you do kind of have an idea you still have to do the research and figure out which framework that you need to pull in and then how those API's work and then do you have to do your own UI and then how do you debug it and you know it's a lot of work so even if you knew where to start from you would still have a lot of code to write.
So now picture for a moment, you can go into Automator, take out that new Take Video Snapshot action that Kerry mentioned earlier add it to your workflow, add that workflow to your application and suddenly that's the feature that your boss asked for. Sounds crazy, maybe, but lets bring Kerry up on stage and he'll show you how it's done.
( Applause )
[Kerry Hazelgren]
Not that crazy. Okay so here I have a sample application that I wrote and it's really great. It's called photo browser, it displays photos but it really doesn't really do much more then that. So as Emily said, my boss asked me to create a function or a feature that takes a photo. So how am I going to do that? Well I'm going to start by creating an Automator workflow and here's my starting points dialog that I talked about earlier.
I am just going to start with an empty, an empty workflow so I am just going to choose the custom selection. Now if I go to the photo's category I can see all of my photo related actions and down here at the bottom there is a Take Video Snapshot action and that's the one that I want. So I drag it to my workflow.
I have a couple of properties that I can choose from, I'm going to save it to the pictures folder, when the snapshot is taken, then I am going to leave the rest of it alone because I don't want to take it automatically, I want the user to take it. So that's my workflow and I am just gonna save it like that. Oh mistype. Save it again. I am going to call it Take Photo's.
I meant to save it to the desktop, oh well. So there we have my workflow. Now if I go and I am going to look that up again, there it is. I am going to drag and add that workflow to my Xcode project. So I am just going to drag it over here to this workflows folder that I created.
Now anyone that used Xcode has seen this dialog before. The one thing you have to know about adding a workflow to your project is that this setting right here that say recursively create groups for any added folders. A workflow is actually a bundle and if you add a bundle to your Xcode project and you have this set, it won't work properly.
The idea is that when your project is build and your application is created it will bundle the workflow inside of your applications bundles so you need that bundle to be intact so we are just going to change this to create folder references. Trust me it won't work unless you do that. And even in Xcode it doesn't look quite like a workflow, it looks like a folder but it will work. Now we have our workflow in our Xcode project.
I previously wired up the toolbar item that you saw, that when it is pressed it will call this Take Photo action, the Take Photo method I should say. So let's look at the code that we are going to run. And the user clicks the toolbar button. First we are going to start by getting a path to the workflow that we want to run, and it's in our main bundles that so we just call path for resource, take photo of type workflow.
Sorry, oh did I name it wrong? Alright and once, well I would know, I would find out later but thank you for saving me the time. Once I have the workflow object I can just call this AM workflow class method, its one line and what it will do is load the workflow and execute it and return the result back to my application.
So I call run Work flow at URL and I pass a URL that we create from the path with input I'm not going to pass any input this time so I just passed nil and then there's an error perimeter that Automator can let me know if something went wrong wither loading the workflow or executing it.
So if I have a result and there is no error then I know that it executed properly and I have a result. Now in this case the result is an array of one item and that item is a path to the picture that was taken. I know that because that's the output of that one action. But this can change depending on whatever the action, the last action in the output returns. It could be Cocoa strings or it could be AppleEvent Descriptors or what have you.
So I just call this method that I wrote in my app, that doesn't really concern our demo it's just going to take, it's just going to add those items to the photo browser object. So let me build and run this. And here we have our app again and if I click this it should take a picture and I am sorry the pictures of me, but here we have it and if I click return, there it is. I get the result in my workflow.
( Applause )
So that was really easy. It's a simple one action workflow but obviously you can add, you know you can have a workflow with however many actions in it and still for the developer you only have to do a couple of lines of code so its scales really well. And so to continue and talk about what we learned, Emily.
(Applause 0:14:35 to 0:14:39)
Emily: Slides please. Okay. So what did you learn in this demo? Kerry showed you how you can create an Automator workflow and add an action to it then once you added the workflow to your applications Xcode project you can and add a couple lines of code you've pretty much added a brand new feature to your application that your boss is thrilled with and it was so easy that even my manager could do it. So clearly this is great technology.
But this is really the tip of the ice burg. This was just one line and workflow class method and you can run this workflow. But what if you wanted to set the input of the workflow if maybe get at the actions in the workflow. We need something more powerful but before we get to into that let's take a step back and look at the big picture. This is Starry Night by Vincent van Gogh, painted in 1889 and when he was still pretty insane. Now you might be wondering what does Starry Night have to do with Automator. I'm also getting there trust me.
Lets say somebody gave you this a blank canvas and a paint brush and they said paint Starry Night. I'm no artist and most of you are probably not artist either and even those of us who are artist, it would probably take us years to try to recreate van Gogh's master piece and even when we were done it probably would look nothing like Starry Night anyway.
But then lets say someone gave you a paint by number version of Starry Night all you have to do is connect the numbers on the colors, how hard can that be and suddenly even like you or I could create Starry Night. Now imagine that your application is like that blank canvas, you want to add features to your application, you want it to be great but your just maybe not quite sure how to do it or it might take you a really long time, you just don't have the resources.
Now what if an Automator was like your paint by number version of your application, then all you'd have to do is create the workflows with drag and drop and then stick them in your application and suddenly you got fully a featured application that you developed quickly and easily. Either that of you have Starry Night, one or the other so both are great, so to tell you more about this technology we're going to talk about the Automator Framework now. And the three workflow classes we've made public for you in Leopard.
The first on is Automator workflow, this is the model object for workflows. The second is AM workflow controller. And the third is AM workflow view, big surprise. So these three classes follow the model controller design pyridine and are meant to be implemented directly so don't sub class these. But first let's go more into detail on AM workflow. Taking my water break.
I'm building up the anticipation. So AM workflow is the workflow model object. You can load this workflow from disc and then once you have the workflow you can access the actions in it or you can set the input for the workflow or get and set variable values. So how do we allocate an AM workflow object? First you have some NSURL that points to a workflow in disc and then you allocate the object by calling in it with contents of URL. And you also have an error parameter so for some reason the workflow isn't created you can check that.
Now what we do once we have this workflow? Well we can look at all the actions in the workflow, that's great. Once you have this you can also add actions or remove actions as well as insert them or move them around. So basically you can do anything programmatically with the workflow and its actions.
You can also set the input of the workflow so if you have the workflow in your application and you want it to interact more directly with your application you can for example pass it, call set input on the workflow and pass in objects from and Automator will work with them.
Kerry also mentioned that a new feature for Automator in Leopard called variables. You can programmatically get and set variables by calling value for variable with name and then passing it the name of the variable that you are interested in. or you can also call set value for variable of name giving it the values you want the variable to have.
So now that we talked about AM workflow you might be wondering well you know I still have all these things and I want to run the workflow, so we don't have, so you saw already that you can run a workflow using the AM workflow class method but to run a workflow object you need AM workflow controller. So that's what we are going to talk about now, how to run you workflow using AM workflow controller.
The controller is the controller objects for workflows and this is how you ruin an AM workflow object. You can also set a delegate on the controller and implement delegate methods there which we'll talk about in a bit. But first running the workflow, we have AM workflow that we created before and we've done whatever it is that we wanted to do to it, so we're set, we want to run this thing so now we have, we create a controller which can also be found in the Automator pallet in interface builder three. Then you simple call set workflow on the controller, and you give it the workflow object and then you call run, that s easy. It may not be one line of code but its still now just four, so it's really not a big deal.
You can also set a delegate on the controller to receive call backs for status updates when the workflow is running. So to do that you simply call set delegate on the controller and you give it the delegate. You can implement several delegate methods including when the workflow will run or stop as well as when actions will run or stop and if the workflow fails. And for example, to something useful you could do with this is you could update the UI in your application and give user feedback on the status of the workflow while it's running.
So we've talked about AM workflow and we've talked about AM workflow controller. But talk is cheap. Let's see this thing in action. So I am going to invite Kerry back up on stage for another demo. He'll show you how you can create a workflow object and how you can set a variable value in the workflow as well as set the input for the workflow so it can interact more with our application and then he run the workflow using AM workflow controller so Kerry.
[Kerry Hazelgren]
Thanks Emily. You heard that so easy my manager can do it thing right? If she's not here next year you'll know why. Okay so in this demo I want to add a new feature to my application, to take whatever photos I select and add them, create an Email message, put them in an Email message so that I can send them to friends and family. I'm going to do it a little differently this time though using AM workflow and AM workflow controller. I already have my workflow here that I am going to run.
It's already in my project, if I just right click and open with Finder, we can open it up in Automator and see what it looks like. So there's three actions, copy Finder items, I want to make a copy of them first because the second action, a scale image is going to you know alter them in a way that is not undoable.
So we are going to copy them and we are going to scale them down to a size that's good for fitting in an Email message and then we're going to create that Email message using the new mail message action which when its past files as input, it adds those to the body of the mail message.
Now there's one more thing that we want to do with this, I want to, I want to create a variable to use as a subject of my Email message, because I want to have it different each time. So if I right click in the subject field, I can see that at the bottom there is a new variable menu item and I am just going to choose that and it's going to create a variable and put it in that field and then pop up this window that lets me edit the variable.
I am going to call it subject and I am not going to give it a value, I am going to do that when the workflow runs, so just erase that and then click done and then my workflow is ready for me to, I'm done with the workflow now. So let me just close it and go back to my project. Now let's look at the code we're going to run when we want to execute the workflow.
So Email photos here is the method that gets called when I got a new toolbar item that you'll see and when the user clicks that its going to call it my Email photos method and again what we are going to do is start by getting a path to the workflow.
Once we have that instead of calling the class method to run and execute it what we're going to do is actually allocate an AM workflow object. What we do is call AM workflow end it with contents or URL, creating a URL form the path to the workflow and then there's also an error parameter.
Now the next thing we are going to do is determine what the subject of our variable, or what the subject is going to be. So we sort of get the paths to the photos that were selected and we see if there's just one and we are going to take the name of that photo, if there's more then one then its going to be the name of the first photo and how many ever more. So when we done all that work to determine what the subject is we are going to set that as the values of the subject variable calling set value for variable with name.
Then we're going to set the input of the workflow to be the path to the photos that we selected and we are going to tell workflow controller here's your workflow by calling set workflow. And finally we're going to run it. So if we build and run our application, again we have our photo browser application and we have an Email toolbar item. So let me select the lady bug and the rhinoceros here, I mean the zebra.
Sorry looks like a rhino doesn't it? And if I click Email then I get an Email message with the two photos that I selected and the subject is ladybug.jpeg and one more. And just for demo purposes lets just select a couple different ones. We can see it adds those and we have a different subject this time.
So this workflow for example just to give you an idea of the time savings here its just a little more complicated then last time cause we were talking about three very different technologies and these actions, one is in the file system interacting and copying files, the second one calls into the image library to scale it, the third one calls some apple script code to tell mail to create a new message and to add those again instead of having to learn those different technologies and implementing them in your application, this workflow a simple drag and drop and again very little code to implement it. So to go over again we will bring back Emily.
( Applause )
Emily: Slides please. Thanks. Thanks Kerry. So what did you learn in the demo? Kerry showed you how you can allocate AM workflow objects and how you can set variable values for this objects as well as set the input for them. And then you saw how you can create AM workflow controller objects and use them to run AM workflows.
So we've talked about AM work flow, and we've talked about AM work flow controller. I am guessing that the next one is going to be AM work flow view. So AM work flow view, this is essentially the view that we have in auto mater, so imagine taking that view with all the actions in it and putting that in your application.
So this is invaluable for a number of reasons, first of all, not only can you and your users view work flows from within your application, no more having to switch applications and going from your app to auto mater and back but in your application you can see these. And not only can you see them but you can edit them.
Let's say for example this gives your users so much more power than they could if you had just created some feature for your application and then shipped it with that. Let's say you really like the color eggplant purple, but someone's like "I don't like that color, I want it to be, you know, tomato red." if you put AM work flow view in your application they can just go to the color well and choose tomato red and be perfectly happy and the best part about that is you didn't even have to write any UI to provide this customization for your user. All you have to do is stick in the work flow view and some develop, action developer has already done the UI for you. All you have to do is put it in your application and let the user go to town.
So how do you get name work flow view in your application, the work flow view is also in the auto mater pallet in interface builder three and so using IB three's new lovely library we have the auto mater pallet in there and the work flow view. So, that's what it looks like, the controller is also in the pallet so you can hook those guys up in interface builder or you can do it programmatically by creating a work flow view and then in the controller called set work flow view with that view. So, seems easy enough so lets see is Kerry can manage this in his next demo.
He's going to show you how you can use AM work flow view and he'll show you that, he'll implement a delegate for the controller and show you how you can update your UI depending on the status of the work flow and then he'll also show you how you can allow users to customize feature of your application. Please don't fire me.
(laughs)
[Kerry Hazelgren]
it should not be this much fun presenting WWC sessions I swear (clears throat) okay, so um, what we want to do is I'm going to show you how to use a AM work flow view in your application. Hello? Yeah can I get a demo machine? Okay actually we're just going to look at the code because we've already wired it up in interface builder and she showed you a screen shot of that um what we're going to do differently here is that in the previous two examples is when a user clicked on one of the tool bar items we loaded the work flow and executed it all at once. This time we're actually we want to pre load the work flow because we want the user to be able to configure it first, and then when they click on the tool bar button it will execute.
So I have this set up add color work flow method that I call from the awake from Nib method in my window controller so this gets run when the window in opened and again I don't think there's really much new here oh well I guess there one line new, we take a work flow path and allocate a work flow object, an AM work flow object then we have a work flow controller, I have a new work flow controller for this method just because I didn't want it to get away, in the way of the other ones but I'm allocating a new work flow controller just using alloc init, there's not special initializer for AM work flow controller.
Then we tell the work flow controller here's your work flow view, the work flow view is initiated, instantiated in the Nib so that's a reference is an I-bar, so the work flow controller's that work flow view with our work flow view we also set the work flow for the work flow controller now they work in harmonies so if you set a work flow view on a controller and you give it a work flow it will automatically load the work flow in the work flow view and you can do those in either order and it'll just automatically load and display the work flow so you don't have to worry about how you display a work flow in a work flow view. Now we're going to set a delegate this time so we can get delegate methods as the workflow runs and I'll show you those.
Now when the user clicks on the tool bar button, it's going to call this add color method and there's not much left to do at this point. We basically just set the input for the workflow with the paths to the selected photos and then we tell the workflow controller to run.
Now the rest of this code here are delegate methods and there's several of them, but they all just use slight variations of the same thing, like when the workflow controller is going to run, just before it starts running, I have a text field and a progress indicator at the bottom of the window and I just want to display progress, so when the workflow's about to run I am just setting up the minimum, the maximum and the starting values of the progress indicator. And then when say down here when the workflow is about to run an action, I just put in the text field that we're running this action and the same when the action finishes running, I say it finished running such and such an action.
Now there's one more thing I do is that when the workflows done I have to get the results. So, this is a little wordy, but basically what I'm doing is getting the out put of the last action in the workflow. And that's essentially the output of the workflow. So I'm asking the controller for it's workflow and then getting the actions array from the workflow, getting the last one and asking for its output. And then I'm adding those items back into my workflow, into my photo browser.
So this is a way that you can add a little more control. If something happens that's wrong you can stop the work flow. You can perform additional setup or follow along if your application needs to. but those delegate methods aren't necessary, they're just for your convenience. Okay. So now lets run building around this workflow or application I should say.
Now if I make this a little bit bigger and expand this, you can see that there's a workflow view already in here. Whoo, now Emily said we want to add a color to it. But we might want to, as the user, I might want to change what color it is. So let me change it to a nice red and I'm going to select a couple of images here and click the add color button. As you can see there's, well it actually allocated pretty quickly, but you can see there's the results.
I'll do it a couple of more times so you can see the progress here at the bottom in the progress indicator. So you can see that it's actually keeping the workflow as I specified, assuming I'm the user, and it's returning the results to my application. So there you have. Now Emily.
( Applause )
Emily: That was unplanned, him doing Tomato Red and Eggplant purple, by the way. So slides, please. Thank you. So what did you see in that demo? You saw how to use an AM workflow view in your application. And you also saw how you can set a delegate on the controller and to implement delegate methods and update your application's UI.
You also saw how you can allow customers, op, this is what you will learn. Same thing. So you did learn all of that. And you also learned how you can allow users to customize your application's features. And in doing so, we learned how to use all three of the workflow classes that we've made public in Leopard.
So, we've now talked about AM workflow and AM workflow controller and AM workflow view. There's only three circles on there. So that's a pretty sure sign that we're close to the end. So don't worry. Let's take another step back and look again at the big picture. What have we learned in this session today? First Kerry told you about some of the new features that we have in Automator in Leopard. We hope you're thrilled to start using these features and we had a fun time making them. So feedback is great.
We also learned how you can make Automator your plug-in development environment. It really doesn't get much easier than just dragging and dropping. And if you just remember that paint by number example that I gave, making applications suddenly becomes really easy. We also learned how you can utilize all three of the new workflow classes that we've added in the Automator framework, and in doing that you can add trivial amounts of code to your application and get non trivial features out of that code.
You've also learned how you can empower the user with customization, with allowing them to customize their experience with your application. So often with rapid application development environments, the programmer just wanted to get the application out the door really quickly and make money. But the user usually ends up having to suffer because of that, because the programmer wasn't really thinking about the user, he just wanted to be done quickly.
However, if you use Automator as your rapid application development environment, not only do you save time, and you still get your application out the door quickly, but the user also benefits because you've given them a workflow view and they can customize how they want that feature to behave. Everyone profits.
So with that, you now know how you can deploy fully featured applications quickly and easily using the three classes in the Automator framework, AM workflow, AM workflow controller and AM workflow view. So with that, I hope you will all go out and embrace Automator workflows with your applications.
( Applause )
- Emily: Thanks. I especially liked the hug gesture. Everyone should hug their neighbor. So for more information, you can contact Matt Drance, our sharing technologies evangelist. And there's also an automated developer's mailing list that you can join and get your questions answered. And for documentation and other sources, you can go to the developer website.