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

How do I convert the selection into a final array object?

Guide ,
Nov 13, 2025 Nov 13, 2025

Hi everyone.

My function only accepts “objects.” So I first need to convert the selection state into “objects,” ideally in array form.

My model is structured as follows:

Some parts are incorrect; please point them out.

Regardless of the cursor's state, the final `sel` should be an array object with a "length" property.

Thank you.

 

Currently, I cannot determine the three states of the cursor when it selects an image (including white arrow, black arrow, and pasted inside).

Additionally, the cursor's position within and outside the table must be considered.

 

var doc = app.activeDocument;
// item represents the specific state of the cursor
var item = doc.selection[0];

// items holds multiple selected objects, e.g., multiple images or text boxes selected simultaneously.
var items;

// sel is my target for operations and must be an array object (containing text boxes, images, geometric shapes)
var sel;

// Example: When cursor is in a text frame (not within a table)
var textFrames = [];
var item = doc.selection[0];
if (item && item.hasOwnProperty(‘parentTextFrames’) && “Table” != item.parent.parent.constructor.name) {
sel = item.parentTextFrames;
for (var j = 0; j < sel.length; j++) {
movePBSel(sel[j], doc)
imageFitToColumn(sel[j]);
};
exit();
}

// When an image is selected
var gra = (item.hasOwnProperty(‘graphics’) && 1 === item.graphics.length);
if (
(gra && item.parent.parent.constructor.name != “Story”))
{
items = doc.selection;
if (“Image” == item.parent.constructor.name) {
Items = item.parent;
}
sel = items;
for (var j = 0; j < sel.length; j++) {
imgFit(sel[j])
};
exit();
}

 

TOPICS
How to , Scripting
172
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 , Nov 13, 2025 Nov 13, 2025

@dublove you are labouring under a misaprehension. Please forget about white cursor or black cursor. They are a distraction from the reality. Instead you should get to understand what you actually have selected.

 

Try this simple script:

/**
 * @file List Ancestry Of Selected Items.js
 *
 * Helper tool for understand what you have selected.
 * Shows the ancestry of each selected item.
 *
 * @author m1b
 * @version 2025-11-15
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-
...
Translate
Community Expert ,
Nov 13, 2025 Nov 13, 2025

@dublove you are labouring under a misaprehension. Please forget about white cursor or black cursor. They are a distraction from the reality. Instead you should get to understand what you actually have selected.

 

Try this simple script:

/**
 * @file List Ancestry Of Selected Items.js
 *
 * Helper tool for understand what you have selected.
 * Shows the ancestry of each selected item.
 *
 * @author m1b
 * @version 2025-11-15
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-do-i-convert-the-selection-into-a-final-array-object/m-p/15590114
 */
(function () {

    var doc = app.activeDocument;
    var sel = doc.selection;

    var result = [];

    for (var i = 0; i < sel.length; i++) {
        result.push(i + ':' + listAncestors(sel[i]));
    }

    alert(sel.length ? 'Selection\n' + result.join('\n') : 'Nothing selected');

})();

/**
 * Return list of full ancestry of `item` up to Document.
 * @author m1b
 * @version 2025-11-15
 * @param {PageItem} item - an Indesign page item.
 * @returns {String}
 */
function listAncestors(item) {

    var ancestry = [];

    do {

        ancestry.push(item.constructor.name);
        item = item.parent;

        if ('Story' === item.constructor.name)
            // now follow the text frame, not the story
            item = item.textContainers[0];

    } while ('Document' !== item.constructor.name);

    ancestry.push(item.constructor.name);

    return ancestry.reverse().join('\u2192');

}

 

Try running this script with every different kind of selection. This will give you critical information for your scripting predicates.

 

Examples:

• I put insertion point in a Table: the alert says Document→Spread→TextFrame→Table→Cell→InsertionPoint

• I select the graphic (not it's frame) that is anchored in text in a table cell: the alert says 

Document→Spread→TextFrame→Table→Cell→Character→Rectangle→PDF

 

Does that makes sense? Play with this until you understand all the things. Your scripts may need to handle these cases. I hope you will find this useful. And please forget about black cursor and white cursor stuff!

- Mark

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 ,
Nov 13, 2025 Nov 13, 2025

Thank you very much.
This is quite interesting.
It reveals the hierarchical relationship between parent and child elements.

 

I used to find determining the cursor's state quite tedious, requiring listing all possible scenarios (Text Cell InsertPoint Word Paragraph Character).
I wanted to obtain the parent target with just one simple line of code: 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
Community Expert ,
Nov 13, 2025 Nov 13, 2025

@dublove good. You may find this function useful too, then...

/**
 * @file Get Ancestor Of Selected Item.js
 *
 * Demonstration of getting a particular ancestor of the selected item.
 *
 * @author m1b
 * @version 2025-11-15
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-do-i-convert-the-selection-into-a-final-array-object/m-p/15590114
 */
(function () {

    var doc = app.activeDocument;
    var sel = doc.selection[0];
    var table = getAncestor(sel, 'Table');

    if (table)
        alert('Found a table.');
    else
        alert('No table ancestor.');

})();

/**
 * Return item's named ancestor, if found.
 * @author m1b
 * @version 2025-11-15
 * @param {PageItem} item - an Indesign page item.
 * @param {String} ancestorName - the constructor name of the target ancestor.
 * @returns {*?}
 */
function getAncestor(item, ancestorName) {

    var ancestor = item;

    while (
        'Document' !== ancestor.constructor.name
        && ancestor.constructor.name !== ancestorName
    ) {
        ancestor = ancestor.parent;

        if ('Story' === ancestor.constructor.name)
            // now follow the text frame, not the story
            ancestor = ancestor.textContainers[0];

    }

    if (ancestor.constructor.name === ancestorName)
        return ancestor;

};

 

Run this script with different selections to see when it can return a Table and when not.

- Mark

 

Edit 2025-11-14: fixed bug in function.

Edit 2025-11-15: improved function so that ancestry follows the text frame, not the story.

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 ,
Nov 13, 2025 Nov 13, 2025

When your cursor is not inside the table, it will directly cause InDesign to close.

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 ,
Nov 13, 2025 Nov 13, 2025

Oops! Fixed now.

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 ,
Nov 14, 2025 Nov 14, 2025

Hi @dublove  and @m1b , Could the selected text be in a text frame inside of a table cell? In that case I get this:

 

Screen Shot 1.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
Community Expert ,
Nov 14, 2025 Nov 14, 2025
LATEST

Thanks @rob day great catch! For the purpose of both these little scripts, we need to follow the text frame, not the story. I have updated both scripts now.

- Mark

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