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)
```python
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)
````
Notes:
- 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 x_callback_url.open_url() function
- This currently supports only the x-success parameter, I'll add support for x-source and x-error at a later point (x-success is 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!