Forum Archive

From Shortcuts to Pythonista to Shortcuts

Enez Houad

I’m beginning to learn Python and Pythonista. As English is not my native language (I’m French) and all documentations and examples are all in English, it’s sometime difficult ;-)
The forum is an extraordinary place to get infos even if the level is very hight for a beginner ;-)
I thank you all.
With the small exercices that I try to resolve, I progress slowly.
But I get stuck for few days with a small problem. The thing I want to do looks simple (I've found an example on the forum but it's to difficult for me) :
1. I want to init to random numbers in Shortcuts,
2. then add them in Pythonista,
3. then get and show the result in Shortcuts.
Points 1 and 2 are done : from Shortcuts, I send:

pythonista://somme.py?action=run&argv=var1&argv=var2

var1 and var2 are my random numbers and somme.py is my Pythonista script:

import sys, webbrowser

n1, n2 = int(sys.argv[1]), int(sys.argv[2])
n3 = n1 + n2
url = "shortcuts://"
webbrowser.open(url)

I’ve found a solution to transfer the result to Shortcuts with the clipboard but it seems possible to do it with x-callback but I can’t find how :-(( I've tried many options in my url in Pythonista and the Open X-Callback URL in Shortcuts but it's always bad.
I need help and I know that I can count on you ;-)

cvp

@Enez-Houad see here, very easy ...

sulcud

Hello @Enez-Houad, you're solution is good, the clipboard is a good way to share information Pythonista => shortcuts the only two problems I saw in your scripts are first you are not setting the values to your clip board you can do it with:

import clipboard
import sys, webbrowser
n1, n2 = int(sys.argv[1]), int(sys.argv[2])
n3 = n1 + n2

# I don't know what variable you want to use n1,n2 or n3?

# This set the value n3 to the device clipboard
clipboard.set('{}'.format(n3))

Second you need a shortcut that can wrap your clipboard, the shortcuts app have a variable with that function, and with it you can use the correct url scheme

# this will open your shortcut with as input the device clipboard
url = "shortcuts://run-shortcut?name=[ShortcutName]&input=clipboard"
webbrowser.open(url)

shortcuts url scheme
Pythonista clipboard

example of the shortcut

cvp

@Enez-Houad When your Shortcut exécutés a Pythonista script, it waits for its return
Example of Pythonista back

import sys, webbrowser,clipboard

n1, n2 = int(sys.argv[1]), int(sys.argv[2])
n3 = n1 + n2
print(n1,n2,n3)
clipboard.set(str(n3))
url = "shortcuts://x-callback-url/run--shortcut?name=[Shortcut_Name]&input=clipboard"
webbrowser.open(url)

Enez Houad

Thanks for your answers. You confirm that my solution with the clipboard was good :-)
The problem I have is that I wanted to understand how to use the open x-backcall from Shortcuts and the documentation doesn’t help (don’t forget that I’m a beginner ;-)
I think that I could use :

url = "shortcuts://x-callback-url/run--shortcut?name=[Shortcut_Name]&input=text&text=[n3]"

But when I use this script there’s an infinite loop because the shortcut is relaunched at its beginning...

cvp

@Enez-Houad In your Shortcut, do you call Pythonista via url or do you with the "Execute Pythonista script"command, because my example above works for me

cvp

@Enez-Houad Try this shortcut with my little script above

Enez Houad

@cvp I've tried your shortcut with this script :
import sys, webbrowser,clipboard

n1, n2 = int(sys.argv[1]), int(sys.argv[2])
n3 = n1 + n2
clipboard.set(str(n3))
url = "shortcuts://x-callback-url/run-shortcut?name=Shortcut_Name&input=clipboard"
webbrowser.open(url)

I've got an infinite loop.

cvp

@Enez-Houad Strange, that works for me. I have the new beta installed, and you?

cvp

@Enez-Houad You can see here an mp4 of recording my screen during shortcut-Pythonista-shortcut

Enez Houad

@cvp I use the last beta of Pythonista too !
And here is a mp4 recording of my screen.
https://drive.google.com/open?id=1-b5L99gCvsmfsZPQOGtUuUvJrDD6bbBW
Strange !

cvp

@Enez-Houad Did you try to remove both apps from memory?

cvp

@Enez-Houad Please try with two "-" between run and shortcut, for me it makes a difference but don't ask me why

url = "shortcuts://x-callback-url/run--shortcut?name=[Shortcut_Name]&input=clipboard

On a zoom of your movie, it seems to be only one

Enez Houad

@cvp It's ok with two :-)) Thanks for your patience !

cvp

@Enez-Houad 😅 This double - is not documented....

Harwood

Shortcuts’ Open X-Callback URL allows for setting custom callback keys. By setting Success Key to argv the success is appended to the call to Pythonista as the last argument. Unfortunately only one of the keys can be set to argv as only the last one will be appended.

Here’s an example:

from datetime import datetime
import json
from urllib.parse import quote_plus

....

x_success = sys.argv[-1] # shortcuts-production://x-callback-url/ic-success/<UUID>
x_cancel = x_success.replace('ic-success', 'ic-cancel')
x_error = x_success.replace('ic-success', 'ic-error')

x_success += '?x-source=Pythonista3'
x_success += '&result=' + quote_plus({....})
x_success += '&datetime=' + quote_plus(str(datetime.today()))

webbrowser.open(x_success)

Hope this is helpful to someone.

Cheers,

teromakotero246

This worked for me Using iOS Shortcuts App and Pythonista