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
p.addButton("OK");
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;
draft.update();

 

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


Automators 23: Diving into Drafts Automation with Tim “The Drafts Man” Nahumck

I was really excited when Tim Nahumck agreed to come onto Automators to talk about Drafts! In the last year the app has increased in power so much, as well as gaining a Mac app, and now seemed like the perfect time to cover it. Continue reading “Automators 23: Diving into Drafts Automation with Tim “The Drafts Man” Nahumck”


Workflow: Drafts 5

Workflow has built in support for Drafts 4 – but no actions for Drafts 5. This workflow is intended to replace some of that functionality. You can share text to it, get text from the clipboard, type text in, or modify the text on the keyboard to start.

The next step is choosing your action, you can create a new draft, and append or prepend to an existing draft – with the ID of the draft (Drafts Action to get ID).

Next you can choose to run an action after putting the text in Drafts, this is done by typing in the action name, though you may want to replace the Ask for Input action with a List and Choose from List if you have a few actions you regularly use.

You can get the workflow here: Drafts 5


Drafts 5 Review

[TOC]

Intro

For those of you not familiar with Drafts, it’s designed as an app which lets you input text and then decide what to do with it. Why? Sometimes that message turns into an email, or maybe the quick idea turns into a mind map. Or maybe you just don’t know where the text is going to end up when you start writing. Drafts is designed to let you write and then process the text, removing the need to choose where the text should go first.

When you open Drafts you get a blank note – automatically. You can set the time out period so that when you return to Drafts you either see your last note or a new note – mine is set to the default of 60 seconds. You can also open the app with force touch which allows you to create a new blank note, create a note from the clipboard, dictate a note, search, or open any one of your 4 most recently edited Drafts. The first three of these options, plus the choice to import a file, appear when you force touch the + on an iPhone, or press and hold on an iPad. The great part about the dictation is that unlike regular Siri dictation there’s no timeout, that’s right – you can dictate for as long as you like. David Sparks made a great introduction to Drafts 5 which I highly recommend: Drafts 5 Overview

What Changed?

Some of you may have used Drafts before, in fact if you’re reading this it’s likely you have so what’s new? First of all the app has been completely rewritten, this means everything is new and shiny – and 100% up to date. There’s also lots of new features.

First things first, you can import all of your notes to Drafts and most of your actions. Make sure you’re running the latest version of Drafts 4, and then in the settings of Drafts 5, tap “Migrate from Drafts 4”. You can choose to import your Drafts, and/or Actions and Keys. You can also export from Drafts 4 into Drafts 5 – there’s a special Send to Drafts 5 for drafts, and inside each action in Drafts 4 you can choose to migrate the action, or add it as a callback so when the action is executed it is done so in Drafts 4 and then returns you to Drafts 5. Not every action which was available in Drafts 4 is available yet in Drafts 5, but Drafts 5 will make a list of the actions that couldn’t be imported so you can handle those manually.

Themes

Drafts 4 had three theme choices – white, cream and black – and the ability to automatically switch to the black theme at a certain brightness threshold. Drafts 5 has several more colour theme options, and you can specify a day theme and a night theme to work with, which will switch at the brightness level you prefer. Choose the theme is a pro feature, and I’ve really enjoyed using the dark Solarized theme during the day with black at night.

Custom Icons

Choosing app icons is definitely a nice way to make your device yours, and Drafts now offers the ability to choose your app icon. There’s a mix of flat images and ones with gradients – I personally picked the purple as that’s my favourite colour.

Tagging and Flagging

Tagging allows you to optionally file your note in multiple places – and helps you to group together notes on a particular topic as well. Tags are shown under a draft in the list of drafts. When adding a tag to a draft you can see a list of your current tags in the keyboard bar, if the one you are looking for doesn’t exist then you can type to create it. Tags are lower case – you can type capitals when creating them but they will be formatted for you. This is really useful as it allows you to avoid having Groceries and groceries by accident.
Flagging is simple – a draft is flagged, or it is not flagged. You can use it however makes the most sense to you.

Filtering

Now we have filters we need to be able to work with them. Filtering allows us to choose combinations of tags to see or not as we want. By tapping the name of a tag in the filter section we add it to include, and by tapping the (ban) icon we can add the tag to the omitted list. This means you could include the tags cat and dog, but disallow spider. These tags are combined with an AND, so only drafts with all of the included tags and none of the omitted tags will show up.
Tags are not the only settings however, you can also set how much of a draft you want to see in the list, the sort order, if flagged items should be included or not, specific text to search for and more.

 

The full documentation for filtering in Drafts is well worth reading.

Workspaces

Workspaces can be created in two ways, from a filter you are currently using, or from scratch. They have the same set up as filters, but as you can name them and set the colour and icon to represent they are easy to keep track of. You can set the order that you would like to view your Workspaces in, and optionally show them, with or without their name, at the bottom of the list of Drafts. Workspaces are ideal for filters you make frequently, or which are more complex to construct.

Focus

By default Drafts will open to a new draft after a set period of time – which can be adjusted by you. Focus mode allows you to do that by focusing on one particular draft, this means that whenever you open the app – even if that’s next month – you’ll be back at the draft you had open last. You can enable focus mode in two ways, by tapping the eye icon in the bottom left hand side of the editor when you’re not editing a draft, or by opening the Settings and enabling it.

SiriKit Integration

Hey Siri, make a note in Drafts to investigate buying the llama farm

You can now create new Drafts using Siri, it’s simple but effective. You can also dictate notes, but that’s not new – what’s very useful though is that the dictation will not end as it does usually when dictating elsewhere on iOS.

Editor Options

In Drafts you can now control a lot of settings with regards to how the editor appears, from paragraph numbers to syntax highlighting, and even whether or not the toolbar or the status bar should show you have plenty of choices.


Here are some of the options you can choose to set for the editor by tapping the Aa button:

  • Syntax highlighting
    • Plain text
    • Markdown
    • Simple List
    • Taskpaper
    • JavaScript
  • Font
    • Choose from any of the fonts pre-installed on iOS, or ones you have installed yourself.
    • Select a particular font to use for Monospaced text.
  • Line height
  • Paragraph spacing
  • Margins
  • Paragraph numbers
  • You can enable or disable
    • Autocorrect
    • Spell check
    • Smart punctuation
      • Quotes
      • Dashes
    • Capitalisation
      • None or sentences
  • Portrait, landscape, or any (only on iPhone)

Draft 5 also has an improved arrange mode, Drafts 4 just allowed you to rearrange lines, Drafts 5 lets you edit blocks (separated by two line breaks) or lines, edit sections, and delete or remove them. You can also trigger the arrange mode via the URL scheme.

There is also a pre-installed Tasks action, which is shown on your keyboard by default. This is very clever, it makes the line that you’re on or the text you have selected into a simple bullet checklist by adding - [ ] at the beginning of the line. By tapping on the [ ] you can mark a task as done [x].

 Actions and Keyboard Groups

For those of you familiar with Workflow, actions are very similar in some ways. An action can contain any number of steps, a number of which are provided for you to use as is. Drafts does not arrive empty, it comes with 5 example action groups, 4 of which are available as keyboard groups for you – you can do what you like with these actions, including delete them. But if you’re not familiar with Drafts and it’s actions I recommend you swipe across the action from left to write, tap edit, and have a look.
One important change between Drafts 4 and Drafts 5 is there is no longer an app wide default as to what to do once an action has been executed, you now set this per action group. This is very useful so if you have a group of “share” actions for example, designed to get the text out of Drafts, you could immediately have the draft the action was executed on archived or trashed as you prefer.
You can choose to enable an action group on the keyboard, Draft has a row of buttons above the keyboard, and if you have multiple groups of keyboard actions you can swipe up and down on this row to switch between them. As you can assign keyboard shortcuts to actions as well this becomes very powerful very quickly.

Scripting in Drafts

The most powerful part of Drafts actions is the Script action, this is what it sounds like – you write a script which does things. Drafts uses JavaScript, if you use OmniGroup products they are introducing JavaScript for automation in those too – so learning the language could definitely pay off in the long run.
There is a wiki about Drafts scripting, which is definitely worth reading if you intend to use them.
My favourite functions that can be included in Scripting is the Prompt – so if you want to ask the user “cats or dogs”, you could create a prompt which shows “cats” and “dogs” as buttons, and then depending on which they choose your script would react accordingly. A prompt can have any of the following options

  • Title
  • Message
  • Text Fields
  • Date and or Time Fields
  • Selects – single or multiple choice
  • Buttons

Here’s the script to create that prompt:

var p = Prompt.create();

p.title = "Title";
p.message = "Message";

p.addTextField("myTextField", "Text Field", "");
p.addDatePicker("myDate", "Date Field", new Date(), {
  "mode": "date"
});
p.addDatePicker("myTime", "Time Field", new Date(), {
  "mode": "time"
});
p.addDatePicker("myDateTime", "Date & Time Field", new Date(), {
  "mode": "dateAndTime"
});
p.addSelect("mySingleSelect", "Single Select Field", ["Cat", "Dog", "Llama"], [], false);
p.addSelect("myMultipleSelect", "Multiple Select Field", ["Cat", "Dog", "Llama"], [], true);
p.addButton("Button");

p.show();

The cancel button is in a prompt by default, but you can hide it if you wish. The prompt will definitely make an appearance in actions you import from the action directory – if only because it’s a great way to get credentials from you which can be used in an action.

Credentials

If you want to Tweet with Drafts, save something to Evernote, or add tasks to Todoist, there are two ways to do that. Passing the data to the app on your device and telling it what to do – which is not possible with every app – or by using the API of the service to complete the action. The difficulty with an API is you need a token to access it – similar to needing a username and password to log in. Drafts 5 has a clever way to store these, so that scripts can access them without needing to save the access key in the script itself – so there’s no worry that by sharing your Todoist action people can start adding things to your Todoist, unless you explicitly want them to do so and share your credential! You can change the credentials saved in Drafts by tapping on the Settings wheel in the bottom right of the editor, and opening Credentials. By choosing the “forget” option the credential is deleted, and you can input a new one the next time you run an action that needs it. This system is especially useful as you can share one credential with many scripts – so it doesn’t matter if you have 1 or 100 Todoist scripts, they can all access the same key, and you are in charge of it. The documentation for credentials in scripts is definitely worth reading if you want to access APIs, and the general documentation is also very helpful – especially if you have multiple accounts you want to interact with.

 Price

Drafts 5 is free. Yes, you read that correctly. For the basic functionality you can get the app for free. If you want to create and edit your own actions, choose your theme(s) and custom icon, use Workspaces, have access to enhanced URL automation and support development you can do so for $1.99 a month, or $19.99 a year. If you’re not sure which you need then there is a 7 day trial to let you play with the pro features.

David Sparks (MacSparky) made a second video for Agile Tortoise showing what’s new in version 5: Drafts 5: What’s New

Upgraded and Awesome Things

That’s a brief overview of the new things, now onto everything else!

Widgets

Drafts 4 had a widget, which allowed quick entry by default and if you enabled “Show More” (on the top right) allowed you to see the most recent Drafts. Drafts 5 has 3 widgets – one for quick entry, one for your recent items, and one for your workspaces. This allows you to enable and disable the ones you want on different devices, and with the workspaces go straight to the collection of Drafts you need.



Backup and Export

Your data is important, and there are backup options in Drafts which allow you to backup your drafts themselves, and/or your actions. These options were there before, but are now unified onto one screen. You can also export your drafts with the option to add a filter – so you could export all of your Drafts with the tag “cake recipe” if you wanted – for example to send them to me.


Reminders Import

Reminders import was present in version 4 too, but now you can add a tag when the reminders are imported – which is very helpful for filtering for them later.

After Action Success

This setting used to be global which could be overridden on a per action basis. You can now set this option for each action group, and you can still override it for an action if you wish. This is extremely helpful for the keyboard action groups as you’re unlikely to want most of those actions to archive or trash your Draft when use them.

Drafts Forums

Agile Tortoise have set up forums where Drafts users can talk to one another, help each other create actions, request features, and more – I’ve already signed up and am checking it regularly to see what I can learn from everywhere else.

Drafts Forums

Conclusion

I’ve been using Drafts 5 instead of Drafts 4 for quite some time now, and I’m very happy with it. It has been my go to “text starts here” application ever since I bought it after hearing it mentioned on an episode of the Mac Power Users at some point – and it has become more than that over time. With the scripts and actions I can now write entire blog posts in Drafts, and could even upload them to the server and post them immediately if I wanted. This app has more than earnt it’s position in my dock and will be there for the foreseeable future.

Other Reviews

Get The App

Drafts 5 is available in the App Store from today.