Open Tweet Links in Tweetbot with LaunchBar

I really went off the rails with this one.

The morning started with experimenting in Keynote to see if it had the tools I needed to make some animations for a video project I’m putting together. Before too long I discovered a bug with the Magic Move feature when applied to two text slides. Looking for a solution led me to this old tweet from Jake Wharton. The tweet didn’t offer a solution, but it was nice to see that he ran into the exact same problem I had. After I came up with a hacky fix, I wanted to ask Jake if he ever found a good solution.

Finally, the meat of this post!

No way I was going to use the Twitter website to post my reply to Jake’s tweet. I’ll do it in Tweetbot for Mac, thank you very much. But how to open this specific tweet ID in Tweetbot? There doesn’t seem to be a way to punch a Tweet ID into Tweetbot. But there is a Tweetbot URL scheme that will accomplish the goal. All I had to do was replace the https://twitter.com/ portion of the tweet’s URL with tweetbot:// and that tweet would open up in Tweetbot. Brilliant!

Enter LaunchBar

Rather than get back to what I was supposed to be doing this morning, I figured there had to be a way to use LaunchBar to make this task easier. In other words, how could I have LaunchBar do the following:

  1. Take in a URL for a specific tweet
  2. Confirm that it is a valid tweet URL
  3. Transform the URL to Tweetbot’s format
  4. Open the transformed URL in the default browser (which in turn would open the tweet in Tweetbot)

Turns out, once you dig into the LaunchBar Action docs a bit, it is pretty simple. So let’s walk through it.

Making a simple LaunchBar Action

  1. Open the LaunchBar Action Editor (Hint: use LaunchBar to open the Action Editor)
  2. Give it a name such as Open Tweet URL in Tweetbot (Bonus: set the Action Icon to com.tapbots.TweetbotMac)
  3. Create a new Action (the + button in the bottom left of the window)
  4. Set JavaScript as the language for the Default Script.
  5. Check the boxes for Requires argument and Accepts string argument
  6. Edit the default.js file (the Edit button in the upper right of the window)
  7. Replace the template code that LaunchBar gives you with the following:
function run(argument) {
  var regex = /^https?:\/\/(www.)?twitter.com\//
  // make sure that we passed in something, ANYTHING, to the action
  if (argument == undefined) {
    LaunchBar.alert('No argument was passed to the action');
  }
  // make sure we passed in a valid Twitter URL
  if (argument.match(regex) != null && argument.match(regex).index === 0) {
    // replace the `https://www.twitter.com/` with `tweetbot://`
    argument = argument.replace(regex, "tweetbot://")
    // now just tell LaunchBar to open the tweetbot URL
    LaunchBar.openURL(argument);
  } else {
    LaunchBar.alert('It appears that\n`' + argument + '`\nis not a valid Twitter URL')
  }
}

That’s all there is to it. A little regular expression magic and knowing how to tell LaunchBar to show alerts and open URLs is all this takes. This is far from a robust script. If you pass it a valid URL to a specific tweet in Twitter, such as https://twitter.com/eirkeirkeirk/status/725185945465556992, it’ll work. But it’ll choke and silently fail with a lot of other types of twitter.com URLs.

It’s on GitHub

So you can grab the .lbaction package from there instead of building it yourself.

A new discovery

lb-action-editor

If you have this Action installed, you can just send a tweet URL directly to Tweetbot itself and it will open. The magic is in the Runtime Behavior > Associated Application field. Because it’s set to com.tapbots.TweetbotMac, LaunchBar will pass the tweet URL on to this Action if you send the URL directly to Tweetbot. 🔥🔥🔥