NEBE - Help with adding a specific tag for Ominifocus and javascript

I am sure this is simple for everyone. I am not a programmer but I have been trying to create a getting things done style app by piecing things together.

The following script will take the “!@” that I put in front of a text and then add it to Omnifocus as a task. this all works. What I can’t get to work is I want to add a tag to the task. the tag will always be the same and in this case, I want it to be “Idea”.

I would be extremely grateful for any help on this.

Here is the script:

// Start every line with a @ to denote it as an idea

const IdeaPrefix = "!@ ";

// Function for removing the task prefix
function removeIdeaPrefix(s) {
  var f       = (IdeaPrefix),
      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(IdeaPrefix)) {

    // Remove the trigger from the line
    var idea = removeIdeaPrefix(line);
    var addTag = idea
    var alink = draft.permalink;
    console.log(idea);
    console.log(greg);
    console.log(alink);
    
    // OmniFocus URL Action
    doCallbackURL("omnifocus:///add", {"name": idea, "addTag": idea,  "note": alink, "autosave": true});
 }
}
1 Like

I’ve tried to simplify it a little, but I think this should work and also allow you to process a draft for things other than ideas too if you would like to.

Hopefully, you can follow the code (it is mostly built from a cut down version of the code you had), but if there’s anything you can’t figure out, just call it out and I or someone else on the forum should be able to help.

function processDraft(p_strIdentifier, p_strNewPrefix = "")
{
	// Check each line of the current draft
	for (let strLine of draft.lines)
	{
		// If the line starts with the task prefix, process it
		if (strLine.startsWith(p_strIdentifier))
		{

			// Build and call the URL
			let cb = CallbackURL.create();
			cb.baseURL = "omnifocus:///add";
			cb.addParameter("name", p_strNewPrefix + strLine.substring(p_strIdentifier.length));
			cb.addParameter("note", draft.permalink);
			cb.addParameter("context", "idea");
			cb.addParameter("autosave", "true");
			cb.open();
		}
	}
	return;
}

//Call the function to process the draft with the identifier and the optional prefix to use in OmniFocus.
processDraft("!@", "💡");

Hope that helps.

1 Like

Hi Sylumer,
I am very grateful that you replied and that it worked! I didn’t know what to expect because I don’t normally reply to boards like this. It paid off. It works and I am able to use it for different tags. I am trying to create a DRAFT page where I can type notes freely using markup language and then run a script to pull out task with tags. In the current case anywhere in the note that I start a line with !@ it now creates a new task in OMNIFOCUS with the idea tag.

If I may, as I am extending the scope a bit, ask another question. Is it possible to build off of your script to take the label of the DRAFT note and put it into the “Project” field in the new task created? For example, if the label for my DRAFT note is “Project_01”, and in the body of the note I have a line with “!@ this is an idea.” a new OMINIFOCUS task is created with the “Project_01” entered into the “Project” field.

Really appreciate this help!

Greg

1 Like

My reply might put you off using boards like this… :slight_smile:

I’m wondering why you’re using these custom markers and not the quite widespread Taskpaper format, wherein tasks begin with a dash and have nice things like @due().

And, since you mentioned OmniFocus, it also supports pasting in Taskpaper (from Drafts).

Other than perhaps not being familiar with Taskpaper, the usual reason for activities like this is that the syntax overlaps.

E.g. a hyphen in Taskpaper is a task, but in Markdown it is a list item; so if you are attempting to process tasks out of something (e.g. meeting notes) that isn’t purely a list of Taskpaper tasks, then this can cause interpretation/extraction ambiguity and require a custom marker to allow fora unique identification.

Drafts don’t have labels. By label I can’t tell if you mean a tag, a title, or something else, so I’ll try and cover a few options below. The OmniFocus URL scheme documentation indicates that projects can be added using the project parameter.

To add the title as the project name, I would use this code alongside the other addParameter() lines.

cb.addParameter("project", draft.displayTitle);

By using displayTitle rather than title, this would deal with things like Markdown headers.

Drafts tags are more complicated. If you always have exactly one tag, then you could use this:

cb.addParameter("project", draft.tags[0]);

However, each draft can have 0 or more tags, and OmniFocus tasks similarly. You could add in a check and optionally add a tag only if there is one tag to add like this:

if(draft.tags.length == 1) cb.addParameter("project", draft.tags[0]);

The add URL scheme is based on the old context approach and I don’t think supports multiple tags. The Taskpaper format @martinpacker references can be applied using the paste URL, and does support multiple tags. If you want the potential to use multiple tags, then either switching to Taskpaper notation, or building a Taskpaper structure on the fly from your own notation, would be what I would suggest as a way forward.

1 Like

A fair point. (Not to make this about md2pptx but I’ll observe that - as it supports Taskpaper and Markdown - I recommend only using asterisks for bullet points.)

And, I agree, Taskpaper might not be familiar to many people. But it does have the nice advantage of being supported by OmniFocus directly. The one issue there is whether tasks are collected at the bottom of the draft, or scattered throughout. (I would think the latter is more common than the former, and naturally so.)

Sylumer, again this is very helpful. I agree with the “title” vs. “Tags” in drafts. I will try using the displayTitle because that is really what I am looking for. I will report back if it works!

No worries, I am open to all and any suggestions. In this case, I agree with Sylumer that TaskPaper has some limitations. I wish I could find an out of the box solution but haven’t so I am trying to create my own.

What I want to do is:

  1. Have a Draft note open during a meeting and freely take notes using markup. I have created pre-defined text strings that if I start a line with them an Action will process it into Focus.

For example (Draft note simulation):

Title of Meeting is date and Project name from Focus

In today’s meeting we talked about a lot of things.

@ Need to remember to thank everyone for helping on this.

A topic came up that I need to research.

!!! research topic and get back to them

Next time I need to talk about something

@@ Service contract renewal


I have the following defined as task labels in OFocus
@ = Urgent
!!! = Research
@@@ = Agenda

I want to create the proper actions to take my text strings in Drafts and create new task in Focus. I have been able (with a lot of help) get the task into Focus and have the correct “tag” assigned to it. This is a big advance in my development.

What I want to be able to do now is write an Action that will put all of these new tasks into the correct project folder in OFOcus. In almost all cases, I will have. project folder already setup for the meeting therefore I can name the Draft with the Project name.

With all of this I have iOS scripts that automatically create the following Draft notes for me:

  1. Pre-meeting worksheet
    This is what I use to prepare for meetings. It loads all of my actions, by tags, from Focus so I have a snap shot for the meeting.
    Once I am finished preparing on this sheet I run the next iOS script to create the the Meeting Sheet

  2. Meeting Sheet
    This sheet uses the iOS script and assembles all of the information in the order that I want it based on the OFocus tabs.
    I have a section where I take notes during the meeting.
    After the meeting I run another iOS script that assembles the data and sends a follow up email to everyone

  3. Meeting Follow-up
    Currently I still have a Draft note created because I don’t trust just sending the email from my meeting notes.
    This is the cool part because I can re assemble all of my task and notes to only display what I want in the follow up email.

Once it is finished it will be pretty slick. Below is an example of the PreMeeting Daft markup created by iOS short cut. The sections show the tags I am using and those are the areas that get filled in when I process the short cut and actions:

Preflight worksheet for Test Project per 4/21/21

Project: Test Project, link to OmniFocus link

No meetings found (yet) for Test Project


  • Number of goals: 2

  • Number of results: 3

  • Number of items: 22 of which waiting for others 3 (Delegation ratio: 14%)

  • Number of items which are overdue: 22 (On time ratio: 0%)

  • Number of next available actions: 4 with an estimated time of hours

:round_pushpin:Added Agenda Topics

  1. 11 Apr 2021: The one new action item for day
  2. 14 Apr 2021: :calendar: Agenda item unchedked

:round_pushpin:Available Resources

  1. 08 Apr 2021: :computer: Resource
  2. 10 Apr 2021: :computer: Resource

:round_pushpin:Open Notes

No Open Notes found for this meeting.

:round_pushpin:Top Priorities

  1. 13 Apr 2021: :round_pushpin: Priority 1
  2. 13 Apr 2021: :round_pushpin: Priority 2
  3. 13 Apr 2021: :round_pushpin: Priority 1 2
  4. 13 Apr 2021: :mantelpiece_clock: Priority
  5. 13 Apr 2021: :round_pushpin: Priority

:round_pushpin:Waiting for…

  1. :coffee: Waiting for: 4/8/21 - :coffee: Waiting for with due date 4/13/21 - link;
  2. :coffee: Waiting for: 4/10/21 - :coffee: Waiting for with due date 4/13/21 - link;
  3. :coffee: Waiting for: 4/13/21 - :coffee: Waiting for with due date 4/13/21 - link;

:round_pushpin:Parked Items

  1. 08-Apr-2021 : :mantelpiece_clock: Check-back
  2. 10-Apr-2021 : :mantelpiece_clock: Check-back
  3. 13-Apr-2021 : :mantelpiece_clock: Check back on

:round_pushpin:Remaining Action Items

:black_square_button: 4/8/21 - no label with due date 4/8/21 - link;
:black_square_button: 4/8/21 - :dart:Action with due date 4/13/21 - link;
:black_square_button: 4/10/21 - :dart:Action Item with due date 4/13/21 - link;
:black_square_button: 4/13/21 - :dart:Action Item with due date 4/13/21 - link;

Decisions Previously Made

:handshake: No decisions yet

Closed notes
  1. Notes :spiral_notepad: - closed on Notes :spiral_notepad:
  2. Notes closed example - closed on Notes closed example
  3. :memo: These are the meeting notes and will be longer in most cases. - closed on :memo: These are the meeting notes and will be longer in most cases.
  4. :memo: Meeting notes checked - closed on :memo: Meeting notes checked
Previous Drafts

:memo: 13-Apr-2021 | Pre-Work for Test Project Link
:memo: 13-Apr-2021 | Pre-Work for Test Project _[Link](drafts5://open?uuid=DF256FA2-3D08-4DDA-B

1 Like

I forgot the “icing on the cake”…
When I am finished with the cycle drafts (per meeting draft, meeting draft and follow-up draft) I want to add them all into a group (named the same as the Project) in Devonthink.

1 Like