Drafts Action: Fix Empty Markdown Links

As is often the case, as I use Drafts I find things that I need to do and write a script to fix it for me! In this case, I have a habit of planning to add links later in my text, but find it a pain to go through the whole article to find them and the find function is too darn manual for me. There were already a few actions in the Actions Directory, but they didn’t quite work the way I wanted them to. If you haven’t guessed where this is going yet: I wrote my own!

This action using the Script function in Drafts, and some good old regular expressions. The code itself can be broken down into eight steps:

  1. Get the contents of the Draft.
  2. Create the regular expression.
  3. Parse the contents of the Draft with the regular expression to get a list of matches
  4. Create a prompt
  5. Add each match to the prompt
  6. Show the prompt
  7. Go through each match again and replace it (within the whole content) with the information provided through the prompt.
  8. Update the draft.
let content = draft.content; //get the content of the draft

let pattern = /\[[^\]]+]\(\)/g; //build the regex

let matches = content.match(pattern); //find all the empty links

//create the prompt
let p = Prompt.create();
p.title = "Missing Links";

//add each empty link to the prompt
matches.forEach(function (match, index){
    p.addTextField(index, match.replace('[', '').replace(']()', ''), "");

//show the prompt
let didSelect = p.show();

//if the prompt wasn't cancelled
if (didSelect) {
    if (p.buttonPressed == "OK") {
        //go through each empty link and replace it with the updated information
        Object.keys(p.fieldValues).forEach(function (index) {
            content = content.replace(matches[index], matches[index].replace('()', '('+p.fieldValues[index]+')'))

//update the draft
draft.content = content;


The idea is I can run this at the end of writing, deal with all my links in one prompt, and get on with things. It’s the kind of action I’ll include as a step in other actions to make sure I don’t accidentally post empty links!

Get it from the action directory

Scriptable: Use JavaScript to control your iPhone

Some time ago on Twitter I stumbled across Simon, he was retweeted by the developer of Working Copy, an app I love and use heavily – and the retweet was a video of a script being run on an iPhone and returning some pretty cool results – so I asked “what is this and how can I get a copy?”. Not too long later I was invited to the beta of Scriptable, and got stuck in.

Scriptable is an iOS 12 app, and has integration with Siri Shortcuts to help you automate to the max. Simon has integrated a huge number of iOS APIs – so you can get your current location, communicate directly with the calendar, and more. The app also uses native UI elements too – so if you write a script that shows an alert the JavaScript you write calls the native alert box.

So, what can you do with this app? The built in examples include getting the current Slack status, grabbing the latest XKCD comic, and viewing shared JSON in a readable format. There are many more sample scripts over on the Automators Forums where I host a community run forum for the app (what better home than with Automators?).

You can also trigger Scriptable scripts in any number of ways. When you tap the settings icon inside of a script you get the option to immediately add the script to Siri, you can copy the URL scheme to the app, and even add it directly to Launch Center Pro which will then let you trigger notifications to run it at specific times or places.

All of your scripts are donated to Siri too, which means they’re available in the Shortcuts app as well.

You can get Scriptable on the App Store now – and it’s free which is insane, so please make use of the tip jar!