Convert Twitter.com URLs to Tweetbot Links

I communicate with my team through iMessage. We’ve tried many “communication services” over the years, yet, since last Fall, we’ve always come back to Apple’s solution. It’s not perfect for us, its reliability is far from 100%, but it works.

As we keep using iMessage every day, there’s one category of “media” we’re constantly sharing: Twitter URLs. We find some cool piece of information or news on Twitter, we share it with the team. Linking back to tweets has, in a way, become our favorite type of commentary for fun, news-hunting, and everything in between.

Twitter.com URLs, though, aren’t the best way to jump back to a tweet, especially when you’re on a mobile device. When you’re on a Mac, clicking on a Twitter link will open a new browser tab, which doesn’t really bother us as we’re used to opening background tabs on our computers. But on the iPhone and iPad, it can become annoying: there’s a limit of 8 Safari tabs on the iPhone, you get yanked out of Messages, and, most of the time, mobile.twitter.com URLs just don’t work. In our team chat, we’ve speculated the “Not Found” errors we’ve seen may be related to how Tweetbot generates Twitter URLs when you hit “Copy Link to Tweet”: instead of using status in the URL slug, it uses statuses, which seems to be the reason behind erroneous redirecting on mobile devices.

We’ve come to the conclusion that we want to be able to easily copy twitter.com URLs and turn them into links based on Tweetbot’s URL scheme. Using a simple tweetbot:// URL, you can use Twitter’s status ID – the same you receive when you copy a link – to open a single tweet directly in Tweetbot. And the best part is, the same URL scheme works consistently across Tweetbot for iOS and Tweetbot for Mac. As everyone on the MacStories team is already using Tweetbot, the solution seemed obvious – plus: no more browser tabs.

The problem was finding a way to convert twitter.com URLs easily, without having to remember complex combinations of keystrokes and commands. Furthermore, as I promised my team I’d come up with a way, I had to figure out a solution to do text conversion directly on iOS.

As a result, I’ve come up with an AppleScript, a Keyboard Maestro macro, and a simple Python script to transform Twitter URLs into their Tweetbot counterparts.

AppleScript

In AppleScript, I had to use text item delimiters to identify Twitter-specific strings to replace in the text.

Remember, we want to turn twitter.com into tweetbot://, and statuses into status, because that’s how Twitter’s website likes links. Then, we want to be able to easily paste back the modified text so we can share it with someone, or just open the new URL.

Unfortunately, to make the process as automatic as possible, I had to settle on GUI scripting. Writing AppleScript commands that rely on visible user interface elements is never advised because a) menu and button names often change with app updates and b) there may be problems with localized apps (menus have different names). However, Tweetbot doesn’t offer an AppleScript dictionary yet, and the “Copy Link to Tweet” action we’re using doesn’t have an associated keyboard shortcut. Which means we have to simulate clicking an item of a submenu of a menubar dropdown. Not the best solution, but it works. I look forward to improving this if Tweetbot ever gets AppleScript support.

tell application "System Events"

set AppleScript's text item delimiters to ""

-- MAKE SURE "ENABLE ACCCESS FOR ASSITIVE DEVICES" IS ENABLED

tell application "Tweetbot"

activate

tell application "System Events"

tell process "Tweetbot"

click menu item "Copy Link to Tweet" of menu 1 of menu item "Share" of menu 1 of menu bar item "Tweet" of menu bar 1

end tell

end tell

end tell

-- DELAY TO IMPROVE RELIABILITY OF COPY

delay 1

set my_URL to the clipboard

--FIX THE URL

set the clipboard to my replaceText("https://twitter.com/", "tweetbot://", "statuses", "status", my_URL)

end tell

(*

======================================

// UTILITY SUBROUTINES

======================================

*)

on replaceText(searchStr, replaceStr, searchStr2, replaceStr2, my_URL)

-- SET VALUE FOR OLD DELIMITER

set oldDelim to AppleScript's text item delimiters

-- CHANGE TWITTER TO TWEETBOT URL

set AppleScript's text item delimiters to searchStr

set this_text to (text items of my_URL)

set AppleScript's text item delimiters to replaceStr

set bot_URL to (this_text as string)

-- FIX SLUG ('STATUSES' TO 'STATUS')

set AppleScript's text item delimiters to searchStr2

set this_text2 to (text items of bot_URL)

set AppleScript's text item delimiters to replaceStr2

set fixed_URL to (this_text2 as string)

-- CHANGE BACK TO ORIGINAL DELIMITER

set AppleScript's text item delimiters to oldDelim

return fixed_URL

end replaceText

As usual with quick script I’ve put together, I haven’t added a check for assistive devices. Make sure the option is enabled in System Preferences > Accessibility.

Once I automated the part to copy the Twitter link, I needed to convert that link to the tweetbot:// format with minimal effort. Using text item delimiters and a handler, the script can identify the portions of the URL we don’t want, and replace them. The “fixed” URL is then placed on the clipboard.

The provided AppleScript works well with Alfred. Create an extension (or download this one), and assign a hotkey to it. Then, in Tweetbot, select a tweet, press the hotkey, and a tweetbot:// link to the tweet will be on your clipboard after a second. Thanks Justin for input and feedback.

Keyboard Maestro

Keyboard Maestro comes with a “Search and Replace Clipboard” action that fits our needs for this workflow. Once again using GUI scripting, I created a macro (and later, our Gabe Glick improved on it) to copy a tweet’s link and turn it into tweetbot:// format.

The macro is smart in that, if Tweetbot is at the front, it’ll interpret the hotkey as a command to copy a selected tweet and convert the URL; if Tweetbot isn’t the frontmost app, you can use the same hotkey to paste. I personally use this over AppleScript, as I can quickly generate a link, then switch back to Messages and hit the same hotkey to paste.

Download the macro here.

Pythonista

I’m new to Python, but I’ve been learning the basics in the past few weeks and I’ve been playing around with the fantastic Pythonista app on my iPhone and iPad. I’ve therefore created a simple Python script to replace occurrences of twitter.com (and its mobile and https variations), `print the result, and launch the new URL directly in Tweetbot.

import clipboard

import console

import webbrowser

mytext = clipboard.get()

mytext = mytext.replace('https://twitter.com/', 'tweetbot://')

mytext = mytext.replace('statuses', 'status')

mytext = mytext.replace('http://twitter.com/', 'tweetbot://')

mytext = mytext.replace('http://mobile.twitter.com/', 'tweetbot://')

mytext = mytext.replace('https://mobile.twitter.com/', 'tweetbot://')

console.clear()

print mytext

webbrowser.open(mytext)

Once configured, it takes a few seconds to launch Pythonista, hit Run, and have the new URL ready in the output. Python users will have no problems modifying the script to, say, set the result to the clipboard.

Wrap-Up

I like to find automated solutions for common problems that no one is fixing. Having to deal with Twitter.com tabs as a Tweetbot user was one of those problems, and I’m glad I’ve set up a system that improves my link-sharing workflow.

It’s easy to modify the scripts and macro to work with other URL schemes, such as Twitterrific’s. As usual, ping me if you’re inspired by this to build something new.