Forum Archive

Using Workflow app with Pythonista

ihf

I'm just beginning to play with the new IOS Workflow app and trying to understand how it works with Pythonista. I was trying to see if I could create a WF which simply ran a script and spoke the output (obviously I don't need WF to do this but it is simply a way to test how data can flow between the apps. I must be missing something elementary but if my script produces prints text on the console, how do I access that in WF? Also, it would seem that WF can't call a script without requiring that the user manually return to WF after the script executes, is that correct?

If others have written any scripts or WF that interact, it would great if you could post them.

ihf

OK, using the clipboard (as opposed to writing to console) brings the output back to WF. However, it still requires the manual action of returning from Pythonista to WF. I guess that is unavoidable?

0942v8653

Here's a little script to extract the workflow URL from the Add to Home Screen page:

import sys, clipboard, base64, re, console

orig = sys.argv[1] if len(sys.argv) > 1 else clipboard.get()
data = orig.replace('data:text/html;base64,', '')
page = base64.b64decode(data)

match = re.search('.*<a id="jump" href="([^"]+)">.*', page)

clipboard.set(match.group(1))
console.hud_alert('Workflow URL copied.')

And a companion bookmarklet:

javascript:location.href='pythonista://Get%20WorkflowApp%20URL.py?action=run&argv='+encodeURIComponent(location.href);

You can use this and the clipboard, but unfortunately you're going to have to split your workflow in two. I suppose it may be possible to use just workflow000000:// to return to Workflow?

dgelessus

Just got Workflow myself (and it looks extremely promising) and wrote a quick test workflow/script pair. I get a text input from WF and pass it to a py script, which prints it out with the regular print statement and no further settings. However the output does not show in Pythonista's console, but in WF once I switch back to it. No idea what you're doing differently, but it seems fairly straightforward to me...

0942v8653

dgelessus: The Run Script action in Workflow passes its input to its output unchanged. Your script is not running at all, I guess…

ihf

@dgelessus That's interesting. When you say the print output shows up in WF, how are you accessing it in the workflow? Also, I realized that I could get the script to go back to WF by just opening the url workflow://.

0942v8653

My conclusion is that Workflow.app's pythonista action is very very broken and we may just have to wait until it is fixed. webbrowser.open("workflow000000://") does work for switching back to the app though.

dgelessus

@0942v8653, just noticed that as well. For whatever reason the script indeed is not actually run at all, neither a print("something") nor console.alert("something") show up. (I'm using Beta 1.6 of Pythonista btw.)

userista

I filed a bug with Workflows - the bug occurs when passing anything to Pythonista via Workflows - the Pythonista script will not run. Looking on Twitter seems like it's a widespread bug.

dgelessus

OK, I experimented with a few things. x-callback-url is not an option due to Pythonista not actually supporting it. Giving Workflow a Pythonista script run URL as the x-callback-url doesn't help either, because the callback part starts with an unescaped & character and thus none of it lands in the &args= part of the URL. And Workflow requires the callback to be to a GUID unique to each run of the workflow, so we can't blindly send a callback to Workflow.

Here's a slightly crazy idea that might work though (untested):

  1. Get some input in Workflow.
  2. Run a Pythonista script with the input as args.
  3. Have that script run a HTTP server on some obscure port.
  4. Back in Workflow, send some request to that port.
  5. The script will return the desired output.
  6. Workflow receives output and sends a special request to shut the server down.
  7. Script does as it is asked to and ends with webbrowser.open("workflow://").
  8. Workflow has its output.

This is way more complicated than it should be and might not even work. But if everything else fails I suppose there is always a way :P

(@omz, can we have an x-callback-url-compatible version of the URL scheme? Pretty please?)

0942v8653

dgelessus: Implemented!

from SimpleHTTPServer import SimpleHTTPRequestHandler
import BaseHTTPServer
import webbrowser

stopping = False

class StoppableHTTPServer (BaseHTTPServer.HTTPServer):
    def serve_forever(self, poll_interval=0.5):
        global stopping
        while not stopping:
            self._handle_request_noblock()

class RequestHandler (SimpleHTTPRequestHandler):
    def do_GET(self):
        global stopping
        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()
        self.wfile.write('hello')
        stopping = True

serv = StoppableHTTPServer(('', 25565), RequestHandler)
port = serv.server_address[1]

webbrowser.open('workflow://')
serv.serve_forever()

The workflow is just a Pythonista Run action, a URL action with "localhost:25565", and a Get Contents of URLs action.

0942v8653

If you're just starting another workflow (not continuing the current one) you can also use the input parameter:

webbrowser.open('workflow://x-callback-url/run-workflow?name=PythonistaTest&input='+urllib.quote('Hi!'))
AriX

Hey there - this is Ari, one of the creators of Workflow. Awesome that you all are trying to get this to work!

Workflow should work great with Pythonista, but as some of you mentioned, there is currently an issue which prevents Workflow's Pythonista action from working correctly. This will be fixed in an update this week! Once the update is out, here is the general process I've used for integrating workflows with Pythonista scripts:

Make a new workflow with some sort of content to be passed to the Pythonista script. For example, maybe a Text action. Then add Run Script and Get Clipboard.

Make a corresponding Pythonista script and put its name into the Run Script action in your workflow. Start with this as your python script:

import sys
import console
import clipboard
import webbrowser

console.alert('argv', sys.argv.__str__(), 'OK')
clipboard.set('here is some output!')
webbrowser.open('workflow://')

This example shows how Workflow can provide input to the Python script (in this case, the Python script will show its input as an alert), and how the Python script can pass output back to Workflow via the clipboard.

(Optionally, you could preserve the clipboard by backing it up when running the workflow. At the beginning of your workflow, add Get Cilpboard and Set Variable, and give the variable a name. Then, at the end of the workflow, add Get Variable followed by Set Clipboard.)

AriX

By the way, before the update comes out, you can simulate this behavior by doing the following in place of the Run Script action:

Add a URL action, followed by Open URL and Wait to Return. Set the URL to something along the lines of along the lines of pythonista://[[script name]]?action=run&argv=[[some argument to pass, can be a variable]]

We don't have URL encode actions yet, so this may be kind of limited at the moment.

Let me also second the request to @omz to make Pythonista x-callback compliant ;)

briarfox

@AriX Thanks for including workflow into pythonista. This will be quite fun :) I do believe the correct url to pass pythonista argv is 'args' not 'argv'

pythonista://[scriptname]?action=run&args=[argv values]
AriX

@briarfox Either works - argv lets you pass arguments one-by-one, and is what we use inside Workflow. See the documentation.

TutorialDoctor

I think for now I will use Editorial, which does have x-callback.

Edit: A simple "Console Output" Action in Editorial automatically jumps back to Workflow.

Omega0

I wonder if Workflow could be used to make an "open in Pythonista" action since it had to be removed from the app natively. I haven't had a chance to mess with Workflow yet, but if this could work it would make my life much easier.

JonB

@Omega0, you can already do this using a bookmarklet in safari. See for example
gistcheck

You create a bookmarklet in safari, then when viewing a gist you click the bookmark, and it downloads and opens in pythonista. I'm not sure what the use case is that you are trying to solve, but with slight modifications you could make this more general.

Omega0

@JonB,
I was unaware of the Safari bookmark method, but what I was looking for was something more along the lines of loading a PDF from the Google Drive app. Which I don't think would be possible with a bookmark.

0942v8653

Omega0: That would be really cool, but unless workflow includes their own web server (or at the very least a base64 conversion action) at some point, it doesn't seem possible.

Edit: I guess if there was an upload action that would work too. Right now the fastest way seems to be using Dropbox :(

TutorialDoctor

@Omega0

One workaround is to change the extension to an extension pythonista can open, then change it back once it is in pythonista.

Edit: looks like Pythonista can't open any file type?

You could read the contents of the file without opening it, and paste it in to pythonista?

0942v8653

Any news on the update? You said "this week" last week :)

not to be impatient or anything

userista

@942v8653
You're aware that this isn't the forum for Workflow, right?

the_buch

@942v8653 App Store review is closed until Dec 29 (at least according to OmniFocus)

0942v8653

@the_buch: Oh, I forgot about that—you could schedule updates (e.g. Christmas icon) to happen in the period but if you didn't get it in by Dec 22 (Monday), you have to wait.

MartinPacker

Pocket'ed for experiments with the Watch.