Create a RichText note with Javascript for Automation

I’m integrating the QuickEntry with my Outlook workflow, and every thing looks great. Now I’m trying to make the task note a bit more beautiful with RichText.

I look on lots of forums and posts but apparently no one needed to do this:

Create a RichText note equivalent to the following HTML:

My Linked Text

I’m aware of this post (Making link clickable in OF2), but besides not having a clear answer for the question, it’s using Applescript instead of Javascript. Does anyone know how to do this?

I’m not familiar with Javascript for Automation, but I can give you a starting point.
Try this code. It set the note of value of first tree of content of front document window


    var of = Application('OmniFocus'),
    oValue = of.defaultDocument.documentWindows[0].content.trees[0]['value']();
    of.strictPropertyScope = true
    of.strictCommandScope = true
    
    oValue.note = 'OmniFocus'
    oValue.note.paragraphs[0].style.attributes['link'].value = 'https://www.omnigroup.com/omnifocus';

Yeah, that didn’t help. was able to create a RichText object with the same structure as your example:

OmniFocus.RichText({text: sender.name, style: {attributes: {link: {value: `mailto:${sender.address}`}}}})

But this does not generate anything on the Note.

Ok, but if I run my code, I’am getting this:

And if I click the word “OmniFocus”, the browser opens in this address: https://www.omnigroup.com/omnifocus
Don’t you experience this behavior?

I agree. The problem is that your code depends on a task being already present.
What I’m trying to achieve is this:

OmniFocus = Application("com.omnigroup.OmniFocus2");
OmniFocus.includeStandardAdditions = true;
OmniFocus.strictPropertyScope = true
OmniFocus.strictCommandScope = true

var task = OmniFocus.InboxTask({
    name: "Email task",
    note: OmniFocus.RichText({
        text: "Some Person",
        style: {
            attributes: [
                {
                    name: "link",
                    value: "http://www.yahoo.com"
                }
            ]
        }
    })
});

OmniFocus.activate();
OmniFocus.quickEntry.inboxTasks.push(task);

As you can see, I don’t have the task beforehand and the new task, although it is created on the Quick Entry panel, it does not have a note.

Ah, now I understand.
Maybe this helps.

Code:

    var of = Application('OmniFocus'),
    ofDoc = of.defaultDocument,
    inbox = ofDoc.inboxTasks;
    
    //of.strictPropertyScope = true
    //of.strictCommandScope = true
    
    task = of.InboxTask({name: 'New Task', note:'OmniFocus'});
    num = of.quickEntry.inboxTasks.push(task);
    of.quickEntry.inboxTasks[num - 1].note.paragraphs[0].style.attributes['link'].value = 'https://www.omnigroup.com/omnifocus';
    
    of.quickEntry.open();

I think I got it. I have to add the task to the quick entry in order to have a RichText note.

I couls use the .insert() method on the RichText class to add stuff to it but I need to find how to use it. Its documentation says:

insert method : Insert text in the middle of an existing blob of text.
insert text : the string to insert.
at: location specifier : Where to insert the text.
[using: Style] : The style to use when inserting the text. If missing, the default style for this text will be used.

But I have no idea how to define the “at” argument of it. I tried:

of.quickEntry.inboxTasks[num - 1].note.insert({
  text: "This is a test",
	at: 0
});

But it gives me the “Can’t convert types.” error.

In Applescript, the at parameter would be, for example:

at before paragraphs

The problem is, I think, relative reference forms aren’t working very well in JXA. This is my hipotesis based on my investigation.
One option would be: create the note text and then set the value of the attribute in the desired paragraph.
Example:


    var of = Application('OmniFocus'),
    ofDoc = of.defaultDocument,
    inbox = ofDoc.inboxTasks;
    
    //of.strictPropertyScope = true
    //of.strictCommandScope = true
    
    task = of.InboxTask({name: 'New Task', note:'Welcome to OmniFocus\n\nOmniFocus HomePage'});
    num = of.quickEntry.inboxTasks.push(task);
	
	of.quickEntry.inboxTasks[num - 1].note.paragraphs[2].style.attributes['link'].value = 'https://www.omnigroup.com/omnifocus';
    of.quickEntry.open();

Do you know what was supposed to be the syntax for location specifiers?

As it happens I’m not an OF user so I can’t test specifically, but generally, objects in JXA dictionaries tend to have .before() and .after() functions on them, which should return values of the ‘location specifier’ variety.

Unfortunately for Paragraphs and Words, the before() and after() functions do not retun a location specifier.