Skip to main content
Known Participant
September 7, 2008
Question

Place some text at cursor position

  • September 7, 2008
  • 11 replies
  • 4348 views
Probably there is an easy solution but I can not see it.

I would like to help my users to "mark" some text as they enter it. I will display a custom dialog asking for the text to enter at the current cursor position; as they enter some text in my dialog textEditbox and press OK I will display that text,enclosed in square brackets, at the current cursor position.

My problem is here: how can I place some text at the current cursor?

Thank you
carlo
This topic has been closed for replies.

11 replies

Known Participant
September 9, 2008
Thank you Olav,

I grasped your point but you will admit that the selection of an insertion point is a bit more abstract with respect to the usual selection of text you copy and paste.

Carlo
Known Participant
September 8, 2008
Hi carlo,

re: "I never realized that "selected" does not mean "selected" in Indesign!"

An insertion point is a selection. Try this: deselect all (either choose it from the Edit menu or press Command-Shift-A (Mac OS) or Ctrl-Shift-A (Windows). Look at the Control panel and other user interface items. Now click an insertion point in a text frame. InDesign's user interface changes when you have a text selection, including an insertion point.

Thanks,

Ole
Known Participant
September 8, 2008
oops, you gave evidence to the fact my knowledge of Indesign is limited to the ExtendedScript Toolkit and the Scripts Panel!

Thanks
carlo
Peter Kahrel
Community Expert
Community Expert
September 8, 2008
>2 textframes where I select some text in each oh them.

Can you do that?
Known Participant
September 8, 2008
Thanks all of you.
I never realized that "selected" does not mean "selected" in Indesign!
As Peter explained to me: "app.selection[0]" is a bit misleading here: with the cursor somewhere in a text frame, in the UI nothing is selected, but from a script's perspective, an insertion point is selected.

Before your posts I had solved with this:

var mydocument=app.documents.item(0);
if (app.selection.length == 1){
//Evaluate the selection based on its type.
switch (app.selection[0].constructor.name){
case "InsertionPoint":
case "Character":
case "Word":
case "TextStyleRange":
case "Line":
case "Paragraph":
case "TextColumn":
case "Text":
case "Story":
break;
}}
else{
alert("Please select some text.");
}
function myProcessText(testo) {
testo.contents=' ['+testo.contents+']'
}

But your code to check if the selection is text is more concise.

Very last question: how could I get the same but with multiple selection? I mean for example 2 textframes where I select some text in each oh them.

carlo
Peter Kahrel
Community Expert
Community Expert
September 7, 2008
Carlo,

No, that line of mine doesn't assume that anything is selected. If the user has an insertion point selected ("has the cursor somewhere, even in an empty text frame"), then the text is entered at the insertion point. To combine Dave's test and my line, this would seem a safe approach:

if (app.documents.length > 0 &&
app.selection.length == 1 &&
app.selection[0].constructor.name == "InsertionPoint")){
app.selection[0].contents = myText;
}

"app.selection[0]" is a bit misleading here: with the cursor somewhere in a text frame, in the UI nothing is selected, but from a script's perspective, an insertion point is selected. You can use this to put the cursor somewhere in a text. This line:

myParagraph.insertionPoints[0].select();

places the cursor at the beginning of myParagraph.

Peter
Inspiring
September 7, 2008
Before there can be a selection, there must be a document. Text selections always have a length of 1. Text objects have a baseline property, non-text objects don't. Hence:
if (app.documents.length > 0 &&

app.selection.length == 1 &&
app.selection[0].hasOwnProperty("baseline")) {
processTextSelection(app.selection[0]);
}
Dave
try67
Community Expert
Community Expert
September 7, 2008
You don't need to force the user to select text, but they will need to select an object to put the text into (by clicking on it). A textframe (empty or not) would do just fine. Otherwise, you need to decide where to put that text without knowing where the mouse is at that moment.
Known Participant
September 7, 2008
thank you both for yout help.

I would like not to force user selecting some text before marking: I suppose Peter suggestion works only if some selection has been done (correct?)

To try67, in general there is no other object: could be an empty textframe where user sets the cursor, or it could be some text before and he has to mark the following word .

carlo
try67
Community Expert
Community Expert
September 7, 2008
Carlo, is there already any kind of object at the location of the cursor when the script is run, and the user has already selected it? If so, use Peter's suggestion.
If you need to create the object into which you place the text, I'm not sure that's possible with a script. As far as I know you can't get the current mouse position, at least not with JavaScript.