In this series:
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.
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.
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.
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.
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.
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.