Will Richardson

Blog Me Photos Twitter GitHub

Shortcuts is a Cursed Minefield

April 22, 2022Will Richardson

This all starts with me wanting to host my photos outside of Instagram. I ended up using GitHub pages and wrote a simple iOS Shortcut to resize and recompress the image to make it suitable for serving on the web.

The shortcut is fairly simple - it takes an image, creates two versions (a main image and a thumbnail for the homepage), crops the thumbnail into a square to fit on the homepage grid, then saves the images to Working Copy ready to be published to GitHub Pages.

The problem arises when cropping the thumbnail. See what I need is:

image showing cropping/resizing

The image needs to be resized to that the shortest side is 600px long, and then crop the centre of that image to a square. Shortcuts has a built-in action for resizing the longest side to a certain length, but not the shortest side.

Instead you have to compare the width and height of the image and resize accordingly:

image showing if statement in shortcuts with resizing

However this gives me stretched out images for landscapes:

squished image after resize

After much head-scratching at what was going on, I figured it out. I was accessing the width and height of the image like this:

accessing height/width with photo media type

While debugging I could print this value and see the correct dimensions for the images that I was selecting. I tried assigning these to separate variables just in case there was some weird type-casting happening when they got passed to the if, but you can’t do less-than or greater-than on arbitrary shortcuts variables:

image showing no less/greater than

In a moment of desperation I swapped the Type to be “Image” rather than “Photo media” (getting desperate at this point), and it worked exactly as it should.

correctly resized and cropped image

Being thorough, I tried changing the Type back to “Photo media”… and it also worked. I made a new shortcut and left it at the default of “Photo media”… and it failed again.

The shortcut would only work if I either left the Type as “Image”, or set it to “Image” and then set it back, obviously flipping some invisible internal value^[internals] of the shortcut to correctly compare the width and height.

No programming language, API, library, or framework has made me as frustrated as writing even the most trivial Shortcuts.

← Back