Extending iOS Shortcuts with JavaScript

by Shaun


With the introduction of iOS 13 Apple made it easy for app developers to add new functionality to shortcuts but this requires you to create an application and go through the process of submitting it to the App Store etc. You can add your own functionality in to Shortcuts with JavaScript using out of the box actions from with in Shortcuts. I have made a number of image utilities with JavaScript to help with creating blog posts from my iPad, lets take a look at one of these.

GeoPattern

The utility that I want to talk about is how I go about creating my pattern header images for my posts. These patterns are created using a JavaScript library called GeoPattern and you can find the link to its GitHub repository here. Here is a couple of examples of images that can be created using this library.

Image of Pattern

2nd image of pattern

The way that this library works is you give it some seed text and it uses this text to generate a random repeatable pattern as a SVG image. You can read all about how to use this library including the options to change the colours and patterns that can be used to generate the image here. Let's take a look at how you can use this JavaScript library in one of your Shortcuts.

Using GeoPattern in Shortcuts

Create HTML Page

To use this library or any JavaScript in Shortcuts you start by create a HTML page in your favourite editor of choice I would create it on your iOS device or at least test it as the tools that you use, use safari under the hood and if you page does not work in safari then it wont work in Shortcuts. Let's take a look at the HTML page I created to use GeoPattern. Note if you want to be able to use your Shortcut without a internet connection you can to include the contents of any external dependencies with the HTML page.

1<html>
2 <body>
3 <script>
4 //This is where source code for GeoPattern but I have excluded it to save space.
5 </script>
6
7 <canvas id="myCanvas" width="1200px" height="500px" style="display:none;"></canvas>
8 <p id="imageOutput"></p>
9
10 <script>
11
12 var seedText = 'Some seed text';
13 var width = "1200";
14 var height = "500";
15 var backgroudColour = "";
16 var pattern = "";
17
18 var PATTERNS = ['octogons','overlappingCircles','plusSigns','xes','sineWaves','hexagons','overlappingRings','plaid','triangles','squares','concentricCircles','diamonds','tessellation','nestedSquares','mosaicSquares','chevrons'];
19 var options = {}
20 if(backgroudColour != "")
21 {
22 options.color = backgroudColour;
23 }
24
25 if(PATTERNS.indexOf(pattern)>-1)
26 {
27 options.generator = pattern;
28 }
29
30
31 var canvas = document.getElementById("myCanvas");
32 var pattern = GeoPattern.generate(seedText, options);
33
34 var context = canvas.getContext('2d');
35
36 function drawImage(imageObj) {
37 var context = canvas.getContext('2d');
38 var x = 0;
39 var y = 0;
40 var ptrn =context.createPattern(imageObj,'repeat');
41 for(var x = 0; x < width; x += imageObj.width)
42 {
43 for(var y = 0; y < height; y += imageObj.height)
44 {
45 context.drawImage(imageObj, x, y);
46 }
47 }
48
49 var imageData = context.getImageData(x, y, imageObj.width, imageObj.height);
50 var data = imageData.data;
51
52 context.putImageData(imageData, x, y);
53 }
54 window.onload = function() {
55
56 var imageObj = new Image();
57 imageObj.onload = function() {
58 drawImage(this);
59 document.getElementById("imageOutput").appendChild(convertCanvasToImage(canvas));
60 };
61 imageObj.src = pattern.toDataUri();
62 }
63
64 function convertCanvasToImage(canvas) {
65 var image = new Image();
66 image.src = canvas.toDataURL("image/png");
67 return image;
68 }
69 </script>
70 </body>
71</html>

Here is a little clip of this code in Textastic and the output with the image. If you don't see the image when you preview this will need to be fixed before moving to the next step.

HTML page in Textastic with preview

Create Shortcut

Lets take a how you now use this HTML page in your shortcuts.

Here are the steps to use it in Shortcuts.

  1. Paste HTML in to a text action
  2. Encode this text as Base64 (parameter - no line breaks)
  3. Create URL data:text/HTML;base64,"encoded base64 data"
  4. Get Contents of webpage at URL
  5. Get images from web page
  6. Quick look of images

Here is the configured shortcut running, you can see that the image that is generated from the library is pulled out of the HTML page and is now available to use and now you could save this is your photo library etc.

Demo shortcut running

A few things to think about if you want to change some of the variables in your JavaScript then you could insert Shortcut variables in to your text action to make it dynamic. If your JavaScript created text output instead of an image your could use the create Rich Text from HTML action to then use the text output in the rest of your shortcut.

If you would like to get a copy of this shortcut you can find it here.