[Share Code] Implemented x-callback-url
This morning I asked if it is possible to use x-callback-urls with Pythonista and access data other apps provide.
I did some experimentation and came up with a script that is working perfectly.
What it does:
- Add support for x-callback.urls to Pythonista
- Replace the default
UIApplication -openURL:sourceApplication:annotation:method with a custom one (Using @JonB's swizzle.py)
- The custom
-openURL:method checks if the url that is opened is the callback from the other app
- If the url is from the other app, a handler function that you provide will be called, with some information about the url as a parameter
- Passed information includes:
- The full url called
- The app that opened the callback-url
- A dictionary containing all parameters and their values from the url (This is where you get data you requested from another app)
- If the url is NOT in response to the x-callback-url, Pythonistas default
-openURL:will be called, with all parameters passed to the swizzled one. This ensures that other scripts using the
pythonista://url scheme to open/launch files still work.
How to use it:
(Using Drafts as an example, works with any app that supports x-callback-urls)
import x_callback_url url = 'drafts4://x-callback-url/get?uuid=YOUR_UUID&x-success=pythonista://' def handler(url_info): print(info) # Do something with the data passed back to Pythonista (available through url_info.parameters) x_callback_url.open_url(url, handler)
- You'll also need swizzle.py
- If you don't want to do anything with data passed back to Pythonista, you can omit the handler parameter and just pass the url to
- This currently supports only the
x-successparameter, I'll add support for
x-errorat a later point (
x-successis by far the most important one)
- If you have any questions about this, just ask!
Where you can get this awesome piece of code:
- Just download it from GitHub
I have to say that this is the Pythonista script I'm by far the most proud of. Thanks @omz for making this great app!
I'm trying to load a file from IAwriter to process it in phytonista...
x-callback-url will not help you for that.
If you want to load a file into Pythonista, you have to share it to Pythonista script or to import it into Pythonista via "open external folder", if the app allows it.
@cvp Thanks, i think that sharing it as input in a script may be the way to go!
After trying to share the file to "Preview Markdown.py", I found that instead of the text, the path of the file is returned by appex.get_text()....
Somewhere the contents gets mixed up, probably in IAwriter...
@dc2 did you try to share to "run Pythonista script", then the standard "import file"?
If i open iAWriter and the file i want to share, then the "share" button, followed by "run Pythonista script" and "import file", the result is that the text file is saved into the script folder.
Not exactly what i was looking for, but from there i could probably manipulate the file and convert it to HTML in accordance with my template.
I was hoping to use the "preview markdown" example and extend that script. But as mentioned before, it only retrieves the path rather that the content.
@dc2 but, if you get the path, you could open a file with this path in reading mode, and thus get its content, isn't it?
@dc2 modify the
Preview Markdown.pyexample with
... def main(): path = appex.get_text() with open(path, mode='rt', encoding='utf-8') as fil: text = fil.read() converted = markdown(text) ...
I've tested with Welcome.md of Pythonista and that works, but perhaps you could have access problem with file from another app. But, if import file works, it should be ok.
In fact opening the text file from Pythonista share didn't work and gave some type mismatch error.
The good news is that sharing from IAwriter directly worked perfectly and produced a markdown to html conversion perfectly!