I want to be able to share interesting web pages on my WordPress blog using iOS directly from Safari using a bookmark and a Pythonista Python script. The script takes the URL of the page, any text you copied and the page title, formats it in Markdown and creates a new post in Poster App automatically.
So here you can find the script and how to set it up. I’m using a WordPress plugin called PHP Markdown Extra, because I want all the posts to be in Markdown, not in HTML on the server. It’s so much easier to go back and edit posts, and I’m using [MarsEdit on my Mac and Poster App on my iPhone/iPad to edit posts. Both of them supports editing in Markdown, which is great.
Javascript Bookmark in iOS Safari
First of all we need a way of getting the URL from the web page, the title of the article and some descriptive text. So first you need to add a bookmark in Safari on your iOS device that will use Javascript to extract the information and send it to Pythonista for formatting and then have the formatted text sent to the Poster App.
Open Safari, and go to any page and press the share arrow and select Bookmark.
Give it a name, I used “To Poster App” but you can call it anything you like. Make sure it’s in the Bookmarks Bar for easy access and press the Save button.
Now we need to edit the bookmark and insert some Javascript that sends the necessary data like URL and the page title to Pythonista. Press the Bookmarks icon in the status bar in Safari and navigate inside the Bookmarks Bar. Press the Edit Button and press on the bookmark we just created.
Select the Javascript code below and paste it into the URL field below the name of the bookmark, replacing whatever was there before.
[cc lang=”javascript”]
javascript:window.location=’pythonista://posterlink?action=run&argv=’+encodeURIComponent(document.title)+’&argv=’+encodeURIComponent(document.location.href);
[/cc]
Adding the posterlink.py script to Pythonista
Now we need to add the script in Pythonista that’s been called from the Javascript code above. Copy the script below on your iOS device, open the Pythonista App and create a new script called posterlink and paste the code. If you prefer another name for the script, remember to change the name in the Javascript code as well.
[cc lang=”python”]
# A simple script for creating a post in Poster App using Pythonista directly from an iOS browser.
# add a bookmark in the browser with the JavaScript code:
# javascript:window.location=’pythonista://posterlink?action=run&argv=’+encodeURIComponent(document.title)+’&argv=’+encodeURIComponent(document.location.href);
#
# Copy text of interest on page, and click bookmark. A new Markdown post
# in poster will be created.
# _ _
# | | | |
# | | __ _ ___| | _____ _ __
# _ | |/ _` |/ __| |/ / _ \ ‘_ \
# | |__| | (_| | (__| < __/ | | |
# \____/ \__,_|\___|_|\_\___|_| |_|
import urllib
from urllib import urlencode
import sys
import webbrowser
import clipboard
import console
numArgs = len(sys.argv)
print numArgs
if numArgs != 3:
console.alert('This script needs two arguments. Title, URL and text in clipboard.')
else:
title = sys.argv[1]
url = sys.argv[2]
clip = clipboard.get()
text = clip.encode('utf-8')
complete = """[{title}]({url}):
> {text}”””.format(title=title, url=url, text=text)
strlist =[]
strlist.append(‘posterapp://create?text=’)
strlist.append(urllib.quote(complete, safe=”))
strlist.append(‘&title=’ + urllib.quote(title))
actionurl = “”.join(strlist)
print actionurl
webbrowser.open(actionurl)
[/cc]
Using the script
You should now be able to go to any page you want to do a blog post about, select some descriptive text and choose copy, then press the bookmark in the Bookmark Bar. This will launch Pythonista, which will format the text in Markdown syntax and send it to Poster App. The text you copied will be formatted as a quote. Just add your own text or comment and publish. A quick way of sharing a link to something interesting on your WordPress blog, directly from Safari.
Ian says
How can I get the code from that window on iOS?