Copy link to clipboard
Copied
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.
1 Correct answer
@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
...
Copy link to clipboard
Copied
You've asked about that before:
Copy link to clipboard
Copied
That seems different.
That was one situation at a time, now it's all-inclusive at once.
Copy link to clipboard
Copied
But what was mentioned there can be used to do what you need.
Copy link to clipboard
Copied
@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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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

