Skip to main content
alidabbs
Inspiring
January 29, 2018
Answered

Insert Note at text cursor

  • January 29, 2018
  • 2 replies
  • 1718 views

Please accept my sincere apologies for the very basic question but I've been a scripting beginner for 20 years and am still struggling.

I would like an InCopy script to insert a Note (containing predetermined text) at the cursor insertion point in text. I thought I would simply adapt the InsertNote.jsx sample supplied with the scripting guide but I don't understand how to get the Note to insert at the text cursor position, rather than at the beginning or end of the story.

Can anyone help me, please? I can't seem to find the answer in the scripting guide in either the Text or Notes chapters.

Thanks,

Alistair

This topic has been closed for replies.
Correct answer TᴀW

Try this:

myNote = app.selection[0].notes.add();

myNote.texts[0].contents = "Hello World!";

This will add a note at the cursor position, and add "Hello World!" to its contents.

Tested in InDesign, but will probably work in InCopy as well.

Whatever is currently selected can be accessed through app.selection[0]. So if you've got a text cursor flashing in the middle of some text, this will return an InsertionPoint. InsertionPoints have a "notes" property, and typically whenever something has a plural property (textFrames, notes, rectangles), that plural property has an add() method which returns the same object in the singular (textFrame, note, rectangle) which is a single instance of the object.

HTH,

Ariel

2 replies

Jongware
Community Expert
Community Expert
January 29, 2018

It's easier than you think, although I agree that the example is rather useless. A Note gets added to an insertion point; and an insertion point is every position that you can click a text cursor on.

The text cursor position is always a selection; its length is the length of the selected text and may thus be 0.

This means you only need this short code to add a note at the cursor:

var myNote = app.selection[0].insertionPoints.item(0).notes.add();

//Add text to the note

myNote.texts.item(0).contents = "This is a note.";

You can experiment to see how the "insertion point" code inside a selection works if you select more than one character: the note will always appear at the start of the selection, which is its "item(0)".

alidabbs
alidabbsAuthor
Inspiring
January 29, 2018

Thank you, J, that's absolutely perfect. Very much appreciated.

Alistair

TᴀW
TᴀWCorrect answer
Legend
January 29, 2018

Try this:

myNote = app.selection[0].notes.add();

myNote.texts[0].contents = "Hello World!";

This will add a note at the cursor position, and add "Hello World!" to its contents.

Tested in InDesign, but will probably work in InCopy as well.

Whatever is currently selected can be accessed through app.selection[0]. So if you've got a text cursor flashing in the middle of some text, this will return an InsertionPoint. InsertionPoints have a "notes" property, and typically whenever something has a plural property (textFrames, notes, rectangles), that plural property has an add() method which returns the same object in the singular (textFrame, note, rectangle) which is a single instance of the object.

HTH,

Ariel

id-extras.com | InDesign tools & scripts for typesetters, form designers, and translators
alidabbs
alidabbsAuthor
Inspiring
January 29, 2018

Ooh lovely, that did the trick. Thank you ever so much!

Alistair

Community Expert
January 29, 2018

Hi alidabbs ,

and if you inspect DOM documentation you will see, that also formatted text (also included anchored frames) can be moved or duplicated to an added Note object ( and also out of it ). In fact you do nothing else if you do text.convertToNote() or note.convertToText() .

Regards,
Uwe