Re-running a Script Filter

New in version 1.23.

Alfred 3.2 introduced an new “rerun” feature. Emitting a rerun value from a Script Filter tells Alfred to run that script again after the specified number of seconds. Alfred will also pass back any variables set at the top-level of your feedback, i.e. via the Workflow.setvar() method.

Set Workflow.rerun to instruct Alfred to re-run your Script Filter.

This could be used, for example, to provide a seamless (to the user) status update in a workflow that reports on the status of a download client like aria2 via its API.

The refresh is invisible to the user, as Alfred doesn’t update its UI unless the Script Filter output changes.

In terms of Alfred-PyWorkflow more generally, you might use this feature to re-run your Script Filter if its data are still being updated in the background. For example:

 1from workflow import Workflow
 2from workflow.background import is_running, run_in_background
 3
 4wf = Workflow()
 5
 6# Unconditionally load data from cache
 7data = wf.cached_data('data', ..., max_age=0)
 8
 9# Update cached data if they're stale
10if not wf.cached_data_fresh('data', max_age=30):
11    run_in_background('update_cache', ...)
12
13# Tell Alfred to re-run the Script Filter if the cache is
14# currently being updated.
15if is_running('update_cache'):
16    wf.rerun = 1
17
18# Show (stale) cached data
19for d in data:
20    wf.add_item(**d)
21
22wf.send_feedback()

In this way, the results shown to the user will be updated only if the background update changes the cached data.