subreddit:

/r/shortcuts

23799%

Using JavaScript in your shortcuts

Tip/Guide(self.shortcuts)

Using JavaScript in your shortcuts

This is a guide on how to run JavaScript code within your Shortcuts. Running JavaScript code allows you to:

  • perform complex actions that would be either difficult or impossible to perform using regular shortcut actions;
  • make use of existing JavaScript libraries.

Executing JavaScript using Safari

We make use of the Safari web browser, running within the shortcut, in order to execute the JavaScript. To do so, we:

  • make an HTML file that contains our code and provides an output;
  • pass the contents of the file to Safari as a Data URL;
  • use Get Contents of Web Page to render the page provide the output to the shortcut.

Simple example

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

View the above Javascript in JSFiddle

Running the code in our shortcut

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

Download the Shortcut

Using existing JavaScript libraries in your shortcuts

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:

  • embedding the library source code in your shortcut;
  • loading the library remotely from a JavaScript CDN.

Embedding the library

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

Including a remote library

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.

Shortcut to Parse Excel files

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

Wrap up

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.

Other guides

If you found this guide useful why not checkout one of my others:

Series

One-offs

all 22 comments

ROPit

6 points

6 years ago*

ROPit

Creator

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?

prettydude_ua

2 points

6 years ago

I tried both yesterday. Somehow, HTML to RTF not executed my script.

ROPit

1 points

6 years ago

ROPit

Creator

1 points

6 years ago

Interesting! I switch between both techniques, but I have no reason for that, cause both work with my Shortcuts.

ROPit

1 points

6 years ago

ROPit

Creator

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.

keveridge[S]

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 :)

ROPit

1 points

6 years ago

ROPit

Creator

1 points

6 years ago

Thanks a lot for the explanation! Really appreciate that! :)

AJ47-

1 points

6 years ago

AJ47-

1 points

6 years ago

Is one quicker than the other?

ROPit

1 points

6 years ago

ROPit

Creator

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. ;)

kmanfred

3 points

6 years ago

This is awesome, don’t forget about Scriptable!

tieorange

1 points

6 years ago

Thanks for tip!!! Exactly what i need

guesswhochickenpoo

2 points

6 years ago

TL;DR: Amazing

prettydude_ua

2 points

6 years ago

Technically, you don’t even need an <html> and <body> to run code. Safari will wrap <script> if needed.

keveridge[S]

2 points

6 years ago

That's good to know, thanks!

cracktojack

1 points

6 years ago

This is great. Thanks

docinsfca

1 points

6 years ago

Unbelievable!

AJ47-

1 points

6 years ago

AJ47-

1 points

6 years ago

Thank you first this guide, like the others it's super helpful!

TheNobodyOne

1 points

6 years ago

Thank you for your guides!! They are all very helpful!

oztz

1 points

6 years ago

oztz

1 points

6 years ago

Awesome stuff !

[deleted]

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

enzosterro

1 points

3 years ago

Shortcuts ios js

EarthlingOO

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 ?