In the Up and Running With the Alfred Powerpack — The Basics tutorial, we covered a number of great PowerPack features such as Syncing, Custom Searches, Email, and 1Password. I highly recommend taking a look at that if you are new to Alfred, as below we will be focusing on Workflows and some of the more advanced aspects of this feature.

In this tutorial, I will be creating two types of workflows. One is a basic app launcher, that you might run when you get to work, while the other is a more in-depth look of how you can code your own workflow. I have intentionally omitted some features in the coding your own workflow section, to give you room to expand and practice what you've learned today. I encourage you to try to add more to them and share with others in the comments what you learned. The best way to learn is to share with others!

Workflows

This is by far, my favorite new feature into Alfred as it already has saved me quite a lot of time and energy, and I'm sure it will do the same for you. Workflows allow you to extend Alfred's abilities to do things that the developers may never have dreamed of. This includes everything from returning search results directly to Alfred to Searching Outlook for a message or calculating a color value based on a hex number and much much more.

I believe Alfred's support page describes workflows best, “Put simply, workflows allow you to connect triggers and keywords to actions and outputs. Once they're connected together, you're up and running!” (http://support.alfredapp.com/workflows).

While some people will create workflows, and expand them on their own ways, many will find workflows that help them online and will never create one. More on this later though. Let's get back to the basics of workflows though.



Should I Watch This Movie? Workflow by Andrew Pepperrell

The Workflows Interface

The Workflows pane, though somewhat overwhelming at first, is actually rather simple. The left most vertical column includes a list of all of the currently installed Workflows that you have. Here it will tell you the name of the workflow, the workflow's author, and will show you the icon associated with the workflow. At the bottom of this column on the left is a Share button, allowing you to easily export a workflow and share it with others.

To the right of the share button is a + and a - button. These buttons give you a quick way to add sample workflows, create a new workflow from either a template or from scratch, or delete a workflow. If you accidently click the - button though, don't worry because Alfred will ask and confirm that you really want to delete the selected workflow.



The Workflow List

On the right hand side you will see what a workflow does. There are a maximum of four columns, with each column corresponding to something specific. The first column, to the farthest left, is the trigger column, commonly used for hotkeys. This allows you to execute the workflow just by pressing the hotkey.

Depending on the workflow, you may be presented with the Alfred window waiting for you to type the next step, or away it will go! Other options for triggers include File Actions, Contact Actions, or Fallback Searches. We will only be referring to the hotkey's portion of the trigger column in this tutorial. You can learn more about each by clicking on their names above.



The Trigger column of a Workflow

The next column is the inputs column. This column is where we pass information into Alfred for it to use. For the Amazon suggest workflow we saw earlier, this would be whatever we are searching for and the keyword associated with it. The different types of inputs that Alfred accepts are Keywords, File Filters, and Script Filters. File filters and script filters are more advanced and will not be used in this tutorial, but they are used to do some very powerful things in Alfred! Developers, these would be features you would want to look at if you are considering creating an Alfred workflow.



The Inputs Column of a Workflow

The third column is where in most workflows the body of the work is done. This is the actions column. The actions column is where you can do the following:

These options, while overwhelming, means that it is possible to extend Alfred to do a multitude of different tasks. Read more by clicking on any of the above links. For the sake of our tutorial though, we will only be focusing on the Launch Apps & Files and the Open URL options.



The Actions Column of a Workflow

The last column in the workflows right pane is the Outputs column. This column allows you to Post Notification, Copy to Clipboard, or Run a Script.



The Outputs Column of a Workflow

Updating Workflows

When Alfred 2 was first released, there was no way to update workflows. To this day, Alfred app has not included built in ways to update a workflow. But, when you give others the power to expand your software, amazing things can happen. This is exactly the case with Workflows for Alfred app. Workflows has allowed users to create workflows that allow you to update your workflows, should that feature be supported by the developer.

While not all workflows contain this feature, there is one workflow specifically which has gone above and beyond and is recognized by Alfred users and developers to be the current “standard” for updating worklfows. This is the Alleyoop workflow by Daniel Shannon. Before I explain anymore about Alleyoop, it is extremely important to note what Alfred’s creator(s) has said about this workflow:

Note from Andrew: While this is a convenient method for updating workflows, I cannot endorse the use of it unless you are fully aware of the security implications of blanket updating all of your workflows. I’m currently working on a built in workflow auto-updater, for a future Alfred 2 release, that performs the standard verification checks that Alfred currently does on manual import, along with migrating your hotkey/keyword settings on upgrading. I'll also be adding 3rd party workflow developer signatures to keep you safe.

What this means is that while this is extremely convenient method of updating workflows, there is an inherenit danger since you are trusting the workflow developer to not have the update link go to something malicious that will be installed on your machine. With that said, and having used it personally, I have not seen or heard of any cases where developers have done that, nonetheless always proceed with caution when using software that connects to an unknown location (though if interested, you can look at the update.json file in the workflow’s folder to view from where the update is being received).

Anyway, back to Alleyoop. Alleyoop allows for developers to tell Alfred where the update will be found when there is one, what version the application is currently on, and a number of other small pieces of information. Alleyoop will then take that URL, check it and say that yes there is a file there. It will then check the versions against each other, if the remote one is a higher version number than the version you have installed on your machine, it will download that file to the Downloads folder so that you can install it manually. This allows Alfred to maintain your settings, configurations, etc.

Creating a Workflow

To be honest with you, when I first read about workflows they really confused me. All this talk about triggers, keywords, actions, outputs…What’s it all mean?! Well, I found that the best way to understand what workflows are was to create one and start playing around with them. For me, the first thing I thought about with a workflow was opening everything I need for work in the morning. At work I need Outlook, InDesign, Chrome (so I can listen to satellite radio), and more. I figured that this might be a nice and easy way to get acquainted with what workflows are, how to make a simple one, and how to use them.

What Do We Want Our Workflow to Do?

Before we dive into putting our workflow together, we need to know what we want it to do. This will make it much easier for us to create a concise workflow that does what we want and nothing more or less. For me, I want my workflow to open some applications, open a URL, and send a notification to Notification Center saying “Good Morning, Kevin!.” While the applications I list will be the ones I use on a daily basis, I want you to try to follow along with your own list! I know it may sound a bit scary, but trust me, you can do it!

First thing we want to do is create a new workflow. To do this, click on the small + button on the bottom right corner of the Workflow's List. Click on Blank Workflow from the pop-up menu.



Create a Blank Workflow

You will be presented with a pop-up modal asking for the Workflow Name, Description, Bundle Id, Created by, Website, and lastly an Icon. The only of these that is required is the Workflow Name. Nonetheless, let's go through these and learn a little bit more about them.

The workflow name is what appears in the workflow list above the author name. It's what the workflow will commonly be referred to. For our new workflow, let's name this, “Good Morning.” Next we have a description. This appears below the title in the right hand portion of the workflows pane and explains what the workflow does and any necessary information that the user might need to know. For ours let's just write, “Starts our morning applications, opens pandora, and wishes us a good morning.” In the box below, you will find the other three sections grouped together.

The bundle Id is used by Alfred to differentiate each workflow. For some of the popular workflows, the developers will release updates, new features, etc. The bundle Id is what allows you to download the new one, open it, and have Alfred know to ask if you would like to replace the existing workflow you have installed. For this reason, you want this number to be entirely unique and random to your application. Type in a random number here (I recommend that it be at least six digits). Next, put in your name and website and click the Create button in the bottom right.



the Workflow Details Modal

If you have an icon that you would like to use, simply drop that in the box to the left of the Bundle Id. Alfred will use that in it's results window.

Setting Up Our Workflow

Now that we have created our workflow, you will be presented with a rather bare window. First, let's setup the actions we want our workflow to do. Previously we had said that we wantted our workflow to open some applications, open a URL, and send a notification to Notification Center. To do this, click on the + button in a white circle in the upper right of the workflows pane.



Adding an Action to our Workflow

From the flyout menu, click Actions > Launch Apps / Files. You'll be presented with a blank modal window. This is the list of applications / files that will be opened by the workflow. To add new items to this list, navigate to the items you would like to add in the Finder window, and drag and drop the application or file onto the open modal dialogue. To know that it will be added, you will see OS X's standard + in a green circle next to the name and image of the application. Drag and drop each application or file in this way to add it to the list. When your list is complete, it should look something like this. The list will vary depending on what you need for your daily activities.



Completed Open Applications / Files Modal Window

You can now click the Save button on the bottom right of the window. This will save the action. Next, we want to setup our URL we want to open. As it is free, we will open Pandora Internet Radio. Click on the white + button again in the upper right of the window and navigate to Actions > Open URL. You will be presented with a modal window asking you for the URL you would like to open.

While this window has the ability to do more complex things, we will simply put http://www.pandora.com/ in the URL box. Select your browser of choice from the list if you would like to use something other than your default browser. When done, your window should look like this.



Completed Open URL Modal Window

Great! Click the Save button in the bottom right of the window. We now have our primary actions done. We had mentioned though that we wanted to post a notification though. Let's add that quickly.

Click on the white + button in the upper right. Navigate to Outputs > Post Notification. In the Ouput to dropdown menu, select the service you would like the notification to appear within. This can be either Mac OS X 10.8's Notification Center or Growl. Below that, we will leave the Only show if passed in argument has content unchecked. This is not needed for what we are doing in this tutorial. In the Title window, let's write, “Good Morning!” In the Text box, let's write something motivational. A great motivational phrase is, “Happiness depends upon ourselves” which was said by Aristotle. Feel free to write whatever works for you though! This is all we need to setup for what we are doing in this tutorial. With this setup, you should have a window that looks like this.



Completed Notification Modal Window

Setup Your Keyword

Great job. We now have all of our actions ready. But currently we don't have anyway to launch them. Click on the + button in the upper right again and navigate to Inputs > Keyword. As you may have guessed. We have another window asking for some information. In the Keyword box, we want to type the keyword we will use to launch this action. For this, I will be using “goodMorning” but feel free to use whatever you would like. To the right you'll see a checkbox and a dropdown menu. As we do not want to pass any information to Alfred, let's uncheck the with space box and from the dropdown select No Argument. The Argument Required and Argument Optional options would allow you to require (or not) the user to give more information to Alfred. Our workflow though does not require this.

Next, we want to put the Title and the Subtext for the keyword. The Title is the top text that appears in the Alfred window after typing the keyword and the Subtext is the bottom text that appears in the Alfred window. See the image below for a nice visual of what I am referring to.



Alfred's Title and Subtext

Let's put, “Good Morning!” for the title and “Let's have a great day” for the Subtext. You should now have a window that looks like this.



Completed Keyword Input

Connecting the Pieces Together

We're just about done. You'll notice if you try to run your new workflow, nothing will happen. We haven't actually hooked the pieces together. When you hover your cursor over any of the squares, you will notice a tab appears on the left and/or right of the box. We use these to connect the pieces of our workflow.



Connecting the Workflow

Click and hold on these tabs and drag them to the purple tabs on the other items that we have created. Specifically, drag the right tab of the Keyword to the Launch Apps / Files. Then drag the right tab of the Keyword box to the Open URL action. If you run the workflow now, you will see that the workflow will open the applications and the URL, but won't post the notification. If you look at the workflow's panel, you'll see that we haven't connected the actions to our output. Let's connect both the Launch App / Files and Open URL to the Post Notification. Voila! We now have a workflow that will launch our morning applications and open Pandora for us! Great job! Your workflows pane should look like this.



Our Completed Workflow

Creating and Downloading More Advanced Workflows

While this tutorial has introduced you to the basics of workflows, it has only scratched the surface of the potential that workflows have to offer. Users have already created a large number of fantastic workflows that do things that the developers couldn't have expected. For some of you though, this tutorial may have peaked your interest and given you a desire to create your own, more advanced workflow. Luckily, Alfred has made the resources you need readily available.

How to Find New Workflows

If you are interested in finding new workflows for Alfred, the best place to find them is the Alfred app Forum's Share Your Workflow subforum. Here, developers have posted a number of fantastic workflows for that are ready for you to use right now! At the time of writing this, there are 20 pages worth of workflows that users have created and shared.



Alfred's Share Your Workflows Subforum

Some users have complained that this is not the best way to handle workflows, and a number of users have said that they are working on a solution for this that would be a centralized location to find and download all of the Workflows available for Alfred. So far, some have come and gone as they faced a number of challenges such as bandwidth, but keep an eye on the forums for this when a viable solution is found!

Use What We’ve Learned to Prevent Your Mac From Going to Sleep

So now that we have a nice basic understanding of how workflows work, the workflows interface, and setting them up. Now would be a great time to step it up a notch and use what we have learned thus far to create something that may be more useful than just a basic app launcher.

With that said, we will be taking advantage of a often overlooked command that was introduced in the OS X Mountain Lion terminal: caffeinate . For those not familiar with the caffeinate command, it allows you to prevent your computer from going to sleep. While going to sleep is often what we would like our machines to do, whether to save battery power on a laptop or to save power on a desktop, there are times when this is not the case, such as when we are watching a movie and don't want to move the mouse every so often. This is where this workflow will come in handy, as users will be able to tell Alfred they would like to caffeinate for X minutes, where X is the duration we set.

Before we begin on this workflow, it should be known that we are essentially mimicking other workflows that exist such as Caffeinate by Shawn Rice. We are not copying the funtionality or code that he is using, as his uses more advanced programming concepts which are beyond the scope of this article.

I recommend checking out his Workflow for a more full featured workflow that can work not only with Mac 10.8 (Mountain Lion), but also with applications such as Caffeine. Nonetheless, we will be using the idea of this workflow, to use the caffeinate command, since it illustrates well how a more advanced workflow can be created.

How Does Caffeinate Work?

Caffeinate is a terminal command that creates a small (no noticable performance impact) command in the background to tell your mac that it should stay awake and not go to sleep. While this command is and can be rather powerful, we are only really going to scratch the surface of it. For those of you who may be interested in learning about the different options available to the command, a more technical description of what is occuring in the background, and the location of the process itself on your machine, visit Apple’s Mac Developer Library entry for the Caffeinate. The basic syntax that we should be familiar with though are the following two:

Thinking Through What the Workflow Will Do

Before we begin writing any code, first thing we need to do is talk about what we want and get some of the basics setup. We want this command to take a keyword and a time argument and then enable caffeinate. Because time can be given in many different formats, such as seconds, minutes, hours, days, weeks or even years, we want to ensure we are clear about how we are expecting the time to come in.

For the purpose of this workflow, I will be expecting the time to be entered into the Alfred prompt as minutes. While there are plenty of reasons you may want to use hours, or another measurement of time, I am choosing to work with minutes since this is the standard way time is given for films, which is why I would normally be using this command.





To get started with our project, create a new workflow using the + > Templates > Essentials > Keyword to Script found in the bottom right hand corner of the available workflow’s list. This will create a new workflow called, “Keyword to Script.” Let's change that name to something a bit nicer. To do this, double click on the workflow, and you'll be presented with a familiar modal window. Change the Workflow Name to ‘Caffeinate’ or whatever name you would like the workflow to have. For the description, I’m going to put, “Prevent the Mac from sleeping.” Now that we have those in, you can fill in the Bundle Id, Created By, Website and Icon if you wish. This is optional though, so feel free to just keep moving forward and skip right over that. With this in place, your workflow should look like this





Now that we have the workflow itself created and setup, let's start adding the power to accomplish our task to it. First thing we should do is add the keyword we want to use. This will allow us to test our workflow if we choose to along the way, which is why I always prefer adding the keyword first. To add the keyword, double click on the Keyword box in the workflow pane and you'll be presented with the keyword dialogue box. Here, we want to set the keyword to ‘caffeinate’ (or you can use something shorter like ‘caff’). Then for the title, we'll put ‘Caffeinate’ and for the Subtext we'll put ‘Prevent the mac from going to sleep’. If you wish, you would be able to also add an icon for this, I will not be doing that for this tutorial, but feel free to use an image of your preference. With these settings in place your dialogue box should look like the image below. Just hit Save and your keyword is now setup!





You will notice that we have left Argument Required selected to the right of the keyword, remember that we need to get the time for how long the user wants enable the caffeinate function. To test that this works, open the Alfred window using the hotkey that you have set and type ‘caffeinate’. If all is setup correctly, you should see the workflow appear in the list like so. If you hit enter though, nothing will happen. So let's fix that!





Harnessing PHP to Caffeinate Our Mac

For this section, I will be using PHP to handle a lot of the work for us. This is primarily due to my personal familiarity and comfort with PHP, though this could be done using other languages as well. If you have questions, comments, or concerns, please leave a comment below and I will do my best to answer your questions.

We will also be assuming that you are paying attention to whether you have enabled or disabled the command, and will not be doing more advanced checks to ensure caffeinate has not already been enabled. For these types of features, I highly recommend using existing workflows for this due to the larger range of features they include.

Double click on the Run Script box in the workflow panel to open up a script modal window. The first thing we want to do is change the Language select box from /bin/bash to /usr/bin/php . This will allow us to write our code in PHP instead of directly in bash (the language of the terminal). We also want to leave all of the default escaping on. This prevents us from accidently using characters that will cause errors later. Only change these if you are familiar with escaping special characters manually and have a good reason to. Generally speaking, the default escaping in Alfred is very good. At this point, your modal window will look a little bland, but we'll put in a script soon enough!





Add a notification after the script by clicking the + within the circle in the upper right of the workflow window. Then select Outputs > Post Notification.





You will then be immediately presented with a dialogue box for Alfred. I will be leaving the Output to Default, but feel free to change this if you prefer to use something different for notifications. Check the box for Only show if passed in argument has content. This will prevent us from displaying a blank notification should something go wrong. For the Title let's put ‘Caffeinate’ and for the Text let's put ‘{query}’. By putting ‘{query}’ we are telling Alfred that we want to rely on our script to generate the notification. All Alfred needs to do is display what the script tells it to. The modal box should now look like this:





Now, just connect the script from the right tab of the script box to the left tab of the Post Notifaction box, and voila! We are ready to start coding the script! So now that we have everything ready for the script, let's jump right into the script. I'll walk you through the code below.

Understanding What's Happening in the Script

Starting at the top, we first use error_reporting(0); . This is important because it takes care of an error that PHP will call about using the system timezone. This will break the script and as such, we just are going to shut error reporting off for this. In other environments, you may want to get the system's timezone and handle that yourself. A great post discussing this can be found on the Alfred forums. Because we aren't working with any timezones though, we just are going to ignore them entirely and shut it off.

Next, we get to a simple $query = "{query}"; . Here we are setting a new variable, named $query equal to the query we entered in Alfred. A query is whatever follows our keyword. For example, if we type “caffeinate 15” into Alfred, ‘caffeinate’ (or ‘caff’ if you went with something smaller) would be our keyword and ‘15’ would be our query. Because we need our query, which is the time, in minutes, that we would like to run caffeinate for, it's important for us to set this variable.

Before we do anything with this information, we need to make sure that what was entered is a number. While you wouldn't intentionally put non-numbers in this, you may accidently hit an extra key, so we want to make sure that we account for that possibility. To do this we put an if / else statement. The if (is_numeric($query)) checks if the query we entered is numeric, if it is, we run all of the code between the curly braces which ends right before the else statement. If it's false, we go straight to the else code block (where you see the comment, //If they didn't enter a number, we want to prevent this script from continuing ). Here we echo out, which will go to our notification, that a number was not entered, please enter a number.

If we did enter a number though, we need to keep working with the query because we are entering how many minutes we would like for caffeinate to run. As mentioned earlier, the caffeinate function doesn't work with time in minutes, and instead it works in seconds. So the next thing we do is $query * 60 and set that equal to a new variable called $timeInSeconds . While there are more concise ways of doing this, I leave the original variable alone in case we need to reference it later, or if you wanted to try to expand the code and use it for something more advanced.

Now we get to the real important part of the script, setting up the command. I like to keep my command separate from the execution, though you certainly could do this other ways. For me, this is easier to read and understand from an education perspective, but not as effective from a performance standpoint. Anyway, here we have the line $shellCommand = "caffeinate -dsit" . $timeInSeconds . ">/dev/null 2>&1 &"; . Don't worry though, while this looks pretty intimidating, it's actually nice and simple.

First, we start with the command we want to run, which in this case is caffeinate . Next, we are setting some options for what we want caffeinate to do. In this case, that is the -dsit . The d tells the operating system that we want to prevent the display from sleeping, the s tells the operating system that we would like to prevent the machine from sleeping.

This flag only works though when your machine is connected to an AC power source, otherwise it will be ignored. The i tells the opertaing system that we would like to prevent the machine from idle sleeping. Lastly, we set the t option which tells the operating system that we will be specifying a time length for this process, then we would like the process to stop and allow our machine to sleep as normal.

After setting our options we then need to specify our time, so that the t option we set knows when to stop. To do this, we close our single quote and put a period after it. This period indicates that we are concatinating the script. For those who aren't familiar with concatenation, it's basically a fancy way of saying we aren't done with our command, we just have to put something different, such as our variable, next. We then put the $timeInSeconds variable in place, because as you remember, we need the time to be in seconds for the command to understand what we would like it to do. After this, we use the period again to continue our script and reopen our single quotes.

In the single quotes, you'll see some weird looking stuff. This section, the >/dev/null 2>&1 looks scary but it's not nearly as scary as it sounds and looks. Xaprb’s blog does a great job of explaining this in more technical terms. The short explanation though that is given there is all that we really need to know though. To quote them, “all output from this command should be shoved into a black hole.” This means that no matter what our command would say or show while it was running, which in this case is nothing, just disappears. After this though you see one last & which tells the machine that we want to run this process in the background. Hopefully this has all made sense, but if you have questions about what's happening here, please just put a comment below and I'll try to answer the questions I can.

Now that we have our command ready, we need to execute it. PHP has a great command for just this purpose called shell_exec() . This function will run the shell command, the type commands you put in the terminal, that we put between the parentheses. We want to run the shell command we just talked about, so here we put shell_exec($shellCommand); . This will execute our command for us. At this point, we could be done, but what's the point without a nice little notification telling us it was activated?

This is why we have the next if / else block and the echo statement. The if statement checks to see if we less than or equal to one minute (by checking to see if time in seconds is less than or equal to sixty). If it is, we want to say minute instead of minutes. We are simply setting a variable to the correct word so that our notification is grammatically correct. After this, we echo that caffeinate has been enabled for the time we entered followed by the correct type of time (minute or minutes). You'll notice that unlike earlier, we did not concatinate this line and instead used the variables inside of the quotation marks. The difference here is that we are using double quotation marks instead of single quotation marks. This allows us to put variables inside of the quotation marks.

And that's it folks. We now have a working Caffeinate workflow that will prevent your mac from going to sleep. While this can be greatly expanded to include other applications, or even just to accept more forms of time such as hours, days or longer, this is a great start. I hope you've enjoyed learning how to craft a workflow and hopefully you learned a lot about the caffeinate command in the process. This is a great command for users of Mountain Lion.

Creating More Advanced Workflows

While here we have shown a basic and intermediate level workflow, we really have only scratched the surface of what is possible with Alfred. Not only does Alfred provide users a forum dedicated to sharing workflows, but they also have a forum dedicated to helping developers achieve their goals and make a great workflow. The Workflow Help & Questions subforum is a goldmine of information for individuals looking to extend Alfred. I can say from experience that David Ferguson, one of the Alfred staff members, is fantastic and is always willing to help developers however he can.

A great resource though that can easily be lost in the forum is David's PHP Workflow's Class This resource is an extremely useful resource that can make writing an Alfred app workflow even easier. David's PHP class gives you a lot of handy features that save you as a developer time and energy in regards to preparing your content and interacting with Alfred itself. Using the class is very simple though and if you have questions, users are extremely helpful on the Alfred forums.

Tying It All Together

Great job for working through this, whether you made both workflows, just one, or just wanted an intro to workflows. In my personal opinion, workflows alone are enough of a reason to purchase the powerpack, as they give you such a great opportunity to expand upon the developer's thoughts and plans and add features that are important and helpful for you and that save you time and energy.

I hope that you've enjoyed this tutorial. If you think I missed something, or feel that I should really cover another aspect of Alfred, let me know in the comments, below.