Workflow setup and skeleton

Alfred-PyWorkflow is aimed particularly at authors of so-called Script Filters. These are activated by a keyword in Alfred, receive user input and return results to Alfred.

To write a Script Filter with Alfred-PyWorkflow, make sure your Script Filter is set to use /bin/zsh as the Language, and select the following (and only the following) Escaping options:

  • Backquotes

  • Double Quotes

  • Dollars

  • Backslashes

The Script field should contain the following:

/usr/bin/env python3 yourscript.py "{query}"

where yourscript.py is the name of your script [1].

Your workflow should start out like this. This enables Workflow to capture any errors thrown by your scripts:

 1#!/usr/bin/env python3
 2# encoding: utf-8
 3
 4import sys
 5
 6from workflow import Workflow
 7
 8log = None
 9
10
11def main(wf):
12    # The Workflow instance will be passed to the function
13    # you call from `Workflow.run`
14
15    # Your imports here if you want to catch import errors
16    import somemodule
17    import anothermodule
18
19    # Get args from Workflow as normalized Unicode
20    args = wf.args
21
22    # Do stuff here ...
23
24    # Add an item to Alfred feedback
25    wf.add_item('Item title', 'Item subtitle')
26
27    # Send output to Alfred
28    wf.send_feedback()
29
30
31if __name__ == '__main__':
32    wf = Workflow()
33    # Assign Workflow logger to a global variable for convenience
34    log = wf.logger
35    sys.exit(wf.run(main))