Matt Gemmell

TOLL is available now!

An action-thriller novel — book 2 in the KESTREL series.

★★★★★ — Amazon

Generating filename slugs with Alfred 2

code & alfred 6 min read

I’m a huge fan of the Alfred utility for OS X, so I was glad to see that Alfred 2 was released very recently. It supports extensibility with “workflows”, which let you chain together various triggers, scripts and actions to perform complex tasks.

I often have to generate URL-safe (or filename-safe) versions of strings (called slugs), such as the titles of my blog posts, and I thought I’d create an Alfred 2 workflow to help with the task.

As an example, since I use Octopress as my blogging system, I create new posts as Markdown files with filenames in a certain format. The title of this post is “Generating filename slugs with Alfred 2”, and the corresponding Octopress-suitable filename slug would be 2013-03-26-generating-filename-slugs-with-alfred-2.markdown.

(Whilst Octopress has a rake task to pre-create files with suitable filenames, I prefer to work on my drafts outside of the actual Octopress site-hierarchy, in my Dropbox, then move them in later for publishing.)

Accordingly, I made an Alfred 2 workflow with the keyword slug for this task. You can download the Alfred 2 workflow here.

Making basic slugs

To invoke the workflow, you simply use the keyword slug in Alfred’s input window.

Slug keyword action

Continue typing the string you want to slug-ify (or paste it in), and you’ll see various options for the type of slug you want to copy. Highlight one (or press the corresponding command-key shortcut shown beside the option you like), and the relevant slug will be copied to the clipboard.

The options are:

  • A plain slug version of the text.
  • A slug prefixed with the current date (in YYYY-MM-DD format).
  • A slug prefix with the date (as above) then the time (in HH-MM-SS format).

The date and time-prefixed versions are formatted such that files will sort predictably (chronologically).

Slugs without file-extensions

File extensions

If you’d like to set a default file-extension to be used in your slugs, you can use the slext keyword (for slug extension). There are a few default options, but you’ll probably want to enter your own. If you decide against having a default extension later, you can invoke slext again and choose the remove option.

Setting a default file-extension

Once you’ve set a default file-extension, you’ll see additional options whenever you generate a slug. You’ll still see the basic no-extension options too, and naturally Alfred will remember your choice and prioritise it next time.

Slugs with file-extensions

Regardless of whether you’ve specified a default extension, you can still specify one whilst you’re typing the string to slug-ify. If you do type an extension, it’ll override any default extension you’ve set.

Specifying a file-extension in a slug

Advanced options

You can change a handful of advanced options by editing some configuration parameters in the workflow’s actual code. It’s all in PHP script files on disk. To access them, simply select the Slug workflow in the “Workflows” section of Alfred’s Preferences window, then right-click the workflow’s name and choose “Show in Finder”.

Opening an Alfred 2 workflow in the Finder

A workflow is actually just a folder, and you’ll see all the contents. The file you want is slug.php, so open it in your favourite text-editor (I use, and highly recommend, BBEdit - which also has a free sibling called TextWrangler).

If your native language isn’t English, you might be particularly interested in the $preferred_locale option. It’s blank by default, which should cause it to use whatever is the default chosen locale in OS X (in the “Language & Text” panel of System Preferences). This option affects how certain characters are transliterated when generate a slug.

For example, if you’re an English speaker, and you pasted in some text including a “ö” (lowercase o-umlaut) character, you’d probably want that character to become a simple “o” in the slug. If you’re a German speaker, however, you’d likely instead want it to become “oe”, which is a better phonetic approximation of the original character in German. The slug workflow attempts to do these conversions automatically based on your system locale, using the rules in Johnny Broadway’s URLify library.

If you’d like to override the setting (for example if your system is set to German but you prefer the English style of transliteration, or vice-versa), feel free to set the $preferred_locale option to whatever country’s style you’d like to use (using standard two-letter country abbreviations, such as “en” for USA/UK/etc or “de” for Germany, and so on).

You can also configure your preferred locale without digging around in the PHP file itself, as follows. First, select the Slug workflow in Alfred’s Preferences window, and double-click the “slug Script Filter” box. It’s highlighted in red in the screenshot below.

Choosing a different default language for transliteration

You’ll see a sheet allowing you to configure this part of the workflow. At the bottom is a large text-field. By default it contains this:

php -f slug.php -- "{query}"

To specify a default locale for Germany, for example, you could add on “de” after a space, at the end of that line of text. So, it’d look like this:

php -f slug.php -- "{query}" "de"

It’s also highlighted in red in the screenshot below.

Editing my Slug workflow in Alfred 2

Click the Save button to save your changes and dismiss the sheet, and you’re done.


That’s about it! Once again, you can download the Alfred 2 workflow here. I hope you’ll find it as useful as I do.

I’m @mattgemmell on Twitter, and mattgemmell on too. You should follow me. If you get some use from this workflow and want to feed my gaming habit (or something else), I have an Amazon UK wishlist.

Enjoy the workflow.