Shorten a link with a custom domain in

I got a short domain recently to spam your Twitter and feeds with interesting things I find throughout the day. Most of the time Buffer and IFTTT get the job done, but whenever I want to shorten a link for private reasons, things get messy. Now I hit a simple shortcut using Keyboard Maestro.

One day left to purchase Keyboard Maestro for $19.99 instead of $36, so if I were you I’d better hurry!

There’s a token in Keyboard Maestro to collect the current active window in Safari or Chrome. Use %SafariURL% for the first, %ChromeURL% for the latter. We’ll turn that into a variable and call it from a shell script.

Next we’re adding a shell script to invoke a little python. However, for the script to work you need your oauth token, which can be generated here after you confirm your password. It is a long string of characters, keep it. Now check this code:


import requests
import json
import os

longURL = os.environ['KMVAR_url']

# Get your oauth token at

query_params = {'access_token': oauth,
                'longUrl': longURL

endpoint = ''
response = requests.get(endpoint, params=query_params)
data = json.loads(response.content)
print str(data['data']['url'])

Place your token where it belongs and remember to set the output to save results to clipboard. I also added a notification to alert me when the action is done. Just set the message to %CurrentClipboard% to get the short link displayed.

You can download the macro here.

Two things to notice here if you’re intending to customize Keyboard Maestro using Python. If you use variables, you can grab them by importing the os module and using the following call: os.environ['KMVAR_var’], where var stands for your variable name and KMVAR_ a mandatory prefix. The other thing to point out is what is a python result for Keyboard Maestro, which is whatever you print.