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

What is the code that determines the current selection?

Guide ,
Jun 23, 2025 Jun 23, 2025

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.

TOPICS
Scripting
247
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 1 Correct answer

Community Expert , Jun 23, 2025 Jun 23, 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 tex
...
Translate
Community Expert ,
Jun 23, 2025 Jun 23, 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.');

})();
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 ,
Jun 24, 2025 Jun 24, 2025

Hi m1b.

Thank you very much
Very useful reference for learning.

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 ,
Jun 24, 2025 Jun 24, 2025

@m1b 

(Please select the top two rows of the table)
This sentence I have tabulated is not correct, it should read:
Please drag and drop from the first row to select one or more rows.

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 ,
Jun 24, 2025 Jun 24, 2025

@m1b 

I tested it and found these problems.
The others are correct.

 

test();
function test() {
    var doc = app.activeDocument,
        item = doc.selection[0];
    if ('Image' === item.constructor.name)
        alert('You have selected a graphic.');
}

I have selected the image and run the script.
The alert doesn't pop up anything.

 

 

test();
function test() {
    var doc = app.activeDocument,
        item = doc.selection[0];
    if (
        item.hasOwnProperty('texts')
        && 1 === item.texts.length
    )
        alert('You have selected something with text.');
}

I have selected the text and run the script.
The alert does not pop up

 

 

 

test();
function test() {
    var doc = app.activeDocument,
        item = doc.selection[0];
    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.');
}

No matter what you choose, this one doesn't pop up with a bail alert message either.

This one asked for it, and I initially indicated an error.
Now corrected:
is only a hint: you should drag and drop to select multiple rows from the first row.
(If you drag and drop the selection from another row, or just place the cursor in the row, this pops up as a hint: you should drag and drop from the first row to select the table header row. It doesn't matter how many rows you select)

 

If you didn't choose like this, prompt.

dublove_0-1750837897199.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 ,
Jun 24, 2025 Jun 24, 2025

@dublove I don't understand any of your comments very well but I'll have a try

 

1. I don't know what "no hint" means here. Tell me when this doesn't work.

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

 

2. we can add a check to see if it is an insertion point only:

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

 

3. Do you mean that you don't care if the whole row is selected... just any cell on both 1st and 2nd rows? If so, then just comment out the predicate for it:

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.');

 

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 ,
Jun 24, 2025 Jun 24, 2025

I'll modify my point above.

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 ,
Jun 24, 2025 Jun 24, 2025

If you're not sure what you have selected, try run this script (and keep it handy):

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

 

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 ,
Jun 24, 2025 Jun 24, 2025

I still don't understand your points.

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 ,
Jun 25, 2025 Jun 25, 2025
LATEST

I'll modify my point above.

It should be understandable this time.

 

Thank you very much.

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
New Here ,
Jun 23, 2025 Jun 23, 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

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