Skip to main content
dublove
Legend
Nov 11, 2026 10:29 AM UTC
Answered

Is there a more efficient way to distinguish where the cursor is located?

Hello everyone.

I feel that this method is not efficient.
Also, I'm puzzled why this sentence can't be written like this:

if (item instanceof (InsertionPoint || Text || Character || Word || Paragraph || TextColumn || TextStyleRange || Line || Table || Cell)) { return 'in' }

I've written everything, it feels a bit too much. Don't these Word Paragraph Character Lines all belong to "Text"?

var d = app.activeDocument;
var item = d.selection[0];
var curStatus = curStatus();
alert(curStatus);

function curStatus() {
if (item instanceof InsertionPoint
|| item instanceof Text
|| item instanceof Character
|| item instanceof Word
|| item instanceof Paragraph
|| item instanceof Line
|| item instanceof TextColumn
|| item instanceof TextStyleRange
|| item instanceof Table
|| item instanceof Cell
) { return 'in' }
//if (item instanceof (InsertionPoint || Text || Character || Word || Paragraph || TextColumn || TextStyleRange || Line || Table || Cell)) { return 'in' }

if (!item) { return 0; }
else { return 'out'; }
}

 

Also, can you use one sentence of code to distinguish whether the cursor is inside or outside the table?

    rob day
    rob dayCorrect answer
    Community Expert
    Nov 11, 2026 4:16 PM UTC

    Not sure I understand. You want to check if the selection is text, table, or an image? If that’s the case you could add an image property to include images. Keep in mind a selection could be multiple objects—selection[0] is the first item in an array of objects:

     

    (sel.hasOwnProperty("pointSize")||sel.hasOwnProperty("cells")||sel.hasOwnProperty("actualPpi"))

     

    19 replies

    rob day
    Community Expert
    Community Expert
    Nov 11, 2026 1:12 PM UTC

    Another option is to check for a text property with hasOwnProperty, and make sure something is selected:

     

    var sel = app.selection[0]
    if (sel == undefined) {
    alert("No Selection")
    } else if (sel.hasOwnProperty("pointSize")) {
    alert("In")
    } else {
    alert("Out")
    }

     

    Translate
    Community Expert
    Nov 11, 2026 10:38 AM UTC

    Try the following

    var sel = app.selection[0]
    if(sel.insertionPoints.length && !(sel instanceof TextFrame))
    alert("In")
    else
    alert("out")

     

    Translate
    -Manan
    dublove
    dubloveAuthor
    Legend
    Nov 11, 2026 1:16 PM UTC

    Hi ​@Manan Joshi ​@rob day 

    You have selected a table, it seems that it cannot be recognized.
    Also, the image in the text has been selected.

     

    Translate
    Community Expert
    Nov 11, 2026 1:23 PM UTC

    Those cases can be handled, with adding extra checks for anchored elements which would be a character and for table because table would always be inside a textframe. You will have to build upon my code to make it handle all the use cases you need.

    Translate
    -Manan