Photo by P C from Pexels

Controlling MacOS WiFi with Alfred — Personal Productivity

Jan-Hendrik Kuperus
5 min readFeb 25, 2020

--

Today’s subject of being awesome with Alfred brings me to the topic of controlling parts of MacOS with Alfred Workflows. Previously I wrote about generating data with Alfred, which introduces how to create (very simple) workflows. Today’s workflow basically builds on top of those concepts, but with a little more bang for your buck.

It just occurred to me that I left out a piece of motivation for me to strive to all of this control through keyboard-centric tools: I dislike switching between the keyboard and a mouse/trackpad. Every time I am on a roll writing code and have to move my right hand from the keyboard to the mouse, it feels like an interruption. Given enough interruptions, I will break out of my flow and that never feels good. The workflow in this post prevents exactly some of those actions and makes you feel like a wizard when you use it. (At least that’s how it makes me feel)

The Workflows

There are three important workflows in this category that I use a lot:

  • Turning WiFi on or off
  • Turning VPN on or off
  • Turning Do not disturb on or off

These workflows differ from the data generators in the fact that they allow for the user to specify one of two keywords after the initial keyword. There are a few ways you can do this in Alfred.

The laziest way in terms of creating a workflow is simply allowing the Keyword input to have an argument and use that in the Run Script node to determine whether it was on or off. This has the advantage that you can simply type whatever you want after the keyword. In this case that is not what we want, since we can only support the keywords on and off.

So the other way to go about this to use a List Filter node instead of a Keyword. It’s also in the category of Inputs and allows you to specify a keyword and a set of possible values as arguments.

WiFi

The remainder of this post will explain the configuration of the WiFi workflow. The VPN workflow introduces yet another node and thus deserves its own post. Without further ado, let’s take a look at the initial List Filter node of the workflow.

Configuration for a List Filter node

Each argument in the list can have a title, a subtitle and a value that’s used in the workflow to pass on to the next node. Every argument can even have its own custom icon. When you configure a node like this and type wifi in Alfred, it looks like this:

Alfred now helps you to select the possible options. You can either type on or off or hit
or ⌘2 to select one of the options.

Now, let’s take a look at the rest of the flow for turning WiFi on/off:

There are two new nodes in this workflow, the one on the far right posts a notification to the MacOS notification center about the status that was just applied to the WiFi. That’s pretty straight forward, right?

So what’s that little chevron doing between the List Filter and Run Script nodes? Well, it makes sure the selected status (i.e. on or off) is persisted for te remainder of the flow. Normally data flows out of one node into another, but the ‘Arg and Vars’ block allows you to set one node’s output to a variable that can be referenced by all following nodes.

This node is necessary to let the notification show whether the WiFi was turned on or off. If we don’t do this, that node only has access to the output of the Run Script node. We could echo the selected value there, but there’s no real guarantee that will be the exact output. (There might be script errors as well, polluting STDOUT and being passed on to the next node).

The ‘Arg and Vars’ block in this flow is configured as follows:

The Argument text box allows you to specify the output of this node. We don’t need to do anything fancy here for this workflow, the Run Script node just needs on or off, which is also the value of {query}, so we just pass it on. The second box shows we are also assigning the value of {query} to a variable named status. This variable is then available in all following nodes through {var:status}, which you will see in the Post Notification node.

The Run Script node contains a nice one-liner to turn the WiFi both on and off. It’s the beauty of MacOS command networksetup that allows us to control the Airport card from the command line.

Configuration for the Run Script node

The value of the query (either on or off) is passed in through normal bash-scripting arguments and is available under $1. The last node in the workflow has a very simple setup:

Configuration of the Post Notification node

In this case, the title gives me all the info I need. When the command succeeds, I will see a confirmation popup in the top right corner of my screen. The node has some magic switches to work with the input, without requiring explicit conditional nodes.

Now you can try controlling WiFi from your Alfred setup. Go ahead, play with it and bask in your awesome new ability to control your WiFi connectivity through the leisure of your keyboard.

Cheers and until next time,

— JH

--

--

Jan-Hendrik Kuperus

Hi! I’m the Founder and Director of Yoink. I love writing code, tweaking it, beautifying it. I'm an all-round coder and a Professional Amateur Baker 😁🎂