Sending TaskPaper to a Grocery list

If there is anyone else on this forum that uses Drafts as a primary way of getting stuff into OmniFocus, I need some help getting a script and a URL scheme written for a specific drafts action I’m trying to pull off.

I want to run a script that places dashes in front of a each line of a list, and places a context at the end of each line. For example, I’d like to turn this:

milk
eggs
bacon
strawberries

Into this:

- milk @groceries
- eggs @groceries
- bacon @groceries
- strawberries @groceries

I’d then like to pass this via a URL scheme to a specific project in OmniFocus (In this case, the project would be called “Get Stuff from the Store”. Anyone know how to do this/where I can go to find help? Unfortunately I know no JavaScript. I can write URL schemes, but I’ve been having problems trying to figure out how to pass a taskpaper-formatted list into a specific project.

Thanks in advance for any help!

here’s a script you can use in drafts. It assumes you’ve previously set a prompt with key = context, so it can get the value of context_text .

// Script steps run short Javascripts
// For documentation and examples, visit:
// http://help.agiletortoise.com

var lines = draft.content.split("\n");
for (var ix=0; ix<lines.length; ix++) {
lines[ix] = " - " + lines[ix] +"@" + draft.getTag(‘context_text’);
}
draft.content = lines.join("\n")

Thanks! I’ll play with this when I get a minute (i.e. when I’m not at work).

Question though - when passing all of this stuff to OmniFocus, do I add the project that I’d like to add these items to in the URL scheme, or do I need to prepend…

Get Stuff from the Store:

…to the beginning of the list?

Building on the code from @dfay, here’s how you would apply it all to a specific project. The code is below for reference and the Drafts action is posted for download here.

The Drafts action would include two steps: (1) Script and (2) URL, both shown in detail below.

This code assumes a project called “Groceries” (defined in line 1) but you can change that to “Get Stuff from the Store” or any other name.

Code from Step 1, Script:

var project = “Groceries”;

var lines = draft.content.split("\n");
for (var ix=0; ix<lines.length; ix++) {
lines[ix] = " - " + lines[ix];
}
draft.content = lines.join("\n");

project = encodeURI(project);
tasklist = encodeURI(draft.content);

var fullUrl = “”;

fullUrl = fullUrl.concat(“omnifocus://x-callback-url/paste?target=/task/”,project,"&content=",tasklist,"&x-success=drafts4:///");

draft.defineTag(“full_url”, fullUrl);

Settings for Step 2, URL:

  • URL Template: [[full_url]]
  • Disable URL encode tag output

If you wanted to drop separate task lists into several different projects (e.g., Groceries to buy, Things to do at the beach, etc.) then you would create separate Drafts actions for each project (changing the project variable in line 1 for each action) and then launch each action separately.

1 Like