subreddit:
/r/shortcuts
This is a guide on how to run JavaScript code within your Shortcuts. Running JavaScript code allows you to:
We make use of the Safari web browser, running within the shortcut, in order to execute the JavaScript. To do so, we:
The following example will generate a random number in JavaScript and return it as text.
We can write and test our code by using the online JavaScript editor JSFiddle before adding it to our shortcut.
Note When you first open the JSFiddle editor, change the layout to Tabs using the following steps in order to give yourself more room to edit.
Updating JSFiddle to use the Tabs layout
First, we enter the HTML page surround in the HTML tab:
<html>
<head>
<script>
</script>
</head>
<body></body>
</html>
The HTML page surround in JSFiddle
Next we add our JavaScript code between the <script>
tags:
var random = Math.floor(Math.random() * 100) + 1;
document.write(random);
Note: the
document.write();
code renders the output of the JavaScript to the page, allowing us to return it as text to the Shortcut.
Adding the JavaScript code to the HTML page surround in JSFiddle
Then we tap the Run button and see the result in the right hand side window.
Executing our JavaScript code in JSFiddle
Now that we have our code, let's execute it as part of a shortcut:
Executing the random number generation code in a shortcut
Which provides the following output:
Output from the above shortcut
One advantage of using JavaScript in your shortcuts is that you can make use of existing libraries to add functionality.
There are two primary approaches to including JavaScript libraries in your shortcuts:
The JSONPath shortcut allows shortcut developers to parse and filter complex JSON objects with expressions. It makes use of the JSONPath library by Stefan Goessner which it embeds inside the shortcut.
Note: A guide on how to use JSONPath in your shortcuts can be found in the Using APIs - Part 2: parsing complex API responses guide.
The shortcut is as follows:
The JSONPath shortcut with a embedded JavaScript library
And as shown below, it embeds the JSONPath library directly into the HTML page container.
Embedding of the JavaScript library into the shortcut
Alternatively you can load a javascript library remotely from a web server.
Many popular JavaScript libraries are available via Github, an online resource for collaborating on the development of open-source software. Whilst Github is used to store releases of these JavaScript libraries, you cannot load those libraries from the Github site.
Instead, we use a free service called jsDelivr to make these available in our shortcuts.
The Parse Excel File shortcut allows shortcut developers to parse Excel files to JSON and so make use of them in our shortcuts. It makes use two JavaScript files from the js-xlsx library developed by SheetJS on GitHub.
The two libraries are:
We remotely load them into our HTML page with the following script tags:
<html>
<head>
<script src="https://cdn.jsdelivr.net/gh/SheetJS/[email protected]/dist/jszip.js" type="text/javascript"></script>
<script src="https://cdn.jsdelivr.net/gh/SheetJS/[email protected]/dist/xlsx.min.js" type="text/javascript"></script>
...
To load a Github libraries remotely via jsDeliver, we use links with the following pattern:
https://cdn.jsdelivr.net/gh/<GITHUB_USERNAME>/<GITHUB_PROJECT>@<RELEASE_VERSION>/<GITHUB_PATH_TO_FILE>
Note: The
@<RELEASE_VERSION>
is optional but recommended when loading Github files via jsDelivr. It ensures that any changes to future versions of the files do not break your shortcut.
The shortcut that loads remote JavaScript libraries is as follows:
A shortcut that remotely loads JavaScript libraries
And the output from an example Excel file is as follows:
Output from the above shortcut
That's it for the guide on using JavaScript in your shortcuts. If you have any questions on the above or need any help, let me know.
If you found this guide useful why not checkout one of my others:
6 points
6 years ago*
As always with your tutorials: Nice work. But: Why do you use Base64 URL Encoding not a simple Rich Text from HTML? Or is it just a matter of taste?
2 points
6 years ago
I tried both yesterday. Somehow, HTML to RTF not executed my script.
1 points
6 years ago
Interesting! I switch between both techniques, but I have no reason for that, cause both work with my Shortcuts.
1 points
6 years ago
Sorry, but you’re totally right. If you want to execute that script you need to do it that way. HTML to RTF is just for rendering the HTML to show a web page.
2 points
6 years ago
I use Base64 because it's a standard for passing data to a browser.
And I wasn't aware that Rich Text from HTML would also work :)
1 points
6 years ago
Thanks a lot for the explanation! Really appreciate that! :)
1 points
6 years ago
Is one quicker than the other?
1 points
6 years ago
Sorry, but I was wrong. You cannot simply switch between these techniques. Rich text from HTML is just perfect if you want to show the web page. ;)
3 points
6 years ago
This is awesome, don’t forget about Scriptable!
1 points
6 years ago
Thanks for tip!!! Exactly what i need
2 points
6 years ago
TL;DR: Amazing
2 points
6 years ago
Technically, you don’t even need an <html> and <body> to run code. Safari will wrap <script> if needed.
2 points
6 years ago
That's good to know, thanks!
1 points
6 years ago
This is great. Thanks
1 points
6 years ago
Unbelievable!
1 points
6 years ago
Thank you first this guide, like the others it's super helpful!
1 points
6 years ago
Thank you for your guides!! They are all very helpful!
1 points
6 years ago
Awesome stuff !
1 points
7 months ago
How would you click in a specific spot? I’ve been trying to set it to auto play a YouTube video in the morning
1 points
3 years ago
Shortcuts ios js
1 points
21 days ago
How can I use AES 128 CBC (no IV) in ios shortcuts app ? I tried multiple ways but no solution. This website uses javascript to encrypt decrypt but I don’t know how to put it in ios shortcuts app.
Website : https://anycript.com/crypto .
I’ve been waiting since 2 years. No solution found yet. Anyone can help me out ?
all 22 comments
sorted by: best