Site logo

Selection

Check if text is selected

// check if text is selected and get the current story
var msgWrongSelection = "One text frame or some text should be selected, or the cursor should be inserted into the text.";
if (app.selection.length == 0 || app.selection.length > 1) ErrorExit(msgWrongSelection);
var sel = app.selection[0];
	
if (sel.constructor.name == "TextFrame") { // a text frame is selected
	story = sel.parentStory;
}
else if (sel.hasOwnProperty("baseline")) { // some text is selected or the cursor is placed in the text
	story = sel.parentStory;
}
else {
	ErrorExit(msgWrongSelection, true);
}

if (story == undefined || !story.isValid) ErrorExit("Can't find the selected story.", true);

Click here to download the snippet.

Select pages and spreads in the pages panel

Usually, it's impossible to interact with a panel by script in InDesign, but you can select pages and spreads in the Pages panel. This can be useful, say, to invoke the "$ID/Override All Parent Page Items" menu action to override all master page items, instead of using the buggy override() method.

app.select(app.activeDocument.pages.itemByRange(1, 3)); // from 2nd to 4th pages
app.select(app.activeDocument.pages); // all pages
app.select(app.activeDocument.spreads[0]); // the first page