How to send PDF from pythonista to shortcuts?

Hi,

I have generated a PDF in pythonista (actually two) and want to send it to shortcuts.

I think this should be possible by encoding the PDF in base64 and then using the URL-scheme of shortcuts and then decoding it in shortcuts with the scripting action.
But I cannot get the encoding right in pythonista. I get an error in shortcuts that tells me that the encoding is not right. There are so many encoding options in python, but none of the ones I tried work.

Does somebody know how exactly I should encode the file in pythonista?

Here is my attempt. The shortcut Basetest just uses a scripting action to base64-decode the shortcut input.

import base64
import webbrowser

with open(filename, 'rb') as f:
    # be = base64.urlsafe_b64encode(f.read())
    be = base64.b64encode(f.read())
    import webbrowser

    url = "Shortcuts://run-shortcut?name=Basetest&input="

    webbrowser.open(url + str(be))

(edit: typo)

I’ve had a quick look and I think the general approach looks right; I’d opt for the URL safe option. What I’m not so sure about is the wrapper it adds of b'*'. I tried stripping it but it didn’t make any difference to it working, but I’m not 100% sure Shortcuts will be expecting that.

I also tried using an online base64 checker and the URL safe approach didn’t give any errors from my test PDF, but the standard one had characters included that failed the validation.

Transferring via a URL scheme doesn’t strike me as necessarily the best way to transfer the file to Shortcuts. I’d suggest modifying things so that the files are read from source (local or online storage) by Shortcuts. Doing so would simply bypass this issue.

Hi,
thank you for your experiments.
Following your idea, I tried an online converter Base64.guru with a repair function. I’m not sure what the repair actually did (to compare/diff the strings I want a screen larger than my phone’s), but it involved cutting the b’ and ’ like you proposed.
If I pass the repaired version from the converter from pythonista to shortcuts it works :slightly_smiling_face:
So now I (think I) need to figure out, how to convert the byte-string be into a real string in python. I report back here once I have figured it out.
Edit: it works!
The str function needs an encoding parameter to convert byte-strings to normal strings, and apparently shortcuts does not like the url-safe encoding. Working code:

import base64
with open(filename, 'rb') as f:
    be = base64.b64encode(f.read())
    import webbrowser

    url = "Shortcuts://run-shortcut?name=Basetest&input="

    webbrowser.open(url + str(be, 'utf-8'))

On your suggestion to read the files directly in shortcuts:
How would you pass/copy/something else a file via the file system?
This was my first thought, but shortcuts an pythonista are both sandboxed.
(I do not want to use a file picker.)

1 Like

That’s just iCloud, but I’m pretty sure I’ve seen some Dropbox integration for Pythonista around, and Shortcuts definitely supports Dropbox. You could then just pass the Dropbox file path to Shortcuts from Pythonista.

Ah, so not really local :wink:

Yes, pythonista has some dropbox integrations, but I feel it is not really simpler than sending the file base64 encoded (now that it’s working).
(And to be honest, my homebrewed dropbox integration was using the old dropbox api and I dropped it as soon as pythonista syncing over icloud was possible.)
So yes, for other people syncing the file to dropbox and only sending the path to shortcuts might be a more convenient option.

Thank you so much for your help. Now there is one less document picker in my life.

No problem. My thinking was the local option would have involved the document picker in Shortcuts and the online would be Dropbox or uploading to a web or (S)FTP site.