Drafts, Shortcuts, and Scriptable – A Workflow Story, Part 2

In this series:

Now that you know my requirements for handling images in my Drafts-centric workflow, it’s time to let you in on a little secret: despite the fact that Drafts has incredible JavaScript and automation support, I use a shortcut called Draft Post Photo to choose my images and generate the specific markdown links that I want. The reason is pretty simple: I want to choose images from my iCloud Photo Library, and Drafts can’t do that. If I want to rely solely on a Drafts script, I would need to manually move images around, and I don’t want to do that.

So a shortcut it is.

I call the shortcut from a Drafts action though. This way, I can be in my draft with my cursor located where I want the image inserted, run the Insert Images action, which runs a shortcut that lets me choose the images and creates the markdown links for them, and returns the links on the clipboard. The Insert Images action then grabs the clipboard and puts its contents in the draft at the cursor location.

Insert Images Drafts Action

The great thing about actions in Drafts that call shortcuts is they can wait for the shortcut to end and then return to Drafts again. Have I ever mentioned that Greg Pierce, creator of Drafts, is a genius? He really has thought of everything.

Shortcut call from Insert Images Drafts action

Insert Images action gets the markdown links from the clipboard

The shortcut saves the correctly named images to two locations: iCloud/Drafts/Library/Preview/images and iCloud/Shortcuts/Drafts/images. I detailed the reasons for this in Part 1 of this series, but basically the first location lets me preview the images along with the text in Drafts (see below image) and the second location lets Shortcuts access the images for later when I use a shortcut to publish my blog post and images.

Drafts preview including images

If you’re a real Shortcuts geek, you might be shaking your head at me right now, knowing that I’m on iPadOS 15 and that Shortcuts in iPadOS 15 is supposed to support folder access for iCloud folders outside the Shortcuts folder. I’d like to reiterate the words “supposed to” from that previous sentence. Right now, it just doesn’t work at all. Only files in iCloud/Shortcuts and sub-folders can be accessed with Shortcuts' built-in Files actions.

You may be wondering how I can access iCloud/Drafts from within my shortcut, then, to save copies of the images there. This is where Scriptable comes in.

Scriptable supports bookmarks to folders and files outside its iCloud/Scriptable folder. This means you can create a bookmark in the Scriptable app and then reference it in inline scripts inside Shortcuts using the Scriptable Run Inline Script Shortcuts action. I use a Scriptable bookmark and an inline Scriptable script in my Draft Post Photo shortcut action to put copies of my desired images in iCloud/Drafts/Library/Previews/images.

In iPadOS developer beta 3, I used this same script to save copies to iCloud/Shortcuts/Drafts/images using a Scriptable bookmark to that folder because Shortcuts' Save File action kept failing even inside the Shortcuts folder, but it works as expected in iPadOS developer beta 4.

Scriptable Run Inline Script action using folder bookmarks

After the shortcut creates unique image file names that include the UUID of their container draft and saves the images with those names to both iCloud/Drafts/Library/Previews/images and iCloud/Shortcuts/Drafts/images, it copies carefully constructed links that meet all my special requirements (as detailed in Part 1 of this series) to the clipboard. When execution is returned from Shortcuts back to the Drafts Insert Images action, the action grabs them off the clipboard and inserts them at the cursor location.

Images in the correct iCloud folder locations

Markdown links created by the shortcut inserted into my draft

I also have a corresponding Drafts action called Delete Images that gets the selected text (in this case a markdown link or links in my draft) and calls a shortcut named Draft Post Delete Photo which deletes the photos in both iCloud directories (inside the Drafts directory with Scriptable and the Shortcuts directory with the Shortcuts Delete File action) and then returns to the Drafts action, which then removes the selected image markdown links from the draft.

Another note: obviously I could just delete the images in iCloud/Drafts/Library/Previews/images directly inside the Delete Images Drafts action. And actually, if I were using the TestFlight beta version of Drafts, I could even use it to delete the images in the iCloud/Shortcuts/Drafts/images directory as well, because Greg is currently working on adding file bookmarks support to Drafts. Unfortunately, the TestFlight beta doesn’t work for me right now because it breaks the Drafts grid widget I have on my iPad Home Screen, and I rely on that widget quite heavily. So I’m still running the release version of Drafts.

As for the Insert Images action, it’s always going to have to call Shortcuts in order for me to get an image picker to choose photos from my iCloud Photo Library. That’s not something a Drafts action can do, as far as I can tell. If it can do that, and it’s just not documented anywhere, please let me know. I’m all ears.

I do make pretty good use of Drafts actions for other purposes and I really enjoy their power, flexibility, and the ability to run the JavaScript right inside of a draft for testing purposes, so I’ll probably start looking at how to better make use of Drafts actions for things that affect my blogging workflow as much as possible in the future.

Next time, I’ll get into my Blog Publishing shortcut and explain a little bit about my server setup and how using Hugo to compile a static website from markdown content and html, css, and JavaScript templates drives the processing I have to do to make it all happen.