Skip to main content
dublove
Legend
June 24, 2025
Answered

What is the code that determines the current selection?

  • June 24, 2025
  • 2 replies
  • 635 views

I need a hint:

Prompts a message when no correct selection is made:


    Please select an image frame.
    Please select the text.
    Please place the cursor in the frame
    Please select the top two rows of the table.
    Please place the cursor in the table row.

 

Thank you very much.

Correct answer m1b

Hi @dublove have a look at this:

(function () {

    var doc = app.activeDocument,
        item = doc.selection[0];

    if (!item)
        alert('You have nothing selected.');

    if ('Image' === item.constructor.name)
        alert('You have selected a graphic.');

    if (
        item.hasOwnProperty('graphics')
        && 1 === item.graphics.length
    )
        alert('You have selected an image frame.');

    if ('TextFrame' === item.constructor.name)
        alert('You have selected a text frame.');

    if (
        item.hasOwnProperty('texts')
        && 1 === item.texts.length
    )
        alert('You have selected something with text.');

    if ('InsertionPoint' === item.constructor.name)
        alert('Your cursor is in the text');

    if (
        'InsertionPoint' === item.constructor.name
        && 'Cell' === item.parent.constructor.name
    )
        alert('Your cursor is in a table row.');

    if (
        // table cell selections are always 'Cell'
        'Cell' === item.constructor.name
        // two rows
        && 2 === item.rows.length
        // rows are first and second
        && 0 === item.rows[0].index
        && 1 === item.rows[1].index
        // both whole rows are selected
        && item.cells.length == item.rows.everyItem().cells.length
    )
        alert('You have selected the top two rows of the table.');

})();

2 replies

Participant
June 24, 2025

Hello,

that fits a situation where the user is supposed to select something — but hasn’t selected anything at all.

"Please select an image frame." – Very specific, applies only to images.

"Please select the text." – Generic, but only applies to a text selection.

"Please place the cursor in the frame." – Requires cursor placement, not selection.

"Please select the top two rows of the table." – Very specific again, not general.

"Please place the cursor in the table row." – Again about cursor, not selection.

 

Best Regard,

Sally

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
June 24, 2025

Hi @dublove have a look at this:

(function () {

    var doc = app.activeDocument,
        item = doc.selection[0];

    if (!item)
        alert('You have nothing selected.');

    if ('Image' === item.constructor.name)
        alert('You have selected a graphic.');

    if (
        item.hasOwnProperty('graphics')
        && 1 === item.graphics.length
    )
        alert('You have selected an image frame.');

    if ('TextFrame' === item.constructor.name)
        alert('You have selected a text frame.');

    if (
        item.hasOwnProperty('texts')
        && 1 === item.texts.length
    )
        alert('You have selected something with text.');

    if ('InsertionPoint' === item.constructor.name)
        alert('Your cursor is in the text');

    if (
        'InsertionPoint' === item.constructor.name
        && 'Cell' === item.parent.constructor.name
    )
        alert('Your cursor is in a table row.');

    if (
        // table cell selections are always 'Cell'
        'Cell' === item.constructor.name
        // two rows
        && 2 === item.rows.length
        // rows are first and second
        && 0 === item.rows[0].index
        && 1 === item.rows[1].index
        // both whole rows are selected
        && item.cells.length == item.rows.everyItem().cells.length
    )
        alert('You have selected the top two rows of the table.');

})();
dublove
dubloveAuthor
Legend
June 24, 2025

Hi m1b.

Thank you very much
Very useful reference for learning.