I wanted to find a good use case for sending something from MacOS to my iPad in real time. I think I’ve found it.
Basically the idea is that there are some things you can interact better with on iOS than MacOS - if only you can get them there.
There are probably lots of ways of sending some text (JSON, specifically) to iOS and invoking Jayson on it but this is the way I’ve come up with. It involves using Pushcut on iOS - which I’m happy to pay for.
Mac Python 3 Code
On the command line on the Mac you can type something like
toJayson < test.json
If you save the following Python as toJayson and make it executable (with chmod +x to Jayson
).
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
toJayson - Sends JSON to Jayson on iOS via Pushcut
Reads from stdin
'''
import requests,sys
# Take command line parameter but do surround by single quotes
myString="".join(sys.stdin.readlines())
myjson={'input':myString}
r=requests.post("https://api.pushcut.io/<mySecret>/View%20JSON",json=myjson,timeout=10)
print(r.text)
All this does is
- Reads in what’s on the command line (stdin), massaging it to remove newlines.
- Forms a JSON payload, adding the
input
parameter. - Posts this JSON to Pushcut using a Webhook. (You will need to replace
<mySecret>
but Pushcut helps you get this right.)
The above relies on the requests module which you can install using the following command:
pip install requests
By the way if you can rewrite it to remove the requests requirement, or in any way improve the code, I’d be grateful. I’m just pleased to get it working. In particular my treatment of the JSON is just to concatenate lines of it. You might be able to (and need to) do better.
iOS Pushcut Notification
You need to also create a Pushcut notification whose name is View JSON
. If you call it something else you will need to adjust the URL in the Python code. It should contain a URL action where the URL is
jayson:///view?text=[%input%]
In Use
You run the Python with a payload. This - via a webhook - causes a Pushcut notification to appear. You tap on the notification and it delivers the payload to Jayson.
I would like to eliminate some of the interaction on the iOS device but it’s scarcely material.
It’s The Principle Of The Thing
What I’ve demonstrated is - with two pieces of componentry and Pushcut - that you can send data from Mac straight into an invoked iOS app.
I think this is a useful principle to establish - and a good use of Pushcut.
As I say, the programming could probably be better and I’ve probably missed a trick or two along the way.
I also am looking for other, similar, use cases - where you invoke an app on iOS simply by posting to a Pushcut webhook, probably with some data. For example, if there were a similar XML app I’d probably want to send stuff there.
I also think that something like a Raspberry Pi could run that Python.