Swift Automator Action Xcode Template

Traditionally, Automator actions are created in Xcode using one of three default action templates: Cocoa Action, Shell Action, or AppleScriptObj-C Action. However, Automator works with any scripting or programming language supported by macOS, even Swift — Apple’s open source programming language for both macOS and iOS.

Available for download here, the “Swift Automator Action Xcode Template” is created for Xcode using the Swift programming language and Apple’s AMWorkflow, AMBundleAction, and AMAction frameworks.

IMPORTANT: This Xcode project template is provided as instructional material and comes with no guarantees, implied or otherwise, as to its usability, functionality, or safety. USE AT YOUR OWN RISK.

To install the template, unpack the ZIP archive and place the folder titled “Automator Action” in the following location:

Home > Library > Developer > Xcode > Templates > Project Templates

If the folder hierarchy does not already exist, add the folders necessary to complete the hierarchy.

The project template folder contains all the files necessary to generate new Xcode Automator Action projects. You do not need to edit these files, leave them as is:

template-folder

Once installed, you can choose the new template from macOS category in the standard Xcode template chooser dialog:

template-chooser

Create a new example Automator Swift action project using the installed template. Once you’ve named and saved the project, it will open in Xcode.

The Xcode Project

The default project file created by the template includes example code for outlets and actions, properties, bindings, localized strings, and action parameters.

As you can see in the illustration below, the Foundation, AppKit, and Automator frameworks are used, as is the OS logging tools for displaying comments in the Console application.

In addition, the project file contains an IBOutlet property that will be used to represent a text field in the action interface.

project-file-01

And further down in the main Swift file is a corresponding IBAction function that will be used to display the contents of the text field linked to the IBOutlet property:

project-file-02
The Action Interface

Activating the example UI elements is a familiar process, in that it requires the same techniques used by standard macOS apps and projects in Xcode.

Select the main.xib file in the files list to display the action interface, and then follow the instructions in this video to connect the example outlet and action:

The example action and outlet are now connected and will be useable once you have compiled and installed the Automator action.

The contents of the text field are represented by a binding to a key placed both within the Parameters object in the nib, and in the project’s Info.plist file.

bindings

Make sure the binding for “Continuously updates value” is selected.

When the workflow containing the action is executed, the value of the contents of this text field will be retrieved in the run handler in the main Swift file, by using the key with the Parameters object.

Install the Action

To build and install the example Automator action, select “Build” from the “Product” menu.

Once the building of the action bundle has completed, Control-click the target bundle in the Products folder in the sidebar, and choose “Show in Finder” from the forthcoming contextual menu. (see below)

show-in-finder

Double-click the icon of the revealed action file, and an installation dialog will appear:

installation-dialog

Click the “Install” button and the action bundle will be placed in the Automator folder in the Home Library folder. It is now installed in the Automator action library. A confirmation dialog will appear:

confirmation-dialog

IMPORTANT: Automator is considered to be a System process. To set up the Xcode project to automatically install and run in Automator, follow these Apple developer instructions, but make sure the “Debug executable” checkbox is not selected. Use print() commands to log to the project console.

Use the Action

In Automator, create a new workflow document, and search the action library for the installed action. Add the action to the workflow by either dragging it from the action list into the workflow area, or by hitting the Return key while the action is selected in the action list:

action-in-workflow

When the action is added to the workflow, the following dialog will appear:

opened-function-called

This dialog was trigger because code for its display is placed within the opened() function in the main Swift file. You can remove the example alert code from within the function.

Automator actions have functions for when actions are added, removed, or updated in workflows. These functions are included and annotated in the main Seift file.

You can test the example outlet and action you previously connected by clicking the “Show Text” button in the action view. The dialog will display the contents of the text field to the right of the button.

interface-test

Use the Action

To view action logs within the Console app when the containing workflow is executed, filter using the Automator process and the name of the action.

console-log

TOP