January 28, 2014
Yesterday, John Gruber released an update to his Make Bookmarklet Perl script. The script performs the following functions, as stated on Daring Fireball:
Bookmarklets are a key ingredient to a few of my Safari workflows on iOS. Most notably, when I want to link to a post on Unapologetic. I copy the text I wish to quote on the webpage, then run a bookmarklet which launches Drafts and creates a new draft with the title of the webpage and the URL formatted as a link in Markdown, and the copied text formatted as a Markdown blockquote. I have a few other bookmarklets too which I use less often, but the one thing they all have in common is that they were exceedingly annoying to manually create on my iPad.
I hadn't known about Gruber's script until I saw its update yesterday, but when I saw it I knew immediately that it was a function I wanted to be able to have access to on my iPad, not just my Mac. So, I set to work converting Gruber's Perl script into a Python script that can be run from Pythonista on an iPad or iPhone.
[YOUR CODE HERE], in case the code you paste in begins or ends with a single quote, which would break the three single quote comment block method being used to set the code as a string in the Python script. Leaving a space on either side will not affect your bookmarklet, as line-leading and line-trailing whitespace is removed by the script anyway. (▼)(▲)If you run the script using the Drafts action or Editorial workflow you do not have to worry about leaving spaces.
Using Gruber's example from DF, if you were to send the Make_Bookmarklet python script the following code:
var str = document.title; alert(str);
It would result in this code being printed to the Pythonista prompt page:
And this bookmarklet being copied onto your clipboard:
Since there is no main document like a BBEdit text file for the code to be placed on, I just have Pythonista print it so that you can copy and paste it anywhere you wish. The bookmarklet itself will be on your clipboard when the script completes, so you can go straight to Safari (or whatever browser you use), bookmark the first page that comes up, then change that bookmark title and paste your bookmarklet in for the URL.
My script also mimics Gruber's in that if you run it on code which already has a commented out bookmarklet on the first line (i.e., the first line contains
Gruber set his script up to not encode every character, making the resulting bookmarklet more readable. Mine encodes a little bit more than his, but leaves some of the most popular characters unencoded. If you want encode everything you possibly can, just remove the characters in the
safe= parameter of the
urllib.quote command on line 31. If you want to encode less characters, add those you don't want encoded into that same parameter.
Source Code to Make_Bookmarklet Script for Pythonista
Running the Script With Code Sent From Drafts
[[selection]] tag will act identically to the
[[draft]] tag, sending the whole draft as most other actions do. (▼)(▲)Hat tip to Charles Bucher
Direct Import Link for "Make Bookmarklet" Drafts Action
Running the Script With Code Sent From Editorial
To run the script from Editorial, import the workflow below. Instructions are identical to those for Drafts: the Pythonista script must be named "Make_Bookmarklet" and the code sent will either be whatever text you have selected in Editorial or the entire document if you have no selected text.
Direct Import Link for "Make Bookmarklet" Editorial Workflow
If you see any errors or have suggestions for the script, please contact me with them.
Tweaked script so it senses whether it is being executed with a
sys.argv argument (i.e., from an external source like Drafts or Editorial or from holding down the run button and typing code manually in Pythonista). If it is then it will use that argument for the
source_code parameter, otherwise is will use the code in the
[YOUR CODE HERE] slot.