71
6
Text and Type
Entering, editing, and formatting text are the tasks that make up the bulk of the time spent working on
most InDesign documents. Because of this, automating text and type operations can result in large
productivity gains.
This chapter shows how to script the most common operations involving text and type. The sample scripts
in this chapter are presented in order of complexity, starting with very simple scripts and building toward
more complex operations.
We assume that you have already read Adobe InDesign CS5 Scripting Tutorial and know how to create,
install, and run a script. We also assume that you have some knowledge of working with text in InDesign
and understand basic typesetting terms.
Entering and Importing Text
This section covers the process of getting text into your InDesign documents. Just as you can type text into
text frames and place text files using the InDesign user interface, you can create text frames, insert text
into a story, or place text files on pages using scripting.
Creating a text frame
The following script creates a text frame, sets the bounds (size) of the frame, then enters text in the frame
(for the complete script, see the MakeTextFrame tutorial script):
var myDocument = app.documents.item(0);
var myPage = myDocument.pages.item(0);
var myTextFrame = myPage.textFrames.add();
//Set the bounds of the text frame.
myTextFrame.geometricBounds = [72, 72, 288, 288];
//Enter text in the text frame.
myTextFrame.contents = "This is some example text."
//Note that you could also use a properties record to
//create the frame and set its bounds and contents in one line:
//var myTextFrame = myDocument.pages.item(0).textFrames.add(
{geometricBounds:[72, 72, 288, 288], contents:"This is some example text."});
The following script shows how to create a text frame that is the size of the area defined by the page
margins.
myGetBounds is a useful function that you can add to your own scripts, and it appears in many
other examples in this chapter. (For the complete script, see MakeTextFrameWithinMargins.)
var myDocument = app.documents.item(0);
var myPage = myDocument.pages.item(0);
//Create a text frame on the current page.
var myTextFrame = myPage.textFrames.add();
//Set the bounds of the text frame.
myTextFrame.geometricBounds = myGetBounds(myDocument, myPage);
//Enter text in the text frame.
myTextFrame.contents = "This is some example text."
The following script fragment shows the myGetBounds function.