Send Draft to OmniFocus Task include text in "Note" of the task

Hi All,

I am using the script below to take a line in Draft that starts with a - and sending it to OmniFocus as a new task with the correct tag. This works really well. What I would like to add is the ability to have the script put information into the β€œNote” section of the OmniFocus task.

For example,

If I write this in Drafts:

  • Work with people to finish this project | I need to connect with Fred so he knows what to do.

After the right script was created, in OmniFocus it would look like this:

Task - Work with people to finish this project
Tag - Fred
Note - I need to connect with Fred so he knows what to do.

In the text from Drafts I’m using the β€œ|” as the divider between the title of the Task and what should go into the Note. I am not married to this so if there is a better way to do this I’m open to it.

Thank you!!!

// Add  - Fred - *
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("project", draft.displayTitle);
			cb.addParameter("context", "πŸ—’ - Fred");
			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("-", "πŸ—’ ");
1 Like

you need several things to achieve what you need. I’m not a very experienced javascript user so there might be a more efficient / elegant solution for your problem.
Here is what I would do (attention i just edited tthis online and did not test it so there may be some typos or syntax issues.:

const taskNoteDivider = "|";

// Add  - Fred - *
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))
		{
			// check if the divider is included in the task
			let taskName = "";
			let taskNote = "";
			if(strLine.includes(taskNoteDivider){
				// split the line into the task name and the content for the note
				let lineContent = p_strNewPrefix + strLine.substring(p_strIdentifier.length);
				let lineParts = lineContent.split(taskNoteDivider);
				taskName = lineParts[0];
				taskNote = linePart[1] + "\n" + draft.permalink;
			} else {
				taskName =  p_strNewPrefix + strLine.substring(p_strIdentifier.length);
				taskNote = draft.permalink;
			}
			// Build and call the URL
			let cb = CallbackURL.create();
			cb.baseURL = "omnifocus:///add";
			cb.addParameter("name", taskName);
			cb.addParameter("note", taskNote);
			cb.addParameter("project", draft.displayTitle);
			cb.addParameter("context", "πŸ—’ - Fred");
			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 this helps :slight_smile:

I want to thank you for taking the time to help. I really appreciate it. I am having a problem with the syntex. I keep getting the attached error. Any chance you can help me out with it (again?)?

It is telling you that this line (15) is missing a closing parenthesis.

if(strLine.includes(taskNoteDivider){

If you look at some of the other lines, you will see examples of where it is correct

if(strLine.includes(taskNoteDivider)){

The parentheses must always be balanced for opening and closing.

Hi,
Again, thank you! I fixed that and it threw this:

Script Error: ReferenceError: Can’t find variable: linePart
Line number: 20, Column 24

I am a newbie (clearly) and appreciate the help.

Just a quick scan.

image

I see one of these variable references seems to be missing an β€œs” from the end of the name. The let defines the variable, so that is the name that should be used throughout. Typo in the name simply refers to a different variable that may or may not exist.

That worked!!! Thank you so much! Not only does it work but I learned a little more about scripting. :slight_smile:

Really appreciate it.

1 Like

thanks @sylumer for fixing this - note to myself: at least check the syntax

You did note that there could be syntax issues, and it was just a couple of typos. I wish my unchecked responses were so accurate :laughing:

1 Like