At this point in the series you should be pretty familiar with Keyboard Maestro; it’s one of the most versatile and powerful Mac apps available. With it, you string series of triggers and actions together to create macros that give you near total control over OS X.
In this tutorial I’ll build on the features I’ve already covered and create a powerful macro using variables. I’m also going to touch on using Keyboard Maestro to execute AppleScript actions.
To follow this tutorial you’ll need to be pretty familiar with Keyboard Maestro. If you aren’t already, the fastest way to get up to speed is to check out the previous tutorials in the series:
- Keyboard Maestro I: Introduction,
- Keyboard Maestro II: Launching Apps Intelligently,
- Keyboard Maestro III: Situational Triggers, and
- Keyboard Maestro IV: Control Flow.
In this tutorial I’m going to be using basic AppleScript. If you’re unfamiliar with it, check out some of the AppleScript introductory tutorials before continuing.
You also need Keyboard Maestro installed. It’s available for $36 from the developer’s website. Although there’s a 30-day trial, I recommend you skip it. If you’re following this tutorial, you’ve already made your mind up that Keyboard Maestro is for you.
A Jumping Off Point
The macro I’m creating in this tutorial only has a single-use case, however, the techniques used to build it can be applied to countless other situations.
Unless you’re already familiar with Keyboard Maestro, I recommend you start by following this tutorial exactly and then looking at how you can apply everything to your own macros. If you already use Keyboard Maestro, then just use my work as an inspiration and jumping off point.
Variables are a hugely important feature of Keyboard Maestro. With variables, Keyboard Maestro can store information that can be used later on. For example, in the third tutorial I used a very simple variable to get the current time and name a screenshot based on it.
Variables aren’t static. With Keyboard Maestro you can perform operations on variables to manipulate their contents. You can also use multiple variables within single actions.
Tweeting What You’re Listening to on Spotify
When I’m working I listen to music using Spotify. If I find a great song, I’ll often share it on Twitter. This macro uses AppleScript to get the information about whatever track is playing from Spotify, save the results as a number of variables in Keyboard Maestro, manipulate the information and post a tweet from Tweetbot that says something like Right now I’m listening to L’Amour Toujours - Radio Edit by Gigi D’Agostino, check it out: http://spoti.fi/1T391Uz #NowPlaying.
When building a macro this complicated, it’s important to lay a solid base and work in simple steps. Jumping straight into the challenging parts will only come back to bite you later on.
Start by creating a new macro called Tweet Spotify Song and give it a simple Hot Key or String trigger.
This macro starts with two nested If Then Else actions. The first checks to see if Spotify is running, the second to see if it is playing.
This means that if the music is coming from a different source, Keyboard Maestro won’t accidentally send out an incoherent tweet. When building your own macros, these preliminary checks are a great way to make sure everything behaves as you want.
Create the first If Then Else loop and have it check to see if Spotify is running. In the otherwise execute the following actions section, have Keyboard Maestro send a notification saying That music ain’t coming from Spotify Cap’n! or whatever you want your error message to be.
The second If Then Else loop has to check if Spotify is playing. There is no easy way to do this with Keyboard Maestro however it is a simple task with AppleScript. Inside the execute the following actions section of the first loop, add an Execute an AppleScript action from the Execute Group. Have the action save the results to variable. I called mine Playing.
Inside the script text box paste the following:
tell application “Spotify” if player state is playing then return 1 else return 0 end if end tell
This code checks to see if Spotify is playing. If it is, the variable Playing is set to 1; if it isn’t, it’s set to 0. Keyboard Maestro can’t check whether or not Spotify is playing, but it can check what value Playing has.
Create the second If Then Else action and have it check if The Variable: Playing is 1. If it isn’t, have the action send the same error notification as before.
Next, assuming Spotify is running and currently playing a track, I need the macro to retrieve three pieces of information: the track’s title, artist and ID. Create three more Execute AppleScript actions and have each one execute one of the snippets below saving the results to the variables Song, Artist and TrackID respectively.
tell application “Spotify” set theTrack to name of the current track end tell return theTrack
tell application “Spotify” set theArtist to the artist of the current track end tell return theArtist
tell application “Spotify” set theID to id of the current track end tell return theID
Now that Keyboard Maestro has all the information it needs, it’s time to get it in order. The Song and Artist variables are fine but the TrackID variable which I’ll use to create the track’s URL needs to be manipulated.
From the Variables group, add a Search and Replace Variable action. Set it to Search variable TrackID for spotify:track: using String Matching. Leave the Replace section empty. This action will search the TrackID variable and remove the unneeded text leaving just the ID.
With that done, it’s time to create the series of actions that will send the tweet. I’m going to use the same technique I used to in the previous tutorial. Create another If Then Else loop that tests to see if your Twitter client is running—in my case it’s Tweetbot. If it is, have the macro use a Select or Show a Menu Item action to select Tweet > New Tweet and activate Tweetbot.
Using an Insert Text action, have Keyboard Maestro paste in Right now I’m listening to %Variable%Song% by %Variable%Artist%, check it out: http://open.spotify.com/track/%Variable%TrackID% #NowPlaying. The values it has stored for Song, Artist and TrackID will be inserted instead of the variable placeholders.
If Tweetbot isn’t running, have Keyboard Maestro launch it, wait until it’s running and then follow the same actions as above.
Now the macro is set up and ready to use.
In this tutorial I’ve shown how variables can be used in Keyboard Maestro to create complex macros. By combining them with other tools like AppleScript, Keyboard Maestro can access information it otherwise couldn’t. Along with what you’ve learnt from the previous tutorials, you should now be able to do almost anything you want with macros.
This is the last tutorial in the current series. If you want me to revisit Keyboard Maestro and look at some more features or have any questions please post them in the comments.