Automator Module

Overview

PyXA supports all of Automator’s OSA features, including but not limited to creating and executing workflows, managing Automator actions and their settings, and interacting with execution return values. PyXA can create workflows and variables, assign and arrange actions, and modify the attributes thereof. PyXA can also observe the execution of workflow files, allowing you to use existing automation workflows aongside PyXA and Python in general.

Automator Tutorials

There is currently one tutorial for the Automator module:

Automator Examples

The examples below provide an overview of the capabilities of the Automator module.

Example 1 - Creating workflows from scratch

The example below creates a workflow that displays a notification, waits five seconds, then starts the screen saver. The process for creating workflows begins with making a new workflow object, adding it to the list of workflows, and saving it to the disk. Without saving here, you may encounter errors as some methods and actions require access to the workflow file. With an empty workflow created, the next step is to add actions, which is most easily done by name. Next, you must retrieve a mutable form of the actions; you can think of the original ones as a template that you’ve now made a copy of. From there, you can change the value of settings however you desire.

 1import PyXA
 2app = PyXA.Application("Automator")
 3
 4# Create and save a new workflow
 5new_workflow = app.make("workflow", {"name": "New Workflow"})
 6app.workflows().push(new_workflow)
 7new_workflow.save()
 8
 9# Add actions to the workflow
10action1 = app.automator_actions().by_name("Display Notification")
11action2 = app.automator_actions().by_name("Pause")
12action3 = app.automator_actions().by_name("Start Screen Saver")
13app.add(action1, new_workflow)
14app.add(action2, new_workflow)
15app.add(action3, new_workflow)
16
17# Obtain actions in mutable form and change their settings
18actions = new_workflow.automator_actions()
19notification_text = actions[0].settings().by_name("title")
20notification_text.set_property("value", "PyXA Notification")
21
22pause_duration = actions[1].settings().by_name("pauseDuration")
23pause_duration.set_property("value", 5)
24
25# Run the workflow
26new_workflow.execute()

Example 2 - Running existing workflows

In the short example below, we open an existing workflow file, run it, and display the execution’s results.

1import PyXA
2app = PyXA.Application("Automator")
3
4app.open("/Users/exampleuser/Downloads/Example.workflow")
5workflow = app.workflows().by_name("Example.workflow")
6workflow.execute()
7
8print(workflow.execution_result)

Automator Resources

For all classes, methods, and inherited members of the Automator module, see the Automator Module Reference.