Skip to main content
dublove
Legend
March 24, 2025
Answered

Is it possible to automatically determine my current target selection using only one variable?

  • March 24, 2025
  • 2 replies
  • 315 views

If the cursor is in a text box, it is assumed that the current target is this article.
If a text or multiple textboxes are selected, it is assumed that the current target is the article in those textboxes.
If nothing is selected, a prompt pops up asking if I want to target the entire document.

target:app.activeDocument.selectione[0].parent;

Thanks.


Correct answer m1b

@dublove this is one way to do it:

/**
 * Example of starting a script by getting the
 * selected stories or the whole document, but first
 * confirming whether to process the whole document.
 * @author m1b
 * @version 2025-03-25
 * @discussion https://community.adobe.com/t5/indesign-discussions/is-it-possible-to-automatically-determine-my-current-target-selection-using-only-one-variable/m-p/15227968
 */
(function () {

    var targets = getStoriesOrDocument(app.activeDocument);

    if ('Document' === targets.constructor.name) {

        if (!confirm('Do you want to apply to whole document?'))
            return;

        // store document in an array for processing
        targets = [targets];

    }

    // now do something with the stories or document
    for (var i = 0; i < targets.length; i++) {
        var target = targets[i];
        $.writeln(target.constructor.name + ' ' + target.id);
    }

})();

/**
 * Returns the document's selected
 * stories or the document itself.
 * @author m1b
 * @version 2025-03-25
 * @param {Document} doc - an Indesign Document.
 * @returns {Array<Story>|Document}
 */
function getStoriesOrDocument(doc) {

    var stories = [],
        unique = {},
        items = doc.selection;

    if (0 === items.length)
        return doc;

    for (var i = 0; i < items.length; i++) {

        if (items[i].hasOwnProperty('parentStory')) {

            if (unique[items[i].parentStory.id])
                // already collected this story
                continue;

            unique[items[i].parentStory.id] = true;
            items[i] = items[i].parentStory;
        }

        if ('function' === typeof items[i].changeGrep)
            stories.push(items[i]);

    }

    return stories;

};

Edit 2025-03-25: added support for other selections, eg. an InsertionPoint.

2 replies

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
March 25, 2025

@dublove this is one way to do it:

/**
 * Example of starting a script by getting the
 * selected stories or the whole document, but first
 * confirming whether to process the whole document.
 * @author m1b
 * @version 2025-03-25
 * @discussion https://community.adobe.com/t5/indesign-discussions/is-it-possible-to-automatically-determine-my-current-target-selection-using-only-one-variable/m-p/15227968
 */
(function () {

    var targets = getStoriesOrDocument(app.activeDocument);

    if ('Document' === targets.constructor.name) {

        if (!confirm('Do you want to apply to whole document?'))
            return;

        // store document in an array for processing
        targets = [targets];

    }

    // now do something with the stories or document
    for (var i = 0; i < targets.length; i++) {
        var target = targets[i];
        $.writeln(target.constructor.name + ' ' + target.id);
    }

})();

/**
 * Returns the document's selected
 * stories or the document itself.
 * @author m1b
 * @version 2025-03-25
 * @param {Document} doc - an Indesign Document.
 * @returns {Array<Story>|Document}
 */
function getStoriesOrDocument(doc) {

    var stories = [],
        unique = {},
        items = doc.selection;

    if (0 === items.length)
        return doc;

    for (var i = 0; i < items.length; i++) {

        if (items[i].hasOwnProperty('parentStory')) {

            if (unique[items[i].parentStory.id])
                // already collected this story
                continue;

            unique[items[i].parentStory.id] = true;
            items[i] = items[i].parentStory;
        }

        if ('function' === typeof items[i].changeGrep)
            stories.push(items[i]);

    }

    return stories;

};

Edit 2025-03-25: added support for other selections, eg. an InsertionPoint.

dublove
dubloveAuthor
Legend
March 25, 2025

Hi m1b.

Thank you very much.

I didn't realize it was so complicated.

I'm a little busy, so I'll study a little.

 

m1b
Community Expert
Community Expert
March 25, 2025

The code that looks a little strange is the var unique = {}; line and the code that adds properties to it. I just add `true` to a property with the story's ID each time I collect one. We don't want to process the same story twice, which is what will happen without that checking.

- Mark

dublove
dubloveAuthor
Legend
March 25, 2025

That seems different.
That was one situation at a time, now it's all-inclusive at once.

Robert at ID-Tasker
Legend
March 25, 2025

But what was mentioned there can be used to do what you need.