Adventures with Alfred:
A Basic Ruby Workflow


I’ve been using Alfred for a little bit now, and have fallen in love with it’s amazing workflow feature. The power it brings to automation is incredible and easy to use. After toying with some of the different workflows out there, a lot of them from Packal, I wanted to learn how to create my own.

Alfred allows you to run scripts in various languages, such as php, bash, and apple script, but I’m most familiar with ruby. I found some good tutorials on creating workflows with ruby but I needed to understand Alfred and how workflows function before going deeper. I’ll walk through the process of creating my first workflow with ruby, trying to keep it as simple as possible.

For my first workflow, I decided to automate (at least partially) my weekly report process. I write my reports in markdown, so it’ll be easy to work with. What I wanted this workflow to accomplish:

  • Run task with a keyword.
  • Run a ruby script to create a new report, update header data, and use previous report’s data for content.
  • Copy the file name (without extension) to the clipboard
  • Show a notification that the workflow completed.

Searching for “Alfred ruby workflow”, you’ll come across an awesome template built by Zhaocai. This is great for when you expand your workflow to take care of multiple tasks, save settings, and distribute. However, it does require you to have a good understanding of workflows, so I’m skipping this for now.

Creating The Workflow

Open Alfred and navigate to the workflow pane. At the bottom of the panel on the left, click the plus button and select a Blank Workflow.

Alfred will then ask you to input some details about your workflow to identify it. You can put whatever you’d like in each of the inputs, and even add your own image to identify your workflow. I’ve skipped the image portion, but you can see an example of how to fill the other fields out below.

Bundle Id, Created By, and Website are all optional.

For this workflow, we’ll want to create a keyword input. A keyword input will run a specific task when we type it in to Alfred. To create one, click the plus button on the upper right of the workflow canvas. Select the Keyword option from the Input menu to bring up a new window asking for details about your keyword.

To keep it easy, I’ve decided that my keyword doesn’t accept any user input. The title and subtext inputs appear when you’re typing into Alfred.

A preview of what it looks like in Alfred.

Adding the Script

Click the plus button on the workflow canvas and select “Run Script” from the Actions menu. This will bring up a new window asking for your script details. I’ve left everything as default here as well, except I changed the language to ruby.

I’ve provided my script below and commented on a few lines, notable to working with Alfred. This isn’t necessarily good ruby code, but I’m not concerned about that right now; version 1.0 people. Feel free to edit and use it, but since it’s not really meant to be shared, you’ll need to make some significant changes. Add your script and save it.

Now you have on the canvas a Keyword item and a Run Script item. When you hover your mouse over the Keyword item, you’ll see a little nub pops out on the right. Click and drag that nub to the Run Script item to connect the two together.

Finalizing the Workflow

The last two things I wanted my workflow to accomplish was to copy the name to the clipboard and notify me that the workflow ran successfully. To do this, click the plus button at the top right of the workflow again and from the Output menu, add a Post Notification and a Copy to Clipboard action.

Copy To Clipboard

Since the ruby script printed the filename, the filename was added to the {query} variable that Alfred provides. We just have to add {query} to the Copy To Clipboard input window.

Like before with the Keyword item, hover over the Run Script item and a nub appears to the right. Click and drag it to the Copy to Clipboard item to connect the two. This action makes sure that the {query} parameter is passed properly.

Post Notification

Last but not least, the post notification will display a banner when the script is finished in Notification Center. I’ve left a lot of these details as default, just adding the Title and Description. You can use the {query} variable here to show the filename as well.

You’ll notice that you can’t hover over the Copy To Clipboard item to get a connector nub to appear, since it doesn’t output anything. Workflow items that have output can be connected to multiple items, however, so connect the Run Script item to the Post Notification item. You should have a workflow that looks like the example at the bottom.

Recap

Creating this workflow has taught me a few things about Alfred:

  • How to create Keyword actions to run scripts.
  • Getting data from scripts to pass to other workflow items.
  • Copying output to clipboard and posting a success message.

There’s definitely some issues with how this workflow functions, namely I have to manually create the new report directory and file when the new year flips, plus It would be nice to figure out to how to automatically email my report after I’ve finished writing it. Before, this seemed like a daunting task, but this simple example workflow showed me how that it’s not as tough as I thought.

Hopefully, you’ve come away with an idea of something to automate in your daily life!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.