Create an Alfred Workflow: the super simple URL shortener

Creating a plugin to shorten URLs from inside Alfred.

Setup the basic Alfred workflow

If you don't have Powerpack for Alfred you might want to get it before you start, as none of this will work without it. Open Alfred Preferences, go to Workflows and hit the + button at the bottom of the list:

Alfred preferences screen: workflows tab with add button highlighted
Alfred preferences screen: workflows tab with add button highlighted

For this workflow, we can start with an "Essentials" template: "Keyword to script to notification":

Menu navigation to basic templates
Menu navigation to basic templates

Now we need to do some basic setup of our workflow. It needs:

  • a name
  • a description
  • a category (optional)
  • bundle id - this is usually your url backwards (tld.domain) followed by the name of the app (shorten)
  • created by - you
  • website
Setup workflow popup
Setup workflow popup

Once you've done that you should have this:

Visual display of the workflow after the basic setup has been completed
Visual display of the workflow after the basic setup has been completed

Do some configuration

Keyword

Double click the keyword section, you'll get this:

Keyword setup dialog
Keyword setup dialog

You need to set a few things up before your workflow will do anything:

  1. pick a keyword - try to make it one you haven't used before (I used "short")
  2. leave the tick in place for with space
  3. choose keyword required (you don't want to send a blank input)
  4. Add a title: Shorten a URL
  5. Add a description of what your workflow does: Use your bit.ly account to automatically shorten a URL
  6. Add an icon if you wish.

Once you're happy hit Save. Don't worry if you want to change things later you can.

Run script

When you double click on the run script section you should get something like this:

Basic script screen
Basic script screen

If you click on the language dropdown you will see that there are a variety of languages you can use, including an option to type python straight into the script box: usr/bin/python we're not going to do that however, we're going to leave the language as bin/bash and put python shorten.py {query} in the script box. Make sure you tick all the escape options along the bottom except backslashes.

Outputs

Once the script has run you want it to copy the shortened URL to the clipboard so that you can use it. Ideally you also want it to tell you that it has done so. If you double click on Post Notification you will see that you have a variety of options to setup:

  • Title: I like it to show me the URL as the title so I put {query} in this box
  • Text: You can give a bit more detail about what the script is doing here.
  • You want to check "Only show if the input has content" to make sure it doesn't flash clipboard copying messages if something has gone wrong.
Output setup dialog
Output setup dialog

Unfortunately this doesn't actually copy the output to the clipboard... It just tells you that it has. In order to actually copy the output to the clipboard you first need to add an output. There are two ways of doing this you can:

  1. use the contextual menu (right click) to add an extra output to your workflow:

    contextual menu: copy to clipboard

    That will bring up a pop-up asking you to tell it what you want to copy to the clipboard. The default settings for this are fine so hit save:

    settings for copy to clipboard popup

    Once you've done that you'll have an orphaned output, which you will have to link to the script by clicking on the node that connects the script to the notification and dragging a link to your new output.

  2. click on the node that connects your script to the notification output and drag out a new connection, when you let go it asks you what you would like your new connection to do:

    new output link

Create the file structure

In order to do this we first need to reopen one of the elements (they all have this button in them) and hit the highlighted button to take you to the folder containing the workflow:

Element of the workflow highlighting the "open in finder button"

You can also right click on the name of the workflow and use the open in Finder option if you prefer. Right now there should only be one file in the workflow folder: info.plist A lot of what we're going to do in the python script relies on the Alfred-Workflow helper library. You need to download the latest stable release and copy the unzipped files into your workflow folder. Make a file named shorten.py in the same folder as info.plist.

Lets write some python

Log yourself into bitly.com and grab your API key It's very simple to shorten a URL using bitly's API all you have to do is call the following URL: https://api-ssl.bitly.com/v3/shorten?access_token=ACCESS_TOKEN&longUrl=LONG_URL Of course the URL does have to have http:// or https:// in front of it therefore you need to check for that first and add http:// if it's not there (most sites with https have a redirect from http so you are fairly safe just making everything without the protocol http). Once you're sure your long URL is complete all you need to do is construct the API call and extract the shortened URL from the returned JSON. Printing the result outputs it to the Alfred Workflow.

#!/usr/bin/python
# encoding: utf-8

import sys
import json
from workflow import Workflow, web API_KEY = "0000000000000000000000000000000000000000"

def main(wf):
    query = wf.args[0]

    if "http" not in query:
        query = "http://" \+ query url = "https://api-ssl.bitly.com/v3/shorten?access_token=" \+ API_KEY + "&longUrl=" \+ query
        r = web.get(url)
        result = r.json()
        print(result['data']['url'])

if __name__ == u'__main__':
    wf = Workflow()
    sys.exit(wf.run(main))

Now, when you type short followed by a long URL into Alfred, it will supply you with a Bitly link for your url which is tied to your Bitly account :).

Comments powered by Talkyard.