Learning Alfred workflow python sdk.
Install Alfred workflow python sdk first: https://www.deanishe.net/alfred-workflow/installation.html
Add script runner
There are two components in Alfred which can execute a python script: script filter and run script. There are a few differences between script filter and run script, we'll talk about it later.
The usage of both components is similar:
Note that the Alfred workflow python sdk supports only Python2, since the built-in Python interpreter in Mac OS is Python2.
Your first python script
The following is a simple example of Alfred Python script:
|
|
This script passes the input {query}
to downstream. In Alfred, all contents which is printed to stdout will be passed to downstream as {query}
.
Variables
If we want to pass more information to downstream, Alfred variable is what we need.
Set variables
There are two types of variables: general variable and environment variable. Alfred Python sdk provides difference methods to set different variables. What's more, variable setting is different in run script component and script filter component.
Set environment variable
Alfred Python provides set_config
in workflow.util
to set environment variables. Here is the API doc, which is quite easy to understand and use.
|
|
Set variables in script filter component
In the upstream script filter, root variable
and item variable
can be used to pass variables to downstream.
|
|
wf.add_item
adds an item, which is a collection of variables as well as one candidate in alfred window. When the candidate is chosen, the item is actioned.
When an item is actioned, all variables in arg
is passed as {query}
to downstream
title
and subtitle
is the text appears in the alfred window.
Set variables in run script component
In run script component, Variables
should be used to store and pass variables.
Here is an example, we initialize a variable called fail
, and pass it to downstream:
|
|
Get variables
In the downstream script, os.getenv(var_name)
is used to get variables:
|
|
In other components of Alfred, we can use {var:var_name}
.
The following is an example:
If the variable called fail
is empty, the downstream component "open file" will be executed with the input {query}
, where {query}
is the file path.
Logger
In Alfred Python, we cannot use print
to debug because the printed content will be passed to downstream as {query}
. But don't worry, the sdk provdes a logger wf.logger
Logger initialization:
|
|
Then we can use log.error()
, log.warn()
, log.debug()
, log.info()
to debug. All info will be printed in debug window when the script is running: