Opening Source Codes in Textastic

Hello there! I wrote a new version of this script that skips Pythonista. You should check it out here.

Textastic recently added a method to open the source code of any web page you're viewing in the browser right on the app. It involves changing the "http://" from the url to "textastic://" in the address bar manually. Wish I could use a bookmarklet to do it, but how to replace the values? Let's call Pythonista and see how it can help us.

First, let's create a document in Pythonista called Source2Textastic. Then paste the following code:

from urlparse import urlsplit
from webbrowser import open

textastic = 'textastic://' + ''.join(urlsplit(sys.argv[1])[1:])


Allow me to explain a little bit. sys.argv[1] is the value brought from the external app via URL scheme. Since it is supposed to be an url, we're going to use urlsplit to break it into its parameters and place them in a tuple. Since the part we want to drop is exactly the first item of the tuple, we can join the url again and avoid the netloc in the way. This will generate your whole link, missing only your http:// or https://. Then we append the textastic:// to the url and voilá, you just gotta open it in the internal browser, as we do with open(textastic).

The final touch is, of course, finding a way to get the data inside Pythonista. This can be achieved with a bookmarklet. If you don't remember how to create one, bookmark any page and change its url to the following code:


Just activate this bookmarklet and the source code from your current url will open in Textastic. That's all for the day.