How to tweet via LaunchBar

Ever since Twitter’s official Mac client started forcing the auto-shortening of links, I’ve had to launch a second client (in my case, Echofon) to publish tweets that contained them. (Sometimes shortened links are OK, even necessary, but for the most part I like to leave them as is, because they can provide context to the tweet/link, and give potential clickers a better indication of what may be found at the link, who the author may be, etc.)

As you can imagine, this was beyond annoying, and so I set out to come up with a solution that would require only LaunchBar, at least as far as the interface was concerned.

I remember coming across something a while back that likely would have worked, but required a third party, through which your Mac talked to Twitter. No thanks. I also happened across a second potential option, but if memory serves it hadn’t been updated to support Twitter’s OAuth stuff, and so it no longer worked. At that point I’d kind of resigned myself to either solving the problem from scratch or just letting it go entirely, and had stopped actively looking for a solution.

Then, just a couple of days ago, TTYtter–a full-blown Twitter client for the shell (written in Perl)–flew onto my radar. The whole project is kind of insane, but that didn’t mean it wasn’t exactly what I was looking for. Once I realized it didn’t have to be run as a daemon, but could instead could be executed with options that told it essentially to tweet and quit, I knew the perfect solution was within my grasp.

After considering a couple of options, I decided to whip up an AppleScript (see below) that would let me tweet, via TTYtter, whatever I typed into LaunchBar’s text field. (Yes, you could use the Run Shell Command available in LaunchBar 5 beta 2, but you’d have to type /path/to/ttytter.pl -status= each time. Sure, that could be made a bit easier with TextExpander, but it’s still less efficient than my AppleScript method.)

on handle_string(tweet)
    do shell script "/path/to/ttytter.pl -status=\"" & tweet & "\""
end handle_string

Setup is simple:

  1. Download TTYtter.
  2. Make it executable, and place it wherever you’d like. (I’m keeping it in ~/bin).
  3. Launch it from a shell, and follow the on-screen prompts to get your OAuth keys from Twitter. (This is the only time you’ll interface with TTYtter directly.)
  4. Save the AppleScript to ~/Library/Application Support/LaunchBar/Actions. You can give the script whatever file name you want, but keep in mind that the file name will be the alias used to specify the script from within LaunchBar. For example, I saved my script as tweeter.scpt, and so to set it in motion I type (or rather start to type) tweeter from within LaunchBar.

And you’re done.

Now, whenever you want to tweet via LaunchBar, simply highlight the script, hit the space bar, type out your tweet and press return.

Update: Soon after I published this piece I added functionality to the script to alert you when the draft tweet was longer than 140 characters, but never got around to updating this post. Then, a couple of days ago, Alan Schmitt wrote me to let me know he extended my script to show, for example, whether the tweet was sucessfully published. (He also added a character-count validator; mine tells you by how many chars you’re over the limit, and his tells tells you the total chars).

His additions require a positive action from the user even when the tweet is published sucessfully. I didn’t want that, and so I modified the code a bit (using display alert instead of display dialog) so that a positive action is required when the tweet is too long (i.e., you must dismiss the alert), but no action is required to dismiss any publishing feedback (i.e., the pop-up alert goes away after, in my case, one second).

Given the way LaunchBar works, I know of no way to get back to the text field after the alert pops up to tell you the tweet’s too long (i.e., once you click Dismiss on the alert box, the alert and LaunchBar disappear). The good thing though is that LaunchBar remembers what you last typed in the text field, and so once the script is invoked again you can immediately start shaving off chars.

The below script is what I’m using now.

on handle_string(tweet)
    if length of tweet ≤ 140 then
        set theResult to do shell script ~/bin/ttytter.pl -status= & quoted form of tweet &  2>&1
        display alert theResult buttons {Dismiss} default button 1 giving up after 1
    else
        set charsOver to (length of tweet) - 140
        display alert (You're  & charsOver &  chars over) buttons {Dismiss} default button 1
    end if
end handle_string