Generate Feedback in Alfred 2 Workflows

After realizing that I had run out of function keys to launch my growing library of Services, I decided to try Alfred as an application launcher. The interface is intuitive and within an hour I had reassigned all of my keyboard shortcuts and was calling scripts by keyword.

The option to generate feedback in workflows means that you no longer have to use dialogs or hard-coded script properties to set users’ preferences. However, despite David Ferguson’s excellent tutorial and the examples available in the forum, the learning curve noticeably steepened when I tried to incorporate feedback into my workflow. This example demonstrates several scenarios and can be used as a template for your AppleScript projects.

Download

Overview:
To generate feedback in Alfred, we use a Script Filter to pass a query to the xml.scpt AppleScript. The script then processes the query and returns a properly formatted XML string containing information for each item to be displayed. After you import the Generate Feedback workflow, launch it with the gf keyword to view my five examples. To reveal the xml.scpt you will need to edit, open the gf Script Filter and click the Open workflow folder button at the bottom of the window.

Basic Usage:
Add each item you would like to be displayed in your workflow to the itemList by passing a record containing the required labels to the xmlItem handler. For an in-depth description of each attribute, I have included pieces of David’s tutorial in the script’s comments.

on run argv
	-- coerce the argv list to text
	set query to argv as text

	-- create an empty list to hold the items
	set itemList to {}

	-- Feedback item 1
	set end of itemList to xmlItem({uid:"My uid", arg:"My arg", title:"My Title"})

	-- Wrap the elements of the list with a declaration and return the list to Alfred
	return giveFeedback(itemList)
end run

xmlItem Handler
Required labels:

{uid:"My uid", arg:"My arg", title:"My Title"}

Optional labels:

subtitle:"My Subtitle"
valid:"no"
autocomplete:"Autocompleted text"
icon:"icon.png"
icon:"public.plain-text", iconAttribute:"filetype"  -- iconAttribute must be used with icon
icon:"/Applications", iconAttribute:"fileicon"       -- iconAttribute must be used with icon

Advanced Usage:
Characters < > & " need to be escaped properly for XML. To insert items that may contain these characters, pass the string to the clean handler along with a true/false value to remove leading and trailing whitespace.

clean("     Title with leading whitesplace & < > \" characters, trimming is true", true)

Alfred uses the uid attribute to learn which results are selected the most and then uses this information to sort your feedback. If you would like to manually arrange the returned items, use the uuid handler to generate a unique uid. In Alfred’s next release (after 2.0.2), this issue will be resolved and the item UID attribute will be optional.

uid:uuid()

Typically, you will want to match the displayed Icon with the arg by passing the same value to the arg and icon labels.

arg:"/Applications", icon:"/Applications", iconAttribute:"fileicon"

I have also included a list of file types with the workflow that may be used as values for the icon label when paired with iconAttribute:”filetype”. See http://imgur.com/a/0zXVX for the corresponding icons.

, ,

One Response to Generate Feedback in Alfred 2 Workflows

  1. Hubert CAMPAN June 30, 2013 at 1:47 pm #

    Hi John,

    Thank you for this post and Alfred workflow. I had now idea from where to begin. I had a ZIP code lookup workflow in mind via a script filter. Your post helped me greatly.

    Thanks !

    Hubert

Leave a Reply