Alfred Workflows

If you ever thought why I should buy a Mac the answer is simple, Alfred ! Can’t think of my work day without it. Soon to be release V2 of Alfred will include a new feature called “Workflows” and as a Mega Supporter I have access to beta version of the app and let me tell you this, if you’re a Power Pack user, prepare for your mind to be blown !

So what’s the big deal about workflows ? Well, IMHO they are game changer for Alfred. Alfred used to be sort of a launcher app, basically you could add search URLs and launch them simply by typing a word, for instance if I want to search Amazon for a product I would launch Alfred and type amazon + query:

Screen Shot 2013-01-23 at 12.21.30

this will work with any page, I have number of search actions added like one to search Google Play for apps:

Screen Shot 2013-01-23 at 12.24.02

Extensions can be installed so you can perform actions on scripts etc. For instance you can search Evernote using AppleScript:

Screen Shot 2013-01-23 at 12.26.20

You can hook up any sort of script to it. So what’s the big deal about workflows ? Basically they make Alfred interactive and only your scripting abilities can stop it. I’ve got this simple Google search workflow from Alfred Blog, you launch Alfred, type g and then your query and here’s what happens:

Screen Shot 2013-01-23 at 12.31.09

So how this works ? Well I took this workflow and used it to learn some basics and created Amazon UK product search, here’s a basic workflow with code stolen from Google workflow by David Ferguson:

Workflow

Screen Shot 2013-01-23 at 12.36.35

Input => Script filter

Screen Shot 2013-01-23 at 12.36.56

The PHP script is used to fetch JSON file from Amazon and return XML that Alfred will present as query suggestions, here’s the script:

<?

require_once('extension_utils.php');
$utils = new ExtensionUtils();

$results = array();
$query = utf8_encode(urlencode($argv[1]));
$json = file_get_contents("http://completion.amazon.co.uk/search/complete?method=completion&q=$query&search-alias=aps&client=amzn-search-suggestions/9fe582406fb5106f343a84083d78795713c12d68&mkt=3");
$sugestions = json_decode($json, true);

foreach($sugestions[1] as $sugg)
{
	$item = array(
		'uid' => 'suggest {query}',
		'arg' => $sugg,
		'title' => $sugg,
		'subtitle' => 'Search Amazon UK for '. $sugg,
		'icon' => 'icon.png',
		'valid' => 'yes'
	);
	array_push($results, $item);
}

if (count($results) > 0)
{
	echo $utils->arrayToXml($results);
}
?>

this script takes single parameter which is passed from Alfred, upon enter the Output action is launched

Action => Open URL

Screen Shot 2013-01-23 at 12.37.06

Here’s what it looks like in action:

Screen Shot 2013-01-23 at 12.48.57

You can download it from GitHub

This is quite simple so far but on the same principal some more complex Workflows can be build. I’m currently working on a Workflow that uses OpenStack Supernova to generate list of all nodes from environments stored in ~/.supernova with public IP and auto suggestions, when node is selected the Workflow launches iTerm and runs ssh command, here’s a workflow:

Screen Shot 2013-01-23 at 12.53.08

This one actually has 2 flows and 3 actions:

  • Suggest node from list of all nodes and SSH to it
  • Suggest node from list of all nodes and SSH to it using Rackspace script (this basically takes care of filling in your LDAP user and use correct SSH ports and it’s useful only internally)
  • Re-generate list of nodes – a seprate action that generates JSON file with all nodes + public IPs

Here’s a small preview:

This workflow uses filter scripts as input and Apple scripts as output + Notification Center notification. Filter script is almost exactly same as the one used for Amazon search workflow, here’s Apple script:

on alfred_script(q)

	# Check to see if iTerm is running
	tell application "System Events"
		set itermRunning to count (every process whose name is "iTerm")
	end tell

	# If it's not running, activate
	if itermRunning = 0 then
		tell application "iTerm" to activate
	end if

	tell application "iTerm"
		make new terminal
    tell the current terminal
        activate current session
        launch session "Default Session"
        tell the last session
            write text "ssh " & q
        end tell
    end tell
end tell

end alfred_script

It still requires some work, I see duplicate characters typed when launching this workflow (I suspect it’s PHPs fault but not sure), the other thing is that iTerm opens new Tab no matter if it opened new window or not, so basically if You don’t have iTerm running it will start it but then add another Tab anyway (that’s my lack of knowledge of Apple script). It also seems that PHP scripts ran by Alfred do not have $_ENV vars which makes my list generating action non functional, unless it’s started from workflow home directory. Btw, all workflows are stored in ~/Library/Application Support/Alfred 2/Alfred.alfredpreferences/workflows.

Happy coding !

  • http://twitter.com/maxxxlarsson Max Larsson

    Say for example that I want to apply this suggestion search on a site like IMDB, do I simply copy the Amazon-script and change the URL and “Search Amazon UK for..”? Or how do I implement it?

    • artkrz

      You need some sort of feed from IMDB that the filter script will use, a URL that returns JSON or XML for your query. Once you have a filter script it’s quite easy to set up a work flow.