Maharsh Patel

Sign in

How to create an Alfred Workflow using Python

Alfred is a Mac-based app that can help increase your productivity through instant searches, hotkeys, snippets, and more. This article will be focused on one core feature: workflows. Using workflows, you can enable quick access to third-party apps, or even create custom flows to help with repetitive tasks. If you can do something with code, you can do it with workflows.

What you’ll need to complete this tutorial:

  • Alfred app with the Powerpack
  • Basic Python knowledge
  • Apple Music (you can also modify this tutorial to work with Spotify)

What we will be making

To understand what is possible using Alfred Workflows, we will be creating a simple workflow that enables us to find music on Apple Music quickly without having to open the app. Alfred will be running our custom Python script using the Script Filter object to generate the results based on our search. Here is the result:

Creating the Workflow

Head over to the Workflows tab in the Alfred preferences and click the + button in the bottom and start a Blank Workflow.

Right-click the workflow page to view different workflow objects. Here you can find a bunch of triggers and actions that you can use to create very powerful workflows. In this tutorial, we are interested in creating a workflow that will run a Python script when we type the trigger command “play”. To do this, go to Inputs > Script Filter.

We want to be able to type “play invisible things” into Alfred to get a list of songs that match the query “invisible things”. To do this, set our keyword to “play” and make sure that an argument is required. Fill out the text that you want to be displayed while you are triggering this workflow as shown below.

In the script section, type in what is required to run your Python script. For now, just write python3 "{query}" Alfred will pass in the input as {query} so we can use that as part of our bash script and pass it to our Python file. If you would like to work with another language, you can replace the script with whatever you would write in a terminal to execute your program.

You may be wondering: Where is We need to create this. Alfred stores all the files associated with your workflow in a specific folder. To find this folder, go back to the main workflows page. Right-click workflow > Open in finder.

Alfred will pass the input to us using system arguments. To retrieve this, we can do the following:

We can pass this search query to the iTunes Search API to find the matching songs.

To view the results through the Alfred dropdown, we need to return them in a specific way. Alfred accepts either XML or a JSON output — here we will do JSON.

Fields Returned

  • title: the title displayed on the Alfred dropdown
  • subtitle: the subtitle displayed on the Alfred dropdown
  • icon: the icon displayed on the Alfred dropdown (put your desired icon in the same folder as your file)
  • autocomplete: what Alfred will use to filter your results
  • arg: the string that is passed to a connected action in your Workflow

For more fields that can be used, look at the Alfred Docs.

The iTunes API gives us a normal URL that we can use in a browser to view the song. For this Workflow, we want it to automatically open the song in Apple Music instead of the browser. To do this, we can change the scheme from https to music and then append &app=music at the end.

The last step remaining is to open the song in Apple Music. In the previous section, we talked about the arg parameter. This will get passed to any connected workflow objects. We can simply create a new workflow object by Right-clicking and going to Actions > Open Url. Set the URL to {query} and then connect both blocks.

Now you can test out your workflow by opening up Alfred and typing “play” followed by a search query. If you are not getting the results you expected, you can try debugging the workflow. This can be done either by debugging the Python file in isolation or by using the Workflow debug mode using cmd+d

Downloads / Conclusion

This tutorial covered a basic workflow example using Alfred. In this case, it allowed us to run our Python script more quickly, and also allowed us to interact with the results. The same learnings can be applied to many other applications. For example, you can create a workflow where you type in a stock ticker symbol and it can give you the current stock price. Or you can create a workflow that interacts with Jira and lets you update the status of a ticket without opening up the website. Alfred allows for very powerful workflows that can help speed up parts of your life. You can download this workflow and the associated code from my Github.

If you have questions about this article, feel free to reach out to me via email.

Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store