Drafts 4 Keyboard Scripts

October 15, 2014 by Gabe | [mmd] |

Unless you've been locked in a doomsday cult bunker you've probably noticed that the beloved iOS text editor and action app Drafts received a big update. The obvious part of the update is the redesign for iOS 8 and the new action creation process. The second most obvious thing is the iCloud based storage and syncing. But the real meat of this update is the extra keyboard row and the new JavaScript actions.

JavaScript Plus

Drafts 4 supports standard JS but includes a few extra functions for working with the text window. Here's the list from the Drafts help:

  • getText() : Returns the full text currently being edited.
  • setText(string) : Replaces full text being edited with string.
  • getSelectedText() : Return only the selected text. If no text selection exists, this will return an empty string.
  • setSelectedText(string) : Replace only the current selected text range with the string. Also updates the selected range to match any change in length of the new string.
  • getTextInRange(start, length) : Return text in the request range.
  • setTextInRange(start, length, string) : Replace the text in the specified range with the value of string.
  • getSelectedLineRange() : Returns the range (start,length) of the full line based on the current cursor position
  • setSelectedRange(start, length) : Set the selected range of text. Invalid ranges will be automatically adjusted, and this text selection will be applied after successful completion of the script. 
  • getClipboard(): Returns current contents of the system clipboard.
  • setClipboard(string): Set the system clipboard to the string passed.

This is where the fun begins.

Sort Lines

I wouldn't classify myself as O.C.D. I like to think I'm more pedantic that anything else. When it comes to lists, sometimes it really bothers me when they're not in sort order. This is especially true of Markdown lists that are only keyed off the first list item.

Here's a keyboard script that scratches my lizard brain just right. It takes a list like this:

5. Cat Food
3. Binoculars
2. Pebble to suck on
1. Lefty scissors

and converts it into a list like this:

1. Lefty scissors
2. Pebble to suck on
3. Binoculars
5. Cat Food

To add this to the extra keyboard row in Drafts 4, do this:

  1. Click the pencil icon on the extra keyboard row to edit the keys
  2. Add a new key with the plus in the upper right corner
  3. Choose a "Script" type key
  4. Add the Javascript, label and voice over.

Here's the script1:

function generate(s){
    var temp = new Array();
    temp = s.split('\n');
    temp.sort();
    var result = "";
    var i;
    for(i=0; i<temp.length; i++) {
        result += temp[i] + "\n";
    }
    return result;
}
setSelectedText(generate(getSelectedText()))

Notice that Drafts even helps validate the Javascript while writing. Pretty slick.

Time Stamp

Speaking of pedantic, I like my timestamps and I like them in a specific format. When I create notes, I regularly insert timestamps at the beginning and in-between notes. This keyboard script makes it easy:

function getDateTime() {
var now = new Date(); 
var year = now.getFullYear();
var month = now.getMonth()+1; 
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds(); 
if(month.toString().length == 1) {
    var month = '0'+month;
}
  if(day.toString().length == 1) {
    var day = '0'+day;
} 
  if(hour.toString().length == 1) {
    var hour = '0'+hour;
}
  if(minute.toString().length == 1) {
    var minute = '0'+minute;
}
if(second.toString().length == 1) {
    var second = '0'+second;
} 
var dateTime = year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second; 
return dateTime;
}
setSelectedText(getDateTime())

I use this all the time and it's terrific.

Flexible, Light and Powerful

Those are the three reasons I keep returning to Drafts for basic notes. It's the jumping off point for words. I don't sit and write in Drafts, but I could. I don't archive all of my notes in Drafts, but I could. What I do, is launch Drafts and write about a hundred times a day. I use Drafts because it's a world of options.

Drafts for iOS | Universal | $5


  1. Courageously copied from the internet