Replacing Text; Inserting Special Characters - Adobe 0046100128056 - InDesign - Mac Manual

Javascript
Table of Contents

Advertisement

C
6: Text and Type
HAPTER
var myDocument = app.activeDocument;
//Set the measurement units to points.
myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
//Create a text frame on the current page.
var myTextFrame = app.activeWindow.activePage.textFrames.add();
//Set the bounds of the text frame.
myTextFrame.geometricBounds = [72, 72, 96, 288];
//Fill the text frame with placeholder text.
myTextFrame.contents = TextFrameContents.placeholderText;
//Now add text beyond the end of the text frame.
myTextFrame.insertionPoints.item(-1).contents = "\rThis is some overset text";
alert("The last paragraph in this alert should be \"This is some overset text\". Is
it?\r" + myTextFrame.contents);
alert("The last paragraph in this alert should be \"This is some overset text\". Is
it?\r" + myTextFrame.parentStory.contents);
For more on understanding the relationships between text objects in an InDesign document, see
"Understanding Text Objects" on page

Replacing text

The following script replaces a word with a phrase by changing the contents of the appropriate object (for
the complete script, see ReplaceWord):
var myDocument = app.activeDocument;
//Set the measurement units to points.
myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
//Create a text frame on the current page.
var myTextFrame = app.activeWindow.activePage.textFrames.add({geometricBounds:[72, 72,
288, 288], contents:"This is some example text."});
//Replace the third word "some" with the phrase
//"a little bit of".
myTextFrame.parentStory.words.item(2).contents = "a little bit of";
The following script replaces the text in a paragraph (for the complete script, see ReplaceText):
//Replace the text in the second paragraph without replacing
//the return character at the end of the paragraph. To do this,
//we'll use the ItemByRange method.
var myStartCharacter = myTextFrame.parentStory.paragraphs.item(1).characters.item(0);
var myEndCharacter = myTextFrame.parentStory.paragraphs.item(1).characters.item(-2);
myTextFrame.texts.itemByRange(myStartCharacter, myEndCharacter).contents = "This text
replaces the text in paragraph 2."
In the preceding script above, we excluded the return character because deleting the return might change
the paragraph style applied to the paragraph. To do this, we used
two characters—the starting and ending characters of the paragraph—as parameters.

Inserting special characters

Because the ExtendScript Toolkit supports Unicode, you can simply enter Unicode characters in text
strings that you send to InDesign. Alternately, you can use the JavaScript method of explicitly entering
Unicode characters by their glyph ID number:
The following script shows several ways to enter special characters. (We omitted the
81.
(where
\unnnn
nnnn
Entering and Importing Text 73
method, and we supplied
ItemByRange
is the Unicode code for the character).
myGetBounds

Hide quick links:

Advertisement

Table of Contents
loading

This manual is also suitable for:

Indesign cs5

Table of Contents