Technical Note TN2322

Building a Cocoa-AppleScript (AppleScriptObjC) Automator Action

This document explains how to use the Cocoa-AppleScript (AppleScriptObjC) Xcode template to create an Automator action.

Xcode includes a template that makes it easy to create Cocoa-AppleScript Automator actions. Building an action with this template consists of the following key steps:

To create a Cocoa-AppleScript Automator action Xcode project:
To configure your action’s attributes and behavior:
To create an interface for your action:
To map a parameter to an interface element attribute:
To add code to your action:
To retrieve a parameter value in your AppleScript code:
To test your action with Automator:
To build and install your action:
Document Revision History

To create a Cocoa-AppleScript Automator action Xcode project:

Figure 1  
Figure 1 Art/tn2322_figure_01.pngFigure 1 Art/tn2322_figure_01.png
Figure 2  
Figure 2 Art/tn2322_figure_02.pngFigure 2 Art/tn2322_figure_02.png
Figure 3  
Figure 3 Art/tn2322_figure_03.pngFigure 3 Art/tn2322_figure_03.png

Your project is saved where you specified, and Xcode opens it in a new window. The next step is to configure attributes for your action and its behavior.

To configure your action’s attributes and behavior:

Figure 4  
Figure 4 Art/tn2322_figure_04.pngFigure 4 Art/tn2322_figure_04.png
Figure 5  
Figure 5 Art/tn2322_figure_05.pngFigure 5 Art/tn2322_figure_05.png
Figure 6  
Figure 6 Art/tn2322_figure_06.pngFigure 6 Art/tn2322_figure_06.png
Figure 7  
Figure 7 Art/tn2322_figure_07.png
Figure 8  
Figure 8 Art/tn2322_figure_08.pngFigure 8 Art/tn2322_figure_08.png
Figure 9  
Figure 9 Art/tn2322_figure_09.pngFigure 9 Art/tn2322_figure_09.png
Figure 10  
Figure 10 Art/tn2322_figure_10.pngFigure 10 Art/tn2322_figure_10.png
Figure 11  
Figure 11 Art/tn2322_figure_11.pngFigure 11 Art/tn2322_figure_11.png
Figure 12  
Figure 12 Art/tn2322_figure_12.pngFigure 12 Art/tn2322_figure_12.png
Figure 13  
Figure 13 Art/tn2322_figure_13.pngFigure 13 Art/tn2322_figure_13.png

Not all Automator actions require an interface. Some actions simply receive input, process it, and produce a result. For example, the Eject Disk action that’s included with Automator doesn’t have any configurable settings. It just receives one or more disks as input, ejects them, and moves on to the next action in the workflow. Because there’s nothing to configure, this action doesn’t have an interface.

To create an interface for your action:

Figure 14  
Figure 14 Art/tn2322_figure_14.pngFigure 14 Art/tn2322_figure_14.png
Figure 15  
Figure 15 Art/tn2322_figure_15.pngFigure 15 Art/tn2322_figure_15.png
Figure 16  
Figure 16 Art/tn2322_figure_16.png
Figure 17  
Figure 17 Art/tn2322_figure_17.png

When adding interface elements to an Automator action view, adhere to the following design guidelines:

To map a parameter to an interface element attribute:

Figure 18  
Figure 18 Art/tn2322_figure_18.pngFigure 18 Art/tn2322_figure_18.png

To add code to your action:

Figure 19  
Figure 19 Art/tn2322_figure_19.pngFigure 19 Art/tn2322_figure_19.png

To retrieve a parameter value in your AppleScript code:

Call valueForKey for the parameters method of the action, passing it the name of the parameter you wish to retrieve. For example:

Listing 1  

valueForKey_("myParameter") of parameters() of me
Figure 20  
Figure 20 Art/tn2322_figure_20.pngFigure 20 Art/tn2322_figure_20.png

In order to test an Automator action Xcode project, you need to do two things. First, you need to set Automator as the run executable for the project. Second, you need to provide an argument that tells Automator where to find your action.

To test your action with Automator:

Figure 21  
Figure 21 Art/tn2322_figure_21.pngFigure 21 Art/tn2322_figure_21.png

Listing 2  

-action "$(BUILT_PRODUCTS_DIR)/$(FULL_PRODUCT_NAME)"
Figure 22  
Figure 22 Art/tn2322_figure_22.pngFigure 22 Art/tn2322_figure_22.png

After configuring your Xcode project as described above, choose Product > Run or enter Command-R. An instance of Automator should launch and load your action. You can search the Automator action library to find it. Now, you can test your action and return to Xcode when you’re done to stop testing and resume development.

Figure 23  
Figure 23 Art/tn2322_figure_23.pngFigure 23 Art/tn2322_figure_23.png

To build and install your action:

Figure 24  
Figure 24 Art/tn2322_figure_24.pngFigure 24 Art/tn2322_figure_24.png
Figure 25  
Figure 25 Art/tn2322_figure_25.png
Figure 26  
Figure 26 Art/tn2322_figure_26.png

After you’ve exported your action, it can be installed into one of the following locations:

/Library/Automator/ - Install it here to make it available to all users.

~/Library/Automator/ - Install it here to make it available to the current user.

When Automator launches, it scans these folders and loads any actions that it finds.

If you’re an app developer, you can install the action into a /Contents/Library/Automator/ directory within your app bundle and Automator will find it there, too.



Document Revision History


DateNotes
2015-01-26

New document that explains how to create a Cocoa-AppleScript (AppleScriptObjC) Automator action.