Evaluating JavaScript in Shortcuts

Shortcuts is a versatile automation tool on iOS, but it does have limitations. Many of these stem from it not being a true scripting/programming language, but rather a visual automation tool. When compared to alternatives like Pythonista or Scriptable, the app comes up a little short on things you might like to do with it sometimes that would be easier in other programming languages? But what if I told you that Shortcuts can actually tap into some of that coding power through a technique for evaluating JavaScript? Interested? Well let me explain.

The technique I’m going to explain does have limitations in terms of the data it can access and the format it can release to Shortcuts. It’s text all the way, but as long as you are careful you can use base64 to transfer non-text items, such as images, in a text-based format.

We start with the fact that iOS has built in support, through it’s web engine in iOS12, to support the ES6 version of JavaScript. Through this we need to identify a way to leverage the web engine to do some JavaScript for us. Fortunately Shortcuts is a rather ‘web-capable’ ap. For this particular case we are going use the Get Contents of Web Page action.

Using this action we can grab whatever text is put out on a web page, and we can embed JavaScript in a web page to produce the output. The next question is therefore how do we create the page in a shortcut? Normally pages are pulled in from a web server somewhere, and whilst we could use an intermediate web host, there is another way.

We can use a Data URL (ref. Data URI, Wikipedia) to actually define a whole web page in a URL. It sounds bizarre, but it’s true, and it was actually used by the Workflow team (before the were acquired by Apple and the app changed to Shortcuts) to build pages where you could save home screen bookmarks to launch Workflow workflows.

Here’s a standalone shortcut that will take some JavaScript as input, wrap it in HTML script tags, build a data URL from it, build the web page, and then grab the output.

To illustrate how this can be used, I’ve created a test shortcut that utilises the shortcut above.

The test shortcut first creates some JavaScript to carry out a calculation. The important point to note is the use of the document.write() function which will output the result of the evaluated JavaScript to the web page. The JavaScript is passed to the EvalJS shortcuts, and the result captured to a variable.

The shortcut then asks for some text-based user input, and includes that in some new JavaScript that utilises a function to reverse the order of the text. The EvalJS shortcut is called once again, but with the new JavaScript code to evaluate. That is also saved to a variable.

Finally the results are displayed to the user in a pop-up.

Whilst there are definite limitations for what can be evaluated with the JavaScript, there are likewise limitations in Shortcuts. Being able to evaluate JavaScript like this does open up some extra options when dealing with Shortcuts. Ultimately though, when even this does not enable the level of functionality you need, it is time to look at leveraging other tools like this mentioned at the top of the post.

Author: Stephen Millard
Tags: | shortcuts | javascript |

Buy me a coffeeBuy me a coffee

Related posts that you may also like to read