Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Computer Skills
  2. Alfred

Creating Alfred Workflows in Haskell

Final product image
What You'll Be Creating

Functional Programming is the programming paradigm where you program without using variables. Functions call other functions with the variable passing between.

This paradigm allows for constants and strong type casting. It is also characterized with lazy evaluation: expressions are not evaluated until a result is required.

Haskell is a functional programming language that has been around since 1987. It has a great community of programmers behind it and is available on the Mac, Windows, and Linux platforms.

In this tutorial, I’ll create an Alfred library to help make workflows in Haskell. Then I am going to create a workflow using this library.

In this tutorial, I’ll assume that you’re already familiar with writing workflows in Alfred. If not, please check out these tutorials:

Since a complete description of Haskell programming is outside the scope of this tutorial, you can read A Gentile Introduction to Haskell: Version 98. I personally learned Haskell from the free book Write Yourself a Scheme in 48 Hours. I like to learn new languages by writing a program that is useful.

Installing Haskell

To use Haskell on the Mac, you first need XCode installed. This is a free install from the Mac App Store. When downloaded, launch XCode and open the Preferences Panel.

Opening XCode Preferences

In the Preferences Panel, click on the Locations item. At the bottom, make sure the Command Line Tools is pointing to your version of XCode. It will ask for a user login. Once given, the command line tools are set up to use.

With XCode installed and set up, download the Haskell compiler and support files from Haskell for Mac OS X.

After unzipping the file, move the ghc-7.10.1.app to the Applications folder (7.10.1 was the latest version as I wrote this tutorial).

Haskell Compiler Installer

The installer shows the code to add to your ~/.bash_profile file and your ~/.zshrc file (if you use Z-shell). Once you add that code to your shell files, click the Refresh Checklist. All of the checkboxes should be green. You are now ready to create Haskell programs.

Alfred Library in Haskell

Haskell has a package manager similar to NodeJS’s npm. It is cabal. With cabal you can download and install many third-party libraries that others have written in Haskel. For the Alfred Haskell Library, you need to install the TagSoup library. This helps retrieve the bundleid of the Alfred workflow. On the command line, type:

Now create a new file called Alfred.hs. All Haskell program files end with the .hs extension. In this file, start placing this code:

The module Alfred ( starts a module. All Haskell files are modules. Inside the parenthesis defines the functions that this module will make available. All Haskell functions start with a lower case letter. If the module is to be the main program, it has to have a main function. Since this is a library, it just has functions and constants declarations.

After declaring the module and what it exports, you write the list of the different libraries used. This library makes use of three libraries: System.Process, System.Directory, System.Environment, and the Text.HTML.TagSoup that we downloaded with cabal.

Next, I define three constants. The first line is the type declaration with the actual definition below it. The :: symbol declares that the next items are type declarations. Here, we are defining the constants as a String type.

The addItem function takes seven String inputs and creates the XML item String as output. Notice the type declaration looks like eight String types separated by ->. The -> symbol shows the next type. The first seven types are the input with the last one being the return value. All functions in Haskell have to have a return value.

The addItemBasic uses the addItem function to create an XML item with three of the inputs set to a default value.

The getBundleID get the ID of the Alfred Workflow bundle from the info.plist file in the workflow directory. This file is a plain text plist. Since this is a type of XML or HTML format, TextSoup library works real well to read it in and decode it. This is a monad function (simply put, a function that interacts with the item outside the program. For instance: files, GUI, etc) You can learn more about monads in the monads tutorial. Any function with the return type of IO () is a monad for input or output functions.

Here, the functional programming style breaks down to a list of items. The do keyword says, “do the following in order”.

The first line after the do reads in the info.plist file and parsed out the tags using parseTags from the TextSoup library. The second line finds the first dict tag and drops everything until the string tag. It then extracts the tags contents and places it in the trasistional constant id. id is a constant because it’s value can not change. Since all Alfred info.plist files have the exact same layout, I do not need to test the key to be correct. The id value is then returned.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.