Replicate ISO Date from macOS in Drafts

Summer is here1 and I'm starting to work in odder locations - some by choice and some due to my work schedule. Normally I work from a Mac, but my travel schedule has led me back to my iPad to get my work done. I love the portability of my iPad 3 but I found I was missing the automation features I've built for my Mac.

Normally I'm writing in Drafts on my Mac, so all my Keyboard Maestro (KM) macros are available to me. When I started working more on my iPad, I realized how much I missed my KM shortcuts. For example, on my Mac I can just type .ds and a KM macro will delete the three characters and insert the current date in ISO 8601 format.

Replicating in Drafts

Drafts is my default writing space. It's the one of few apps that can look and feel the same on both macOS and iPadOS. The consistency between both platforms lessens some of the mental gymnastics I have to do in other apps when switching between my Mac and iPad. 4

The core of scripting in Drafts is Javascript.5 I had a rough outline of the script from a BIRT report date script. So I was half way there to replicating my .ds shortcut in Drafts.

The code to create the date string: 2

// See online documentation for examples
// https://docs.getdrafts.com/docs/actions/scripting

// Add keyboard shortcut Ctrl-Opt-D to insert date 
// in current cursor location

var today = new Date();
var year = today.getFullYear();
var month = today.getMonth() + 1;
// need to +1 month since Jan = 00
var date = today.getDate();

dd = today.getDate()
if(dd<10) {
    dd='0'+dd 
} 
mm = today.getMonth() + 1
if(mm<10) {
     mm='0'+mm
}

yyyy = today.getFullYear()

var iso_date = yyyy + '-' + mm + '-' + dd ;

var selRange = editor.getSelectedRange();
editor.setSelectedText(iso_date);
editor.setSelectedRange(selRange[0]+iso_date.length, 0);

Drafts also allows you to assign a keyboard shortcut to an automation script. So instead of having to use the trackpad to click and run an automation, I can just use Ctrl-Opt-D to trigger the automation.

I'm using Ctrl-Opt-D because iOS has some default shortcuts set when using the Cmd key and I could get other key combinations to work.


  1. At least for us in the northern side of the world. 

  2. Someday I'll have fancy code lines like Dr. Drang. 

  3. If Apple would only make an 11" MBP Pro with a cellular chip... 

  4. I'm looking at you Microsoft. 

  5. I'm still looking for a good book on writing automation Javascript. 

Comments

Top