Pythonista, finally programming workflow automation for iOS?
I’m doing most of my writing on my iPad or occasionally on my iPhone, and I write everything in Markdown. There are some great scripts in my current favorite language Python out there for doing Markdown conversions, image manipulations, etc., but unfortunately, Apple rules regarding programming apps on iOS devices are pretty stringent. But an app for programming Python on iOS called Pythonista have some smart ways of getting around some of Apple’s restrictions. And one of the wonderful things you can do is home screen shortcuts, making your scripts available directly from the home screen of your iOS device. All you have to do to create a Pythonista home screen shortcut is to go to a web page and enter the script name and optional arguments. After that, add the page home-screen with Safari’s bookmarks button. Another way of starting your scripts is directly as bookmarks in Safari or another web browser.
I found the app from this article about making life easier by scripting with Pythonista, a great read of all the functions available in the app.
Because they added a Python library for managing the clipboard, you can copy pictures or text to it, then start your script that reads from the clipboard and then put the new modified text/picture back to the clipboard, ready for pasting into any app. Very handy!
Pythonista has an URL Scheme so that it can be called from other programs. This makes it perfect for apps on the iPhone like Launch Center Pro on iPhone that supports URL Schemes to start your Python scripts.
With more and more apps supporting handleOpenURL, the things you can do starts to look very handy indeed.
Applications that support the x-callback-url specification makes it possible to send information to one app and get the results back and then continue to process it or sending it to yet another app. Hopefully, more apps will support x-callback-url, but some great ones already do.
- Pythonista (of course!)
- Google Chrome
- Scanner Go
- iCab Mobile
So far I own nine of these applications, four of them bought just because they support x-callback-url, so listen up developers!
To work with the editor is a treat! Fast, responsive and very smart. It’s straightforward to use. Most of my editing has been modifying existing scripts and changed them so I can use the clipboard. But you can make more extensive programs. There’s a built-in interactive prompt where you can try out commands and test. Great for trying out a concept or try to find a bug. It’s also handy for people like me who just started to learn Python and need to try out stuff.
There is an extended keyboard with the most used keys you need when programming Python. You can move the cursor in the text by just sliding your finger from back and forth across the extended part of the keyboard, and the cursor moves accordingly. Very clever!
It includes all the standard libraries for Python, but the developer of Pythonista has also created some handy libraries for things specific to the iPhone/iPad
- canvas — Vector Graphics
- clipboard — Copy and paste
- console — Functions for working with the text output and keyboard input
- editor — Functions for scripting Pythonista’s text editor
- keychain — Secure Password Storage
- scene — 2D Graphics and Animation
- sound — Play simple sounds
Other handy libraries included are:
- bs4 — BeautifulSoup 4
- feedparser — Universal Feed Parser
- Python Imaging Library
- Requests – HTTP for Humans
So there are plenty of libraries available, and more are coming.
Here you get a couple of examples of how it works and what you can do.
- An interactive example of using the built-in web server in Pythonista to create MultiMarkdown tables.
- Here’s a GitHub repository with a lot of examples, mostly related to Markdown, but also some with image manipulation because Pythonista contains not only the standard Python libraries but also the Python Imaging Library, making it excellent for doing automatic image manipulations.
- I’ve written a little snippet for posting directly from Mobile Safari to the Poster app on my iPad or iPhone.
I hate that I can’t look at the HTML code when surfing on Mobile Safari, so here’s a little script for using Pythonista to display the HTML source, nicely formatted for easier reading.
A simple script for showing the HTML code of a page directly from mobile Safari.
| | | |
| | __ _ ___| | _____ _ __
_ | |/ _` |/ __| |/ / _ \ ‘_ \
| || | (_| | (| < __/ | | |
____/ __,|___||____|| ||
from bs4 import BeautifulSoup
numArgs = len(sys.argv)
if numArgs != 2:
console.alert(‘This script needs an URL as an argument.’)
url = sys.argv
usock = urllib.urlopen(url)
data = usock.read()
soup = BeautifulSoup(data)
The major problem I have with the app is the way to import scripts into it. Most of the blame goes to Apple for not allowing importing of scripts from Dropbox or by other means. Apple wants to keep the iOS out of the troubles of malicious code execution, and that’s understandable, but the apps are already jailed, so they can’t access files outside its own folders. But I guess the more avenues you give for a hacker, bad stuff will happen.
For now, I have a folder in Dropbox with the scripts I find or develop on my Mac, and then open them with a text editor app and paste them into Pythonista.
It’s nice to finally have the ability to script tedious and repetitive tasks on iOS. With more libraries being added, this app will become even more useful. It’s also a fun and convenient way of learning Python, a great programming language!