I write this guide for people who want to learn how to set up Alfred with Script Filter as the input. It’s perfectly fine if you’re not familiar with any scripting languages. The guide will show you how to convert CSV files into JSON objects that can populate Alfred search results.

Pick a Scripting Language

You can write the script in any language you’re comfortable with and run it through a Unix shell. For this tutorial, we’re going to use Python 3. The easiest way to install Python is through a package manager like Homebrew. You can refer to this guide to install Python on macOS.

Workflow Setup

Create a blank workflow and add Script Filter input with keywords to trigger the workflow. Write this down in the script input field.

python3 main.py

Click on the folder icon as seen from the screenshot above to reveal the workflow directory. Create a new Python file in the workflow folder and name it main.py. The main process will be written in this script.

There are two things we want to achieve in this script:

  1. The workflow must be able to read the file with a relative path using tilde so any user can use it.
  2. The workflow must be able to convert the CSV into JSON for Alfred to filter the results.

Here is the sample.csv content that we’ll be using for this tutorial. You can store it on the desktop.

Download CSV

Python Script

Here is a Python script to read a CSV file from your selected source on your machine. I’m going to break down each part of the code, so they make sense, and you can modify it to match your workflow.

import os.path
import json
import csv

csv_file_path = os.path.expanduser("~/Desktop/sample.csv")

result = {
	"items": []
}

with open(csv_file_path) as csv_file:
	csv_reader = csv.DictReader(csv_file)
	for row in csv_reader:
		row_item = {
			"uid": row['title'],
			"title": row["title"],
			"subtitle": row["subtitle"],
			"match" : row["title"] + " " + row["platform"],
			"arg": row["title"],
			"text": {
					"copy": row["price"]
				}
			}
		result["items"].append(row_item)

output = json.dumps(result)
print(output)

First, we must import the modules we need to process the JSON and CSV files. These modules allow us to run the functions, read CSV files and convert them into JSON. We also want to expand the tilde using the os.path.expanduser function so this script can work for all users.

import os.path
import json
import csv

csv_file_path = os.path.expanduser("~/Desktop/sample.csv")

Alfred accepts JSON as the output of the script. The result must be stored inside the items key for Alfred to show them when you search for it. The following script declares a result dictionary variable with items as an array.

result = {
	"items": []
}

Now we want to populate the items key with the row item from the csv_file_path. The most important part is the value you assign for each key in the row_item. You can learn more about Script Filter JSON Format.

with open(csv_file_path) as csv_file:
	csv_reader = csv.DictReader(csv_file)
	for row in csv_reader:
		row_item = {
			"uid": row['title'],
			"title": row["title"],
			"subtitle": row["subtitle"],
			"match" : row["title"] + " " + row["platform"],
			"arg": row["title"],
			"text": {
					"copy": row["price"]
				}
			}
		result["items"].append(row_item)

The advantage of using Script Filter is that we can tell Alfred which value to use for the workflow. Here are some things that you want to consider modifying:

  • Define the value that you want to pass in arg.
  • Pass multiple values into the match key to expand what Alfred can search. In the following example, we combine row title and platform values.
  • Define the text result you receive from copying Command-C and displaying large type with Command-L in the text key.

Once the items are populated, you can convert them into JSON using json.dumps(result) and print the result for Alfred to pick up.

output = json.dumps(result)
print(output)

Example Use Case

Once you have the Script Filter input configured, you can chain them with actions based on the argument you’ve defined. For example, I have a CSV file containing the list of sites in the ad network. I can pass the site ID as the argument and open the dashboard URL to check the site performance.

If you use CRM like HubSpot, you can also export the list of customers you manage and store them on your machine. Instead of using HubSpot search, you can filter the result right from Alfred. You can be creative with the modifiers:

  • Create an action to accept customer ID to take you to the HubSpot contact page.
  • Set the text key to the contact email address so you can copy it from the search result.
  • Display the company name as the subtitle of the search result.

The advantage of Script Filter input is the flexibility to modify the value of the search result. It’s possible to build a fancier workflow with API, however, I see that processing CSV as the most practical use case for most people.