Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How can we determine the different operation targets of sel[j] based on the cursor state?

Guide ,
Aug 08, 2025 Aug 08, 2025

I am not very clear about the current selection object and its hierarchical relationships, especially the representation of the parent level. Could someone clarify these relationships?


For example:
Situation 1: When the cursor is outside the table
    A. When the cursor is in a paragraph with no selection, sel[j] is the entire paragraph.
    B. If part of the text is selected, sel[j] is the selected text. (This includes selecting all text within the text box.)
    C. If the text box is selected, sel[j] is all content within the text box (including images, text, and tables).

 

Situation 2: When the cursor is inside a table
   D. If the cursor is within a cell, sel[j] is the current cell.
   E. If multiple cells are selected via drag, sel[j] is the selected cells.
   F. If the entire table is selected, sel[j] is the entire table.

 

Thank you very much.

TOPICS
How to , Scripting
541
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Aug 09, 2025 Aug 09, 2025

You can get the parents of a selection like this— a selected word in a table cell

 

 

var sp = app.documents[0].selection[0]
alert("\n" + sp.constructor.name + "\n" + sp.parent.constructor.name+ "\n" + sp.parent.parent.constructor.name+ "\n" + sp.parent.parent.parent.constructor.name+ "\n" + sp.parent.parent.parent.parent.constructor.name)

 

Screen Shot 46.png

Translate
Community Expert , Aug 10, 2025 Aug 10, 2025

If you have multiple cells selected the following will give you text of the cells

app.selection[0].cells.everyItem().contents

If you have some text selected which is outside the table and have the table selected as well. then the following will get you contents of all the cells in the selected table

app.selection[0].tables.everyItem().cells.everyItem().contents

If the extire frame is selected then the following would work

app.selection[0].tables.everyItem().cells.everyItem().contents

-Manan

Translate
Community Expert ,
Aug 08, 2025 Aug 08, 2025

When the cursor is in a text within or outside the table without any selection then it is insertionPoint. What you can do is make different selections with whatever use case you want and then run the following code

alert(app.selection[0].constructor.name)

-Manan

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 09, 2025 Aug 09, 2025

For example, how do you indicate the entire paragraph where the cursor is located?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 09, 2025 Aug 09, 2025
app.selection[0].paragraphs[0].contents

Should give you the paragraph where the current cursor is placed

-Manan

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 09, 2025 Aug 09, 2025

What you mentioned above should be selecting some text.


When I want to indicate the entire paragraph, the cursor is only on the line and nothing is selected.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 09, 2025 Aug 09, 2025

cursor is only on the line and nothing is selected

 

If you want to change the selection to the paragraph containing an insersion point it would be this:

 

//a selected insertion point
var sel = app.documents[0].selection[0];
//select the insertion point’s paragraph
app.select(sel.paragraphs[0], SelectionOptions.replaceWith)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 09, 2025 Aug 09, 2025

It really became selected. 
Perhaps so, but I just wanted to convert the target to the entire segment, and I thought I had to use parent to indicate that.

 

I actually wanted to clear the preferred options based on different situations.

I'm starting to doubt my idea...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 09, 2025 Aug 09, 2025

This changes the properties of the insertion point’s paragraph

 

//the paragraph the insertion point is in
var sp = app.documents[0].selection[0].paragraphs[0];

//change the properties of the selection’s paragraph
sp.properties = {pointSize:15, leading:20, fillTint:40}

 

 

Screen Shot 43.png

 

Screen Shot 45.png

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 09, 2025 Aug 09, 2025

I mainly want to use a script to implement multiple functions.
Determine the operation object based on the cursor status.

So I want to know how to determine the cursor status and how to convert to the target.

 

Take clearing overtype as an example.

If the cursor is in a cell, clear the overtype of the current cell.
If I select some cells, clear the overtype of those cells.
If I select the entire text box, clear all text in the text box and all overtype in all tables.

 

Is it like setting a shortcut key to clear overtype?
It seems that this is not entirely the case.

 

I don't have to worry about whether the current selection matches.
Any possible selection target has the corresponding operation ready.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 09, 2025 Aug 09, 2025

You can get the parents of a selection like this— a selected word in a table cell

 

 

var sp = app.documents[0].selection[0]
alert("\n" + sp.constructor.name + "\n" + sp.parent.constructor.name+ "\n" + sp.parent.parent.constructor.name+ "\n" + sp.parent.parent.parent.constructor.name+ "\n" + sp.parent.parent.parent.parent.constructor.name)

 

Screen Shot 46.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 09, 2025 Aug 09, 2025

 

If I drag and select several cells, how do I get the text content?

dublove_4-1754806195072.jpeg

If I select text and tables outside the table, how do I get the text in the cells?

Sunil Yadav wrote this before, and it is also invincible.

I don't know why there are two text[0].text[0]

Occasionally, it may fail.

obj.texts[0].tables.everyItem().cells.everyItem().texts[0].texts[0].appliedParagraphStyle = "myTabBodyPar";

dublove_5-1754806258344.jpeg

 

If I select the entire text Frame, how can I retrieve the text in the cell?

dublove_6-1754806298211.jpeg

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 10, 2025 Aug 10, 2025

If you have multiple cells selected the following will give you text of the cells

app.selection[0].cells.everyItem().contents

If you have some text selected which is outside the table and have the table selected as well. then the following will get you contents of all the cells in the selected table

app.selection[0].tables.everyItem().cells.everyItem().contents

If the extire frame is selected then the following would work

app.selection[0].tables.everyItem().cells.everyItem().contents

-Manan

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 10, 2025 Aug 10, 2025

HI 

The rest basically worked.
But this seems wrong. I only want to change the currently selected "te666xt b", not "chose".

 

The constructor.name here also seems uncertain. It could be Text, Word, Character, all numbers, or TextStyleRange.

My target audience seems to be wrong.

 

alert(app.selection[0].constructor.name);
if (app.selection[0].constructor.name == "Text") {
    selWord();
}
function selWord() {
    app.selection[0].texts[0].appliedParagraphStyle = parStn;
}

dublove_0-1754826453243.jpeg

 

What is TextStyleRange? TextStyleRange? It's not called "number"?

dublove_0-1754828203966.jpeg

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 10, 2025 Aug 10, 2025

The constructor.name here also seems uncertain.

 

You have more than a word and less than a paragraph selected so its constructor name is Text

 

The constructor name depends on what is selected:

 

alert(sp.constructor.name)

 

Screen Shot 47.png

Screen Shot 48.png

Screen Shot 49.png

Screen Shot 50.png

Screen Shot 51.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 10, 2025 Aug 10, 2025

Hi @Manan Joshi  @rob day 

I also feel that the code is repetitive and verbose. Can we represent the target object using parameters?

selTextFrame():
function selTextFrame() {app.selection[0].tables.everyItem().cells.everyItem().texts[0].appliedParagraphStyle = bodyParStn;
}

I also want to change it to the following, so that several possibilities can share one function: General, It seems like it won't work.

selGeneral(app.selection[0].tables.everyItem().cells.everyItem().texts[0],ps);

function selTextFrame(tg,ps) {
tg.appliedParagraphStyle = ps;
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 10, 2025 Aug 10, 2025

You will run into problems if you assume the selection includes a table. If I wrap the selection in a try statement :

 

try {
    alert(app.selection[0].tables.everyItem().cells.everyItem().texts[0])
}catch(e) {
    alert(e)
}  

 

Screen Shot 52.png

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 10, 2025 Aug 10, 2025

No, everything is normal.

if (app.selection[0].constructor.name == "Cell") {
    selCells();
}
function selCells(){
app.selection[0].tables.everyItem().cells.everyItem().appliedCellStyle = bodyStn;
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 10, 2025 Aug 10, 2025
LATEST

Thank you very much, both of you.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines