SOLVED: Get callback result to a specific line in drafts (v4)


Hello Drafts Community,

I’ve been using drafts for a while now and started to harness its real power by creating some actions. The latest action I created does however not really perform as I hoped…

The action is to fetch the email address of a person who’s name is in line 2

In drafts I create a note where the email subject is on line 1, the full name of the person I write to is on line 2 and the content of the email is on line 4 and after.

I created an x-callback action to Workflow (now Shortcuts) which uses the input on line 2 to get the email address from my contacts. This works perfectly and I am returned the email address of the person I entered on line 2. However the result is not inserted after the name but at the end of the document. This becomes a problem when I use my second action which sends the entire content of the note to my email (line 1 subject, line 2 recipient, line 4 and after email content).

As the email address is not inserted on line 2 my second action does no work properly and the email address is in the emails content instead of being in the “to” section of the email.

So my question is, is there any way I can tell the callback to insert the result of the Workflow query after the name on line 2? If not possible in the callback, could this be done using a script bearing in mind that I am still on Drafts 4.

workflow://x-callback-url/run-workflow?name={{Drafts: get email address}}&input=[[line|2]]&x-success={{drafts4://x-callback-url/replaceRange?uuid=[[uuid]]&start=[[selection_start]]&length=[[selection_length]]&retParam=result}}


This would be considerably easier in Drafts 5, but you are on the right track on how to do it in Drafts 4. The problem with your current setup is that the returning /replaceRange URL is configured to replace the current selected text using the start and length parameters pointing to the last selection.

There is no built-in tags to figure out the range of the second line. That would require a script step before the URL step which split the draft content, figured out the range and assigned custom start/length tags to be used in the URL step.

Hopefully that heads you in the right direction.


Found my own solution which may be more complicated than necessary but it works, here is the script:

// variables
var orig = draft.content;
var lines = orig.split(’\n’);
var j = 0; // variable for line 1
var k = 1; // variable for line 2
var ll1 = lines[j].length; // counts length of line 1
var ll2 = lines[k].length; // counts length of line 2

draft.selection = lines[1] // defines line 2 as ‘selection’
draft.selectionStart = ll1+ll2+1 // defines cursor position
var sel = lines[1]; // var for selection tag
var selst = ll1+ll2+1; // var for cursor position tag
draft.defineTag(“selection”,sel); // define selection tag
draft.defineTag(“selection_start”,selst); // define start tag

This script goes in front of the URL Callback action which should be changed to:

workflow://x-callback-url/run-workflow?name={{Drafts: get email address}}&input=[[selection]]&x-success={{drafts4://x-callback-url/replaceRange?uuid=[[uuid]]&start=[[selection_start]]&length=[[selection_length]]&retParam=result}}