URL Scheme and x-callback-url
- brianpartridge170
Is there any chance of extending Pythonista's URL scheme to support x-callback-url like behavior?
Basically, I'd like to be able to launch an app from Pythonista, while Pythonista sits and waits for a response. Once the response comes, the script in Pythonista could resume and handle the callback by a method in the script.
Or is it just not possible because the running script dies as soon as the app enters the background?
I ask because I'm interested in the possibility of more scriptable iOS applications and Pythonista seems like a great engine for scripting workflows between apps. Imagine being able to launch an app (A), which returns info back to Pythonista, letting my script process the returned data, and then launch into another app (B) based on information gleaned from the previously launched app, and repeat with other apps (C, D, etc) until a workflow is complete.
(This looks possible if app A supports x-callback-url by using multiple Pythonista scripts, but the problem is that you loose all context that the script had as soon as A launches.)
Any thoughts, on this or other possibilities?
- C0deH4cker
If the only issue now is that the script's state isnt kept, you could just pickle everything before you launch app A and then unpickle it for the callback.
- pudquick51
You can do it - to a certain extent. I'll describe how, first, and then correct a possible misunderstanding I think you might have.
First and foremost - Pythonista doesn't support x-callback-url protocol <i>directly</i> ... But you can <b>almost</b> fake it.
The format for real x-callback-url support is:
pythonista://x-callback-url/actionName?[parameters]
For Pythonista, as it currently is, you'd have to do:
pythonista://x-callback-url?action=run&args=[encoded actionName + parameters]
... And then write a connecting script named 'x-callback-url.py' to intercept and interpret it.
Unfortunately, this is just different enough from the standard that it pretty much breaks it unless the developer coded specific Pythonista-style support for it.
Which brings me to the 'misunderstanding' part: you do know that x-callback-url support has to be implemented by the developer of whatever app it is you're using that was published on the AppStore, right?
Apple, for instance, doesn't do / use it - so there's no way to switch to Calculator.app, do some math, then return the result to Pythonista.
In fact, I don't really know of any apps (that are well-known) that implement it.
You'd be better off with a web app - but if you're going that route, Pythonista can directly do the web interaction - no need for x-callback-url.
I'm just really really really not sure what a useful example workflow would be for what you're trying to do here.
- brianpartridge170
Pudquick, thanks for the info, I had thought about the encoding and launching from another script, but it seems like too much of a hack to bother with.
x-callback-url is definitely not widely used, which is a shame, it's a bit like AppleScript support in that not many people use it, so it just doesn't get implemented. Without giving too much away, I work on a suite of apps for enterprise user's that may use it. I'd like to give the more power users among them a way of scripting workflows and Pythonista seemed perfect for that.
- pudquick51
Well, since you're the developer of the app(s) in question that would be working with Pythonista, you can do that modified Pythonista-friendly form of x-callback-url protocol in your iOS app and then provide the helper glue 'x-callback-url.py' script to your users.
The only trick I'd warn you about, however, is that the action=run Pythonista URL handler only works if a script is not already running.
So if a Pythonisa script calls out to an app to do work and you want to come back with the results to Pythonista, your call out should use sys.exit(0) immediately after (it'll trigger while Pythonista is moving into the background and end your script).
I'd recommend a state-saving technique, like pickling or saving objects to a DB, prior to calling out - and passing a SessionID-like value to your enterprise app along with the data/arguments.
Then your enterprise app, when it calls back to Pythonista with the results, can return the SessionID, which will allow your script in Pythonista to pick up where it left off - and start interpreting the new data.
I can work up an example if it helps.
@pudquick could I take you up on your offer to work up an example?!? talks about a really interesting idea of Pythonista launching a barcode scanning app that supports x-callback-url and getting the barcode content sent back into a Pythonista script. The first part was easy but I could not get the second part to work. Ideas?
Using pic2shop (it is strictly speaking not using the x-callback-URL mechanism, but works just fine), save the following as 'pic2shop' (without the quotes):
<pre>
import webbrowser
import sys
if len(sys.argv)>1:
print 'Barcode:'+sys.argv[1]
else:
webbrowser.open('pic2shop://scan?callback=pythonista%3A//pic2shop%3Faction%3Drun%26argv%3DEAN')
</pre>
Late to this party, but here's a slightly more complete version that does QR codes too:
import webbrowser import sys # From: # # pic2shop always scans UPC-A, UPC-E, EAN13, EAN8 and QR, one cannot restrict to some formats # pic2shop will replace "EAN" by barcode or "QR" by QR code if len(sys.argv)>1: #print 'args: ' + str(sys.argv[1:]) if sys.argv[1] != 'EAN': print 'Barcode: ' + sys.argv[1] elif sys.argv[2] != 'QR': print 'QR: ' + sys.argv[2] else: print 'Scan not recognized' else: webbrowser.open('pic2shop://scan?callback=pythonista%3A//scanner%3Faction%3Drun%26argv%3DEAN%26argv%3DQR')
Save as "scanner" or change the callback URL; also pic2shop doesn't seem to fully escape the QR result ('&' causes trouble, for one)