Based on my previous article "InDesign Scripting with ScriptUI: How to Move Cursor and Select Text", let's add a button to play around.
The original code looks like this:
// --- Last Part of UI (No Panel) ---------------------------------------------
function AddCloseButton(w) {
w.grpButtons = w.add('group');
w.btnClose = w.grpButtons.add('button {text: "Close"}');
w.btnClose.onClick = function () {
w.close();
}
}
// -------------------------------------------------------
Let me add a button to select a piece of text.
Below in in the listener function "TestIt", I don't add the boundary checking .
You may try and modify the variables "start" and "end" to go over the boundary. And see what happens.
// --- Last Part of UI (No Panel) ---------------------------------------------
function AddCloseButton(w) {
w.grpButtons = w.add('group');
w.grpButtons.orientation = "column";
w.btnTest = w.grpButtons.add('button {text: "Test"}');
w.btnClose = w.grpButtons.add('button {text: "Close"}');
w.btnClose.onClick = function () {
w.close();
}
w.btnTest.onClick = function () {
TestIt();
}
}
function TestIt() {
var myDocument;
try {
myDocument = app.activeDocument;
}
catch (xError) {
// output message to the debug console
$.writeln("No opened document.");
app.activate();
return;
}
if (myDocument.selection.length > 0) {
var theParent = myDocument.selection[0].parent;
var storyLength = myDocument.selection[0].parentStory.length;
try {
var start = storyLength;
var end = -storyLength;
// if (start => storyLength) ||
// (start < -storyLength) ||
// (end => storyLength) ||
// (end < -storyLength) ,
// an error will be catched.
var newRange = theParent.characters.itemByRange(start, end);
myDocument.select(newRange);
} catch (err) {
// output message to the debug console
$.writeln(err.message);
}
app.activate();
} else {
// output message to the debug console
$.writeln("No text has been clicked.");
}
}
// -------------------------------------------------------
The line
$.writeln("Some message here.");
will output message to the debug console.I use "Adobe Extendscript Toolkit CC" to debug my code.
The UI:
In the future, I will modify the button's click-listener to play with and test InDesign's script.
Have fun!
Comments