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

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

Guide ,
Mar 23, 2025 Mar 23, 2025

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.

one.pngexpand imagetow.pngexpand image

TOPICS
How to , Scripting
167
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 , Mar 24, 2025 Mar 24, 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 ('Docume
...
Translate
Community Expert ,
Mar 24, 2025 Mar 24, 2025
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 ,
Mar 24, 2025 Mar 24, 2025

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

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

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

 

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 ,
Mar 24, 2025 Mar 24, 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.

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 ,
Mar 25, 2025 Mar 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.

 

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

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

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