Add link to OF task? [SOLVED]

I see several people have asked for a way to add a link from a created OF task back to some other iOS artifact, but I don’t see any responses.
What I would like to do is create an OF task with a note contains a link back to the Drafts text that created it.
If I create a task in OF myself, adding a URL in the note field, then copy that task to the clipboard, it looks like this:

- A test note for linking from OmniFocus. @parallel(true) @autodone(false)
	http://example.com

I can create this exact structure in a Drafts action, using draft.permalink for the URL, etc. When the /paste callback is executed, OmniFocus treats the text as a group, and the URL as a sub task within that group. Any help greatly appreciated.

Ref: Original code

// Start every line with a - to denote it as a task

const taskPrefix = "-";

// Function for removing the task prefix
function removeTaskPrefix(s) {
  var f       = (taskPrefix),
      r       = "",
      re      = new RegExp(f,"g"),
      matches = s.match(re);

  if (matches) {
    return s.replace(re,r);
  }
}

// Function to perform the callback url
function doCallbackURL(url, params) {
  var cb = CallbackURL.create();
  cb.baseURL = url;

  for(var key in params) {
   cb.addParameter(key, params[key]);
  }

  var success = cb.open();
  if (success) {
    console.log("Event created");
  } else {
    console.log(cb.status);
    if (cb.status == "cancel") {
      context.cancel();
    } else {
      context.fail();
    }
  }
}

// Scan for the task prefix in the draft
var lines = draft.content.split("\n");

for (var line of lines) {
  // If the line includes the task prefix, 
  // we remove exclude it from the final notes
  if (line.startsWith(taskPrefix)) {

    // Remove the trigger from the line
    var task = removeTaskPrefix(line);
    task += "@parallel(true) @autodone(false)\n\t";
    task += draft.permalink;
    console.log(task);
    
    // OmniFocus URL Action
    doCallbackURL("omnifocus://x-callback-url/paste", {
      "content": task,
      "target": "inbox"
    });
 }
}

Here’s the text sent to the console log by the code above:

 A test note for linking from OmniFocus.@parallel(true) @autodone(false)
	drafts5://open?uuid=1589AE0F-E0BF-40BA-9944-47BE4D90E348
Event created
Script step completed.
Tags "meetings" assigned.

I worked things out.
Link to the Action here:

https://actions.getdrafts.com/a/1Re

Glad it worked out for you. What was the key learning point?

It would be hard to pick just one:

  • I’d never worked with JavaScript
  • I’d never used an x-callback-url
  • I’d never written a Drafts action
  • the add function behaves differently than paste, and accepts the note parameter - it’s also a url, rather than a callback
  • the colons for the parameters are outside the quotes - when looking at example urls, I thought the url was going to be constructed with colons, rather than equals. This was when I thought the colons were inside the quotes
  • autosave=true is not listed in the OF docs, but is used in an example in the docs
1 Like