Deprecated: Return type of I::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/public/kirby/toolkit/lib/i.php on line 62

Deprecated: Return type of I::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/public/kirby/toolkit/lib/i.php on line 91

Deprecated: Return type of I::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/public/kirby/toolkit/lib/i.php on line 71

Deprecated: Return type of I::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/public/kirby/toolkit/lib/i.php on line 101

Deprecated: Return type of I::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/public/kirby/toolkit/lib/i.php on line 53

Deprecated: Return type of Collection::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/public/kirby/toolkit/lib/collection.php on line 80

Deprecated: parse_str(): Passing null to parameter #1 ($string) of type string is deprecated in /home/public/kirby/toolkit/lib/url.php on line 135
One Tap Less | Shorten a link with a custom domain in bit.ly

Shorten a link with a custom domain in bit.ly


Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /home/public/kirby/toolkit/lib/str.php on line 506

Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /home/public/kirby/toolkit/lib/str.php on line 506

I got a short domain recently to spam your Twitter and App.net 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:

#!/usr/bin/python

import requests
import json
import os

longURL = os.environ['KMVAR_url']

# Get your oauth token at https://bitly.com/a/oauth_apps
oauth = 'INSERT YOUR OAUTH TOKEN HERE'

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

endpoint = 'https://api-ssl.bitly.com/v3/shorten'
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.