Macromedia InDesign - CS4 User Guide

ADOBE® INDESIGN® CS4
ADOBE INDESIGN CS4
SCRIPTING GUIDE: JAVASCRIPT
© 2008 Adobe Systems Incorporated. All rights reserved.
Adobe® InDesign® CS4 Scripting Guide: JavaScript
The content of this guide is furnished for informational use only, is subject to change without notice, and should not be construed as a commitment by Adobe Systems Incorporated. Adobe Systems Incorporated assumes no responsibility or liability for any errors or inaccuracies that may appear in the informational content contained in this guide.
Please remember that existing artwork or images that you may want to include in your project may be protected under copyright law. The unauthorized incorporation of such material into your new work could be a violation of the rights of the copyright owner. Please be sure to obtain any permission required from the copyright owner.
Any references to company names in sample templates are for demonstration purposes only and are not intended to refer to any actual organization.
Adobe, the Adobe logo, Creative Suite, and InDesign are either registered trademarks or trademarks of Adobe Systems Incorporated in the United States and/or other countries. Microsoft and Windows are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Mac OS is a trademark of Apple Computer, Incorporated, registered in the United States and other countries. All other trademarks are the property of their respective owners.
Adobe Systems Incorporated, 345 Park Avenue, San Jose, California 95110, USA. Notice to U.S. Government End Users. The Software and Documentation are “Commercial Items,” as that term is defined at 48 C.F.R. §2.101, consisting of “Commercial Computer Software” and “Commercial Computer Software Documentation,” as such terms are used in 48 C.F.R. §12.212 or 48 C.F.R. §227.7202, as applicable. Consistent with 48 C.F.R. §12.212 or 48 C.F.R. §§227.7202-1 through
227.7202-4, as applicable, the Commercial Computer Software and Commercial Computer Software Documentation are being licensed to U.S. Government end users (a) only as Commercial Items and (b) with only those rights as are granted to all other end users pursuant to the terms and conditions herein. Unpublished-rights reserved under the copyright laws of the United States. Adobe Systems Incorporated, 345 Park Avenue, San Jose, CA 95110-2704, USA. For U.S. Government End Users, Adobe agrees to comply with all applicable equal opportunity laws including, if appropriate, the provisions of Executive Order 11246, as amended, Section 402 of the Vietnam Era Veterans Readjustment Assistance Act of 1974 (38 USC 4212), and Section 503 of the Rehabilitation Act of 1973, as amended, and the regulations at 41 CFR Parts 60-1 through 60-60, 60-250, and 60-741. The affirmative action clause and regulations contained in the preceding sentence shall be incorporated by reference.
Contents
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
How to Use the Scripts in This Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
About the structure of the scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
For More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2 Scripting Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Script preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Getting the current script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Script versioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Targeting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Interpretation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Using the doScript method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Sending parameters to doScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Returning values from doScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Controlling Undo with doScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Working with script labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Running scripts at start-up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Session and main script execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3 Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Basic document operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Creating a new document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Opening a document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Saving a document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Closing a document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Basic page layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Defining page size and document length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Defining bleed and slug areas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Setting page margins and columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Changing the appearance of the pasteboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Guides and grids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Changing measurement units and ruler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Defining and applying document presets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Setting up master spreads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Adding XMP metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Creating a document template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Printing a document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Printing using page ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Setting print preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Printing with printer presets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3
Contents 4
Exporting a document as PDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Exporting to PDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Setting PDF export options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Exporting a range of pages to PDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Exporting individual pages to PDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Exporting pages as EPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Exporting all pages to EPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Exporting a range of pages to EPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Exporting as EPS with file naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4 Working with Page Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Creating Page Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Page Item Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Grouping Page Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Duplicating and Moving Page Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Creating Compound Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Using Pathfinder Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Converting Page Item Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Arranging Page Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Transforming Page Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Using the transform method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Working with transformation matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Coordinate spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Transformation origin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Resolving locations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Transforming points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Transforming again . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Resize and Reframe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
5 Text and Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Entering and importing text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Creating a text frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Adding text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Stories and text frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Replacing text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Inserting special characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Placing text and setting text-import preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Exporting text and setting text-export preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Understanding text objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Working with text selections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Moving and copying text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Text objects and iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Working with text frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Linking text frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Unlinking text frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Removing a frame from a story . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Splitting all frames in a story . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Creating an anchored frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Contents 5
Formatting text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Setting text defaults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Working with fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Applying a font . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Changing text properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Changing text color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Creating and applying styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Deleting a style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Importing paragraph and character styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Finding and changing text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
About find/change preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Finding and changing text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Finding and changing text formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Using grep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Using glyph search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Working with tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Path text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Autocorrect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Footnotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Setting text preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
6 User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Dialog overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Your first InDesign dialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Adding a user interface to “Hello World” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Creating a more complex user interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Working with ScriptUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Creating a progress bar with ScriptUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
7 Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Understanding the event-scripting model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
About event properties and event propagation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Working with eventListeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
An example “afterNew” eventListener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Sample “beforePrint” eventListener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
8 Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Understanding the menu model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Localization and menu names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Running a menu action from a script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Adding menus and menu items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Menus and events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Working with scriptMenuActions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
A more complex menu-scripting example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Contents 6
9 XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
The best approach to scripting XML in InDesign? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Scripting XML elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Setting XML preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Setting XML import preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Importing XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Creating an XML tag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Loading XML tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Saving XML tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Creating an XML element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Moving an XML element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Deleting an XML element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Duplicating an XML element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Removing items from the XML structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Creating an XML comment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Creating an XML processing instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Working with XML attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Working with XML stories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Exporting XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Adding XML elements to a layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Associating XML elements with page items and text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Marking up existing layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Applying styles to XML elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Working with XML tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
10 XML Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Why use XML rules? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
XML-rules programming model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
XML rules examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Setting up a sample document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Getting started with XML rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Changing the XML structure using XML rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Duplicating XML elements with XML rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
XML rules and XML attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Applying multiple matching rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Finding XML elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Extracting XML elements with XML rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Applying formatting with XML rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Creating page items with XML rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Creating tables using XML rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Scripting the XML-rules processor object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
1

Introduction

This document shows how to do the following:
Work with the Adobe® InDesign® scripting environment.
Use advanced scripting features.
Perform basic document tasks like setting up master spreads, printing, and exporting.
Work with page items (rectangles, ellipses, graphic lines, polygons, text frames, and groups).
Work with text and type in an InDesign document, including finding and changing text.
Create dialog boxes and other user-interface items.
Customize and add menus and create menu actions.
Respond to user-interface events.
Work with XML, from creating XML elements and importing XML to adding XML elements to a layout.
Apply XML rules, a new scripting feature that makes working with XML in InDesign faster and easier.
We assume that you have already read the Adobe InDesign CS4 Scripting Tutorial and know how to create, install, and run scripts. If you need to know how to connect with your scripting environment or view the InDesign scripting object model from your script editor, that information can be found in the Adobe InDesign CS4 Scripting Tutorial.

How to Use the Scripts in This Document

For the most part, the scripts shown in this document are not complete scripts. They are only fragments of scripts, and are intended to show only the specific part of a script relevant to the point being discussed in the text. You can copy the script lines shown in this document and paste them into your script editor, but you should not expect them to run without further editing. Note, in addition, that scripts copied out of this document may contain line breaks and other characters (due to the document layout) that will prevent them from executing properly.
A zip archive of all of the scripts shown in this document is available at the InDesign scripting home page, at: http://www.adobe.com/products/indesign/scripting/index.html expanded the archive, move the folders corresponding to the scripting language(s) of your choice into the Scripts Panel folder inside the Scripts folder in your InDesign folder. At that point, you can run the scripts from the Scripts panel inside InDesign.

About the structure of the scripts

. After you have downloaded and
The script examples are all written using a common template that includes the functions “main,” “mySetup,” “mySnippet,” and “myTeardown.” We did this to simplify automated testing and publication— there is no reason for you to construct your scripts this way. Most of the time, the part of the script you will be interested in will be inside the “mySnippet” function.
7
CHAPTER 1: Introduction For More Information 8

For More Information

For more information on InDesign scripting, you also can visit the InDesign Scripting User to User forum, at
http://www.adobeforums.com
newest scripts. The forum contains hundreds of sample scripts.
. In the forum, scripters can ask questions, post answers, and share their
2

Scripting Features

This chapter covers scripting techniques related to InDesign’s scripting environment. Almost every other object in the InDesign scripting model controls a feature that can change a document or the application defaults. By contrast, the features in this chapter control how scripts operate.
This document discusses the following:
The scriptPreferences object and its properties.
Getting a reference to the executing script.
Running scripts in prior versions of the scripting object model.
Using the doScript method to run scripts.
Working with script labels.
Running scripts at InDesign start-up.
Controlling the ExtendScript engine in which scripts execute.
We assume you already read Adobe InDesign CS4 Scripting Tutorial and know how to write, install, and run InDesign scripts in the scripting language of your choice.

Script preferences

The scriptPreferences object provides objects and properties related to the way InDesign runs scripts. The following table provides more detail on each property of the scriptPreferences object:
Property Description
enableRedraw
scriptsFolder
scriptsList
Turns screen redraw on or off while a script is running from the Scripts panel.
The path to the scripts folder.
A list of the available scripts. This property is an array of arrays, in the following form:
[[fileName, filePath], ...]
Where fileName is the name of the script file and filePath is the full path to the script. You can use this feature to check for the existence of a script in the installed set of scripts.
9
CHAPTER 2: Scripting Features Getting the current script 10
Property Description
userInteractionLevel
This property controls the alerts and dialogs InDesign presents to the user. When you set this property to InDesign does not display any alerts or dialogs. Set it to
UserInteractionLevels.interactWithAlerts to enable alerts but
disable dialogs. Set it to alerts and dialogs. The ability to turn off alert displays is very useful when you are opening documents via script; often, InDesign displays an alert for missing fonts or linked graphics files. To avoid this alert, set the user-interaction level to opening the document, then restore user interaction (set the property to
interactWithAll) before completing script execution.
version
The version of the scripting environment in use. For more information, see
Script versioning” on page 11. Note this property is not the same as the
version of the application.

Getting the current script

You can get a reference to the current script using the activeScript property of the application object. You can use this property to help you locate files and folders relative to the script, as shown in the following example (from the ActiveScript tutorial script):
var myScript = app.activeScript; alert("The current script is: " + myScript); var myParentFolder = File(myScript).parent; alert("The folder containing the active script is: " + myParentFolder);
UserInteractionLevels.neverInteract,
interactWithAll to restore the normal display of
UserInteractionLevels.neverInteract before
When you debug scripts using a script editor, the activeScript property returns an error. Only scripts run from the Scripts palette appear in the
When you debug scripts from the ExtendScript Toolkit, using the To avoid this error and create a way of debugging scripts that use the
activeScript property.
activeScript property returns an error.
activeScript property, use the
following error handler (from the GetScriptPath tutorial script):
function myGetScriptPath() {
try{
return app.activeScript; } catch(myError){
return File(myError.fileName); }
}
CHAPTER 2: Scripting Features Script versioning 11

Script versioning

InDesign CS4 can run scripts using earlier versions of the InDesign scripting object model. To run an older script in a newer version of InDesign, you must consider the following:
Targeting — Scripts must be targeted to the version of the application in which they are being run
(i.e., the current version). The mechanics of targeting are language specific.
Compilation — This involves mapping the names in the script to the underlying script ids, which are
what the application understands. The mechanics of compilation are language specific.
Interpretation — This involves matching the ids to the appropriate request handler within the
application. InDesign CS4 correctly interprets a script written for an earlier version of the scripting object model. To do this, run the script from a folder in the Scripts panel folder named
Scripts (for InDesign CS3 scripts) or Version 2.0 Scripts (for InDesign CS2 scripts), or explicitly set
the application's script preferences to the old object model within the script (as shown below). Put the previous version scripts in the folder, and run them from the Scripts panel.

Targ et in g

Targeting for JavaScripts is implicit when the script is launched from the Scripts panel. If the script is launched externally (from the ESTK), use the
target directive:
Version 5.0
//target CS4 #target "InDesign-6.0" //target the latest version of InDesign #target "InDesign"

Compilation

JavaScripts are not pre-compiled. For compilation, the application uses the same version of the DOM that is set for interpretation.

Interpretation

The InDesign application object contains a scriptPreferences object, which allows a script to get/set the version of the scripting object model to use for interpreting scripts. The version defaults to the current version of the application and persists.
The following examples show how to set the version to the CS3 (5.0) version of the scripting object model.
//Set to 5.0 scripting object model app.scriptPreferences.version = 5.0;

Using the doScript method

The doScript method gives a script a way to execute another script. The script can be a string of valid scripting code or a file on disk. The script can be in the same scripting language as the current script or another scripting language. The available languages vary by platform: on Mac OS AppleScript or JavaScript; on Windows
®
, VBScript or JavaScript.
®
, you can run
CHAPTER 2: Scripting Features Using the doScript method 12
The doScript method has many possible uses:
Running a script in another language that provides a feature missing in your main scripting language.
For example, VBScript lacks the ability to display a file or folder browser, which JavaScript has. AppleScript can be very slow to compute trigonometric functions (sine and cosine), but JavaScript
®
performs these calculations rapidly. JavaScript does not have a way to query Microsoft
Excel for the contents of a specific spreadsheet cell, but both AppleScript and VBScript have this capability. In all these examples, the
doScript method can execute a snippet of scripting code in another language,
to overcome a limitation of the language used for the body of the script.
Creating a script “on the fly.” Your script can create a script (as a string) during its execution, which it
can then execute using the
doScript method. This is a great way to create a custom dialog or panel
based on the contents of the selection or the attributes of objects the script creates.
Embedding scripts in objects. Scripts can use the doScript method to run scripts that were saved as
strings in the
label property of objects. Using this technique, an object can contain a script that
controls its layout properties or updates its content according to certain parameters. Scripts also can be embedded in XML elements as an attribute of the element or as the contents of an element. See
Running scripts at start-up” on page 15.

Sending parameters to doScript

To send a parameter to a script executed by doScript, use the following form (from the DoScriptParameters tutorial script):
var myParameters = ["Hello from DoScript", "Your message here."]; var myJavaScript = "alert(\"First argument: \" + arguments[0] + \"\\rSecond argument: \" + arguments[1]);"; app.doScript(myJavaScript, ScriptLanguage.javascript, myParameters); if(File.fs == "Windows"){
var myVBScript = "msgbox arguments(1), vbOKOnly, \"First argument: \" & arguments(0)"; app.doScript(myVBScript, ScriptLanguage.visualBasic, myParameters);
} else{
var myAppleScript = "tell application \"Adobe InDesign CS4\\rdisplay dialog(\"First argument\" & item 1 of arguments & return & \"Second argument: \" & item 2 of arguments & return & end tell"; app.doScript(myAppleScript, ScriptLanguage.applescriptLanguage, myParameters);
}

Returning values from doScript

The following script fragment shows how to return a value from a script executed by doScript. This example uses a JavaScript that is executed as a string, but the same method works for script files. This example returns a single value, but you can return multiple values by returning an array (for the complete script, refer to the DoScriptReturnValues script).
CHAPTER 2: Scripting Features Using the doScript method 13
//To send parameters to a script run using app.doScript(), the doScript //statement must not appear inside a function. If it does, the parameters //will not be passed to the script. var myDocument = app.documents.add(); var myPage = myDocument.pages.item(0); var myTextFrame = myPage.textFrames.add(); myTextFrame.geometricBounds = ["72pt", "72pt", "288pt", "288pt"]; myTextFramecContents = "Example text frame."; var myDestinationPage = myDocument.pages.add(LocationOptions.after, myPage); var myPageIndex = myDestinationPage.name; var myID = myTextFrame.id; var myJavaScript = "var myDestinationPage = arguments[1];\r" ; myJavaScript += "myID = arguments[0];\r"; myJavaScript += "var myX = arguments[2];\r"; myJavaScript += "var myY = arguments[3]\r;" myJavaScript += "var myPageItem = app.documents.item(0).pages.item(0).pageItems.itemByID(myID);\r"; myJavaScript += "myPageItem.duplicate(app.documents.item(0).pages.item(myDestinationPage));\r" //Create an array for the parameters we want to pass to the JavaScript. var myArguments = [myID, myPageIndex, 0, 0]; var myDuplicate = app.doScript(myJavaScript, ScriptLanguage.javascript, myArguments); //myDuplicate now contains a reference to the duplicated text frame. //Change the text in the duplicated text frame. myDuplicate.contents = "Duplicated text frame.";
Another way to get values from another script is to use the scriptArgs (short for “script arguments”) object of the application. The following script fragment shows how to do this (for the complete script, see DoScriptScriptArgs):
var myJavaScript = "app.scriptArgs.setValue(\"ScriptArgumentA\", \"This is the first script argument value.\");\r"; myJavaScript += "app.scriptArgs.setValue(\"ScriptArgumentB\", \"This is the second script argument value.\")"; var myScriptArgumentA = app.scriptArgs.getValue("ScriptArgumentA"); var myScriptArgumentB = app.scriptArgs.getValue("ScriptArgumentB"); alert("ScriptArgumentA: " + myScriptArgumentA + "\rScriptArgumentB: " + myScriptArgumentB); if(File.fs == "Windows"){
var myVBScript = "Set myInDesign = CreateObject(\"InDesign.Application.CS4\")\r"; myVBScript += "myInDesign.ScriptArgs.SetValue \"ScriptArgumentA\", \"This is the
first script argument value.\"\r";
myVBScript += "myInDesign.ScriptArgs.SetValue \"ScriptArgumentB\", \"This is the
second script argument value.\"";
app.doScript(myVBScript, ScriptLanguage.visualBasic);
} else{
var myAppleScript = "tell application \"Adobe InDesign CS4\"\r"; myAppleScript += "make script arg with properties{name:\"ScriptArgumentA\",
value:\"This is the first script argument value.\"}\r";
myAppleScript += "make script arg with properties{name:\"ScriptArgumentB\",
value:\"This is the second script argument value.\"}\r";
myAppleScript += "end tell\r"; app.doScript(myAppleScript, ScriptLanguage.applescriptLanguage);
} var myScriptArgumentA = app.scriptArgs.getValue("ScriptArgumentA"); var myScriptArgumentB = app.scriptArgs.getValue("ScriptArgumentB"); alert("ScriptArgumentA: " + myScriptArgumentA + "\rScriptArgumentB: " + myScriptArgumentB);
CHAPTER 2: Scripting Features Controlling Undo with doScript 14

Controlling Undo with doScript

InDesign gives you the ability to undo almost every action, but this comes at a price: for almost every action you make, InDesign writes to disk. For normal work you using the tools presented by the user interface, this does not present any problem. For scripts, which can perform thousands of actions in the time a human being can blink, the constant disk access can be a serious drag on performance.
doScript method offers a way around this performance bottleneck by providing two parameters that
The control the way that scripts are executed relative to InDesign’s Undo behavior. These parameters are shown in the following examples:
//Given a script "myJavaScript" and an array of parameters "myParameters"... app.doScript(myJavaScript, ScriptLanguage.javascript, myParameters, UndoModes.fastEntireScript, "Script Action"); //UndoModes can be: //UndoModes.autoUnto: Add no events to the Undo queue. //UndoModes.entireScript: Put a single event in the Undo queue. //UndoModes.fastEntireScript: Put a single event in the Undo queue. //UndoModes.scriptRequest: Undo each script action as a separate event. //The last parameter is the text that appears in the Undo menu item.

Working with script labels

Many objects in InDesign scripting have a label property, including page items (rectangles, ovals, groups, polygons, text frames, and graphic lines), table cells, documents, stories, and pages. This property can store a very large amount of text.
The label of page items can be viewed, entered, or edited using the Script Label panel (choose Window > Automation > Script Label to display this panel), shown below. You also can add a label to an object using scripting, and you can read the script label via scripting. For many objects, like stories, pages, and paragraph styles, you cannot set or view the label using the Script Label panel.
label property can contain any form of text data, such as tab- or comma-delimited text, HTML, or
The XML. Because scripts also are text, they can be stored in the
Page items can be referred to by their layers) can be referred to by their label property (for the complete script, see ScriptLabel):
name. The following script fragment demonstrates this special case of the
label property.
label, just like named items (such as paragraph styles, colors, or
CHAPTER 2: Scripting Features Running scripts at start-up 15
var myDocument = app.documents.add(); var myPage = myDocument.pages.item(0); var myPageWidth = myDocument.documentPreferences.pageWidth; var myPageHeight = myDocument.documentPreferences.pageHeight; //Create 10 random page items. for(var myCounter = 0; myCounter < 10; myCounter++){
myX1 = myGetRandom(0, myPageWidth, false); myY1 = myGetRandom(0, myPageHeight, false); myX2 = myGetRandom(0, myPageWidth, false); myY2 = myGetRandom(0, myPageHeight, false); myRectangle = myPage.rectangles.add({geometricBounds:[myY1, myX1, myY2, myX2]}); if(myGetRandom(0, 1, true)){
myRectangle.label = "myScriptLabel";
}
} var myPageItems = myPage.pageItems.item("myScriptLabel"); if(myPageItems.getElements().length != 0){
alert("Found " + myPageItems.getElements().length + " page items with the label.");
} //This function gets a random number in the range myStart to myEnd. function myGetRandom(myStart, myEnd, myInteger){
var myRandom; var myRange = myEnd - myStart; if(myInteger == true){
myRandom = myStart = Math.round(Math.random()); } else{
myRandom = myStart + Math.floor(Math.random()*myRange); } return myRandom;
}
In addition, all objects that support the label property also support custom labels. A script can set a custom label using the
insertLabel method, and extract the custom label using the extractLabel
method, as shown in the following script fragment (from the CustomLabel tutorial script):
var myDocument = app.documents.add(); myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points; myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points; var myPage = myDocument.pages.item(0); var myRectangle = myPage.rectangles.add({geometricBounds:[72, 72, 144, 144]}); //Insert a custom label using insertLabel. The first parameter is the //name of the label, the second is the text to add to the label. myRectangle.insertLabel("CustomLabel", "This is some text stored in a custom label."); //Extract the text from the label and display it in an alert. var myString = myRectangle.extractLabel("CustomLabel"); alert("Custom label contained: " + myString);

Running scripts at start-up

To run a script when InDesign starts, put the script in the Startup Scripts folder in the Scripts folder (for more information, see “Installing Scripts” in Adobe InDesign CS4 Scripting Tutorial).
OTE: Scripts run in the session ExtendScript engine when InDesign starts can create objects and
N
functions that will be available to other scripts for the duration of the session. For more information, see
Session and main script execution” on page 16.
CHAPTER 2: Scripting Features Session and main script execution 16

Session and main script execution

InDesign has two ways to run a JavaScript, session and main. These names correspond to the ExtendScript “engine” used to run the script.
By default, when you run an InDesign JavaScript, the script is interpreted and executed by the “main” ExtendScript engine, which is destroyed when the script completes execution. Script objects created by the script do not persist.
Scripts run in the session engine can create objects that persist until you close InDesign. You can refer to these objects from other scripts run in the InDesign JavaScript, add the following line to the start of your script.
#targetengine "session"
You can create your own persistent ExtendScript interpretation and execution environment. To do this, use
#targetenging statement and provide your own ExtendScript engine name, as shown in the
the following script fragment:
#targetengine "adobe"
session engine. To set the session engine as the target of an
3

Documents

The work you do in InDesign revolves around documents—creating them, saving them, printing or exporting them, and populating them with page items, colors, styles, and text. Almost every document-related task can be automated using InDesign scripting.
This chapter shows you how to do the following
Perform basic document-management tasks, including:
Creating a new document.
Opening a document.
Saving a document.
Closing a document.
Perform basic page-layout operations, including:
Setting the page size and document length.
Defining bleed and slug areas.
Specifying page columns and margins.
Change the appearance of the pasteboard.
Use guides and grids.
Change measurement units and ruler origin.
Define and apply document presets.
Set up master pages (master spreads)
Set text-formatting defaults.
Add XMP metadata (information about a file).
Create a document template.
Print a document.
Export a document as Adobe PDF.
Export pages of a document as EPS.
We assume you already read Adobe InDesign CS4 Scripting Tutorial and know how to create, install, and run a script.
17
CHAPTER 3: Documents Basic document operations 18

Basic document operations

Opening, closing, and saving documents are some of the most basic document tasks. This section shows how to do them using scripting.

Creating a new document

The following script shows how to make a new document using scripting (for the complete script, see MakeDocument):
var myDocument = app.documents.add();
To create a document using a document preset, the add method includes an optional parameter you can use to specify a document preset, as shown in the following script (for the complete script, see MakeDocumentWithPreset):
//Creates a new document using the specified document preset. //Replace "myDocumentPreset" in the following line with the name //of the document preset you want to use. var myDocument = app.documents.add(true, app.documentPresets.item("myDocumentPreset"));
You can create a document without displaying it in a window, as shown in the following script fragment (from the MakeDocumentWithParameters tutorial script):
//Creates a new document without showing the document window. //The first parameter (showingWindow) controls the visibility of the //document. Hidden documents are not minimized, and will not appear until //you add a new window to the document. var myDocument = app.documents.add(false); //To show the window: var myWindow = myDocument.windows.add();
Some script operations are much faster when the document window is hidden.

Opening a document

The following script shows how to open an existing document (for the complete script, see OpenDocument):
app.open(File("/c/myTestDocument.indd"));
You can choose to prevent the document from displaying (i.e., hide it) by setting the showing window parameter of the performance of a script. To show a hidden document, create a new window, as shown in the following script fragment (from the OpenDocumentInBackground tutorial script):
//Opens an existing document in the background, then shows the document. //You'll have to fill in your own file path. var myDocument = app.open(File("/c/myTestDocument.indd"), false); //At this point, you could do things with the document without showing the //document window. In some cases, scripts will run faster when the document //window is not visible. //When you want to show the hidden document, create a new window. var myLayoutWindow = myDocument.windows.add();
open method to false (the default is true). You might want to do this to improve
CHAPTER 3: Documents Basic document operations 19

Saving a document

In the InDesign user interface, you save a file by choosing File > Save, and you save a file to another file name by choosing File > Save As. In InDesign scripting, the shown in the following script fragment (from the SaveDocument tutorial script):
//If the active document has been changed since it was last saved, save it. if(app.activeDocument.modified == true){
app.activeDocument.save();
}
The save method has two optional parameters: The first (to) specifies the file to save to; the second
stationery) can be set to true to save the document as a template, as shown in the following script
( fragment (from the SaveDocumentAs tutorial script):
//If the active document has not been saved (ever), save it. if(app.activeDocument.saved == false){
//If you do not provide a file name, InDesign displays the Save dialog box. app.activeDocument.save(new File("/c/myTestDocument.indd"));
}
You can save a document as a template, as shown in the following script fragment (from the SaveAsTemplate tutorial script):
save method can do either operation, as
//Save the active document as a template. var myFileName; if(app.activeDocument.saved == true){
//Convert the file name to a string. myFileName = app.activeDocument.fullName + ""; //If the file name contains the extension ".indd", change it to ".indt". if(myFileName.indexOf(".indd")!=-1){
var myRegularExpression = /.indd/gi
myFileName = myFileName.replace(myRegularExpression, ".indt"); }
} //If the document has not been saved, then give it a default file name/file path. else{
myFileName = "/c/myTestDocument.indt";
}
app.activeDocument.save(File(myFileName), true);

Closing a document

The close method closes a document, as shown in the following script fragment (from the CloseDocument tutorial script):
app.activeDocument.close(); //Note that you could also use: //app.documents.item(0).close();
The close method can take up to two optional parameters, as shown in the following script fragment (from the CloseWithParameters tutorial script):
CHAPTER 3: Documents Basic page layout 20
//Use SaveOptions.yes to save the document,SaveOptions.no to close the //document without saving, or SaveOptions.ask to display a prompt. If //you use SaveOptions.yes, you'll need to provide a reference to a file //to save to in the second parameter (SavingIn). //Note that the file path is provided using the JavaScript URI form //rather than the platform-specific form. // //If the file has not been saved, display a prompt. if(app.activeDocument.saved != true){
app.activeDocument.close(SaveOptions.ask); //Or, to save to a specific file name: //var myFile = File("/c/myTestDocument.indd"); //app.activeDocument.close(SaveOptions.yes, myFile);
} else{
//If the file has already been saved, save it. app.activeDocument.close(SaveOptions.yes);
}
You can close all open documents without saving them, as shown in the following script fragment (from the CloseAll tutorial script):
for(myCounter = app.documents.length; myCounter > 0; myCounter--){
app.documents.item(myCounter-1).close(SaveOptions.no);
}

Basic page layout

Each document has a page size, assigned number of pages, bleed and slug working areas, and columns and margins to define the area into which material is placed. Again, all these parameters are accessible to scripting, as shown in the examples in this section.

Defining page size and document length

When you create a new document using the InDesign user interface, you can specify the page size, number of pages, page orientation, and whether the document uses facing pages. To create a document using InDesign scripting, use the creating a document, you can use the the following script fragment (from the DocumentPreferences tutorial script):
var myDocument = app.documents.add(); with(myDocument.documentPreferences){
pageHeight = "800pt"; pageWidth = "600pt"; pageOrientation = PageOrientation.landscape; pagesPerDocument = 16;
}
NOTE: The app object also has a documentPreferences object. You can set the application defaults for page height, page width, and other properties by changing the properties of this object.
documents.add method, which does not specify these settings. After
documentPreferences object to control the settings, as shown in

Defining bleed and slug areas

Within InDesign, a bleed or a slug is an area outside the page margins that can be printed or included in an exported PDF. Typically, these areas are used for objects that extend beyond the page edges (bleed) and
CHAPTER 3: Documents Basic page layout 21
job/document information (slug). The two areas can be printed and exported independently; for example, you might want to omit slug information for the final printing of a document. The following script shows how to set up the bleed and slug for a new document (for the complete script, see BleedAndSlug):
myDocument = app.documents.add(); //The bleed and slug properties belong to the documentPreferences object. with(myDocument.documentPreferences){
//Bleed documentBleedBottomOffset = "3p"; documentBleedTopOffset = "3p"; documentBleedInsideOrLeftOffset = "3p"; documentBleedOutsideOrRightOffset = "3p"; //Slug slugBottomOffset = "18p"; slugTopOffset = "3p"; slugInsideOrLeftOffset = "3p"; slugRightOrOutsideOffset = "3p";
}
Alternately, if all the bleed distances are equal, as in the preceding example, you can use the
documentBleedUniformSize property, as shown in the following script fragment (from the
UniformBleed tutorial script):
//Create a new document. myDocument = app.documents.add(); //The bleed properties belong to the documentPreferences object. with(myDocument.documentPreferences){
//Bleed documentBleedUniformSize = true; documentBleedTopOffset = "3p";
}
If all the slug distances are equal, you can use the documentSlugUniformSize property, as shown in the following script fragment (from the UniformSlug tutorial script):
//Create a new document. myDocument = app.documents.add(); //The slug properties belong to the documentPreferences object. with(myDocument.documentPreferences){
//Slug: documentSlugUniformSize = true; slugTopOffset = "3p";
}
In addition to setting the bleed and slug widths and heights, you can control the color used to draw the guides defining the bleed and slug. This property is not in the
pasteboardPreferences object, as shown in the following script fragment (from the
in the
documentPreferences object; instead, it is
BleedSlugGuideColors tutorial script):
with(app.activeDocument.pasteboardPreferences){
//Any of InDesign's guides can use the UIColors constants... bleedGuideColor = UIColors.cuteTeal; slugGuideColor = UIColors.charcoal; //...or you can specify an array of RGB values (with values from 0 to 255) //bleedGuideColor = [0, 198, 192]; //slugGuideColor = [192, 192, 192];
}
CHAPTER 3: Documents Basic page layout 22

Setting page margins and columns

Each page in a document can have its own margin and column settings. With InDesign scripting, these properties are part of the new document, then sets the margins and columns for all pages in the master spread. (For the complete script, see PageMargins.)
myDocument = app.documents.add(); with (myDocument.pages.item(0).marginPreferences){
columnCount = 3; //columnGutter can be a number or a measurement string. columnGutter = "1p"; bottom = "6p" //When document.documentPreferences.facingPages == true, //"left" means inside; "right" means outside. left = "6p" right = "4p" top = "4p"
}
To set the page margins for an individual page, use the margin preferences for that page, as shown in the following script fragment (from the PageMarginsForOnePage tutorial script):
marginPreferences object for each page. This following sample script creates a
myDocument = app.documents.add(); with (myDocument.pages.item(0).marginPreferences){
columnCount = 3; //columnGutter can be a number or a measurement string. columnGutter = "1p"; bottom = "6p" //When document.documentPreferences.facingPages == true, //"left" means inside; "right" means outside. left = "6p" right = "4p" top = "4p"
}
InDesign does not allow you to create a page that is smaller than the sum of the relevant margins; that is, the width of the page must be greater than the sum of the left and right page margins, and the height of the page must be greater than the sum of the top and bottom margins. If you are creating very small pages (for example, for individual newspaper advertisements) using the InDesign user interface, you can easily set the correct margin sizes as you create the document, by entering new values in the document default page Margin fields in the New Document dialog box.
From scripting, however, the solution is not as clear: when you create a document, it uses the application’s default-margin preferences. These margins are applied to all pages of the document, including master pages. Setting the document margin preferences affects only new pages and has no effect on existing pages. If you try to set the page height and page width to values smaller than the sum of the corresponding margins on any existing pages, InDesign does not change the page size.
There are two solutions. The first is to set the margins of the existing pages before you try to change the page size, as shown in the following script fragment (from the PageMarginsForSmallPages tutorial script):
CHAPTER 3: Documents Basic page layout 23
var myDocument = app.documents.add(); myDocument.marginPreferences.top = 0; myDocument.marginPreferences.left = 0; myDocument.marginPreferences.bottom = 0; myDocument.marginPreferences.right = 0; //The following assumes that your default document contains a single page. myDocument.pages.item(0).marginPreferences.top = 0; myDocument.pages.item(0).marginPreferences.left = 0; myDocument.pages.item(0).marginPreferences.bottom = 0; myDocument.pages.item(0).marginPreferences.right = 0; //The following assumes that your default master spread contains two pages. myDocument.masterSpreads.item(0).pages.item(0).marginPreferences.top = 0; myDocument.masterSpreads.item(0).pages.item(0).marginPreferences.left = 0; myDocument.masterSpreads.item(0).pages.item(0).marginPreferences.bottom = 0; myDocument.masterSpreads.item(0).pages.item(0).marginPreferences.right = 0; myDocument.masterSpreads.item(0).pages.item(1).marginPreferences.top = 0; myDocument.masterSpreads.item(0).pages.item(1).marginPreferences.left = 0; myDocument.masterSpreads.item(0).pages.item(1).marginPreferences.bottom = 0; myDocument.masterSpreads.item(0).pages.item(1).marginPreferences.right = 0; myDocument.documentPreferences.pageHeight = "1p"; myDocument.documentPreferences.pageWidth = "6p";
Alternately, you can change the application’s default-margin preferences before you create the document, as shown in the following script fragment (from the ApplicationPageMargins tutorial script):
with (app.marginPreferences){
//Save the current application default margin preferences. var myY1 = top; var myX1 = left; var myY2 = bottom; var myX2 = right; //Set the application default margin preferences. top = 0; left = 0; bottom = 0; right = 0;
} //Create a new example document to demonstrate the change. var myDocument = app.documents.add(); myDocument.documentPreferences.pageHeight = "1p"; myDocument.documentPreferences.pageWidth = "6p"; //Reset the application default margin preferences to their former state. with (app.marginPreferences){
top = myY1; left = myX1 ; bottom = myY2; right = myX2;
}

Changing the appearance of the pasteboard

The pasteboard is the area that surrounds InDesign pages and spreads. You can use it for temporary storage of page items or for job-tracking information. You can change the size of the pasteboard and its color using scripting. The
previewBackgroundColor property sets the color of the pasteboard in Preview
mode, as shown in the following script fragment (from the PasteboardPreferences tutorial script):
CHAPTER 3: Documents Basic page layout 24
myDocument = app.documents.add(); with(myDocument.pasteboardPreferences){
//You can use either a number or a measurement string //to set the space above/below. minimumSpaceAboveAndBelow = "12p"; //You can set the preview background color to any of //the predefined UIColor enumerations... previewBackgroundColor = UIColors.gray; //...or you can specify an array of RGB values //(with values from 0 to 255) //previewBackgroundColor = [192, 192, 192];
}

Guides and grids

Guides and grids make it easy to position objects on your document pages. These are very useful items to add when you are creating templates for others to use.
Defining guides
Guides in InDesign give you an easy way to position objects on the pages of your document. The following script fragment shows how to use guides (for the complete script, see Guides):
var myDocument = app.documents.add(); var myPageWidth = myDocument.documentPreferences.pageWidth; var myPageHeight = myDocument.documentPreferences.pageHeight; with(myDocument.pages.item(0)){
//Place guides at the margins of the page. guides.add(undefined, {orientation:HorizontalOrVertical.vertical, <lb> location:marginPreferences.left}); guides.add(undefined, {orientation:HorizontalOrVertical.vertical, <lb> location:(myPageWidth - marginPreferences.right)}); guides.add(undefined, {orientation:HorizontalOrVertical.horizontal, <lb> location:marginPreferences.top}); guides.add(undefined, {orientation:HorizontalOrVertical.horizontal, <lb> location:(myPageHeight - marginPreferences.bottom)}); //Place a guide at the vertical center of the page. guides.add(undefined, {orientation:HorizontalOrVertical.vertical, <lb> location:(myPageWidth/2)}); //Place a guide at the horizontal center of the page. guides.add(undefined, {orientation:HorizontalOrVertical.horizontal, <lb> location:(myPageHeight/2)});
}
Horizontal guides can be limited to a given page or extend across all pages in a spread. From InDesign scripting, you can control this using the
fitToPage property. This property is ignored by vertical guides.
You can use scripting to change the layer, color, and visibility of guides, just as you can from the user interface, as shown in the following script fragment (from the GuideOptions tutorial script):
CHAPTER 3: Documents Basic page layout 25
var myDocument = app.documents.add(); var myPageWidth = myDocument.documentPreferences.pageWidth; var myPageHeight = myDocument.documentPreferences.pageHeight; with(myDocument.pages.item(0)){
//Place guides at the margins of the page. guides.add(undefined, {orientation:HorizontalOrVertical.vertical, <lb> location:marginPreferences.left}); guides.add(undefined, {orientation:HorizontalOrVertical.vertical, <lb> location:(myPageWidth - marginPreferences.right)}); guides.add(undefined, {orientation:HorizontalOrVertical.horizontal, <lb> location:marginPreferences.top}); guides.add(undefined, {orientation:HorizontalOrVertical.horizontal, <lb> location:(myPageHeight - marginPreferences.bottom)}); //Place a guide at the vertical center of the page. guides.add(undefined, {orientation:HorizontalOrVertical.vertical, <lb> location:(myPageWidth/2)}); //Place a guide at the horizontal center of the page. guides.add(undefined, {orientation:HorizontalOrVertical.horizontal, <lb> location:(myPageHeight/2)});
}
You also can create guides using the createGuides method on spreads and master spreads, as shown in the following script fragment (from the CreateGuides tutorial script):
var myDocument = app.documents.add(); with (myDocument.spreads.item(0)){
//Parameters (all optional): row count, column count, row gutter, //column gutter,guide color, fit margins, remove existing, layer. //Note that the createGuides method does not take an RGB array //for the guide color parameter. createGuides(4, 4, "1p", "1p", UIColors.gray, true, true,
myDocument.layers.item(0)); }
Setting grid preferences
To control the properties of the document and baseline grid, you set the properties of the
gridPreferences object, as shown in the following script fragment (from the DocumentAndBaselineGrid
tutorial script):
var myDocument = app.documents.add(); //Set the document measurement units to points. myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points; myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points; //Set up grid preferences. with(myDocument.gridPreferences){
baselineStart = 56; baselineDivision = 14; baselineShown = true; horizontalGridlineDivision = 14; horizontalGridSubdivision = 5 verticalGridlineDivision = 14; verticalGridSubdivision = 5 documentGridShown = true;
}
CHAPTER 3: Documents Basic page layout 26
Snapping to guides and grids
All snap settings for a document’s grids and guides are in the properties of the guidePreferences and
gridPreferences objects. The following script fragment shows how to set guide and grid snap properties
(for the complete script, see GuideGridPreferences):
var myDocument = app.activeDocument; with(myDocument.guidePreferences){
guidesInBack = true; guidesLocked = false; guidesShown = true; guidesSnapTo = true;
} with(myDocument.gridPreferences){
documentGridShown = false; documentGridSnapTo = true; //Objects "snap" to the baseline grid when //guidePreferences.guideSnapTo is set to true. baselineGridShown = true;
}

Changing measurement units and ruler

Thus far, the sample scripts used measurement strings, strings that force InDesign to use a specific measurement unit (for example, “8.5i” for 8.5 inches). They do this because you might be using a different measurement system when you run the script.
To specify the measurement system used in a script, use the document’s
viewPreferences object., as
shown in the following script fragment (from the ViewPreferences tutorial script):
var myDocument = app.activeDocument; with(myDocument.viewPreferences){
//Measurement unit choices are: //* MeasurementUnits.agates //* MeasurementUnits.picas //* MeasurementUnits.points //* MeasurementUnits.inches //* MeasurementUnits.inchesDecimal //* MeasurementUnits.millimeters //* MeasurementUnits.centimeters //* MeasurementUnits.ciceros //Set horizontal and vertical measurement units to points. horizontalMeasurementUnits = MeasurementUnits.points; verticalMeasurementUnits = MeasurementUnits.points;
}
If you are writing a script that needs to use a specific measurement system, you can change the measurement units at the beginning of the script, then restore the original measurement units at the end of the script. This is shown in the following script fragment (from the ResetMeasurementUnits tutorial script):
CHAPTER 3: Documents Basic page layout 27
var myDocument = app.activeDocument with (myDocument.viewPreferences){
var myOldXUnits = horizontalMeasurementUnits; var myOldYUnits = verticalMeasurementUnits; horizontalMeasurementUnits = MeasurementUnits.points; verticalMeasurementUnits = MeasurementUnits.points;
} //At this point, you can perform any series of script actions //that depend on the measurement units you've set. At the end of //the script, reset the measurement units to their original state. with (myDocument.viewPreferences){
try{
horizontalMeasurementUnits = myOldXUnits;
verticalMeasurementUnits = myOldYUnits; } catch(myError){
alert("Could not reset custom measurement units."); }
}

Defining and applying document presets

InDesign document presets enable you to store and apply common document set-up information (page size, page margins, columns, and bleed and slug areas). When you create a new document, you can base the document on a document preset.
Creating a preset by copying values
To create a document preset using an existing document's settings as an example, open a document that has the document set-up properties you want to use in the document preset, then run the following script (from the DocumentPresetByExample tutorial script):
var myDocumentPreset; if(app.documents.length > 0){
var myDocument = app.activeDocument; //If the document preset "myDocumentPreset" does not already //exist, create it. myDocumentPreset = app.documentPresets.item("myDocumentPreset"); try {
var myPresetName = myDocumentPreset.name; } catch (myError){
myDocumentPreset = app.documentPresets.add({name:"myDocumentPreset"}); } //Set the application default measurement units to match the document //measurement units. app.viewPreferences.horizontalMeasurementUnits = myDocument.viewPreferences.horizontalMeasurementUnits; app.viewPreferences.verticalMeasurementUnits = myDocument.viewPreferences.verticalMeasurementUnits;
CHAPTER 3: Documents Basic page layout 28
//Fill in the properties of the document preset with the corresponding //properties of the active document. with(myDocumentPreset){
//Note that the following gets the page margins
//from the margin preferences of the document; to get the margin
//preferences from the active page,replace "app.activeDocument" with
//"app.activeWindow.activePage" in the following line (assuming the
//active window is a layout window).
var myMarginPreferences = app.activeDocument.marginPreferences;
left = myMarginPreferences.left;
right = myMarginPreferences.right;
top = myMarginPreferences.top;
bottom = myMarginPreferences.bottom;
columnCount = myMarginPreferences.columnCount;
columnGutter = myMarginPreferences.columnGutter;
documentBleedBottom =
app.activeDocument.documentPreferences.documentBleedBottomOffset;
documentBleedTop =
app.activeDocument.documentPreferences.documentBleedTopOffset;
documentBleedLeft =
app.activeDocument.documentPreferences.documentBleedInsideOrLeftOffset;
documentBleedRight = app.activeDocument.documentPreferences.
documentBleedOutsideOrRightOffset;
facingPages = app.activeDocument.documentPreferences.facingPages;
pageHeight = app.activeDocument.documentPreferences.pageHeight;
pageWidth = app.activeDocument.documentPreferences.pageWidth;
pageOrientation =
app.activeDocument.documentPreferences.pageOrientation;
pagesPerDocument =
app.activeDocument.documentPreferences.pagesPerDocument;
slugBottomOffset =
app.activeDocument.documentPreferences.slugBottomOffset;
slugTopOffset = app.activeDocument.documentPreferences.slugTopOffset;
slugInsideOrLeftOffset =
app.activeDocument.documentPreferences.slugInsideOrLeftOffset;
slugRightOrOutsideOffset =
app.activeDocument.documentPreferences.slugRightOrOutsideOffset; }
}
CHAPTER 3: Documents Basic page layout 29
Creating a document preset
To create a document preset using explicit values, run the following script (from the DocumentPreset tutorial script):
var myDocumentPreset; //If the document preset "myDocumentPreset" does not already exist, create it. myDocumentPreset = app.documentPresets.item("myDocumentPreset"); try {
var myPresetName = myDocumentPreset.name;
} catch (myError){
myDocumentPreset = app.documentPresets.add({name:"myDocumentPreset"});
} //Fill in the properties of the document preset. with(myDocumentPreset){
pageHeight = "9i"; pageWidth = "7i"; left = "4p"; right = "6p"; top = "4p"; bottom = "9p"; columnCount = 1; documentBleedBottom = "3p"; documentBleedTop = "3p"; documentBleedLeft = "3p"; documentBleedRight = "3p"; facingPages = true; pageOrientation = PageOrientation.portrait; pagesPerDocument = 1; slugBottomOffset = "18p"; slugTopOffset = "3p"; slugInsideOrLeftOffset = "3p"; slugRightOrOutsideOffset = "3p";
}

Setting up master spreads

After setting up the basic document page size, slug, and bleed, you probably will want to define the document’s master spreads:. The following script shows how to do that (for the complete script, see MasterSpread):
myDocument = app.documents.add(); //Set up the document. with(myDocument.documentPreferences){
pageHeight = "11i" pageWidth = "8.5i" facingPages = true; pageOrientation = PageOrientation.portrait;
} //Set the document's ruler origin to page origin. This is very important //--if you don't do this, getting objects to the correct position on the //page is much more difficult. myDocument.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;
CHAPTER 3: Documents Basic page layout 30
with(myDocument.masterSpreads.item(0)){
//Set up the left page (verso). with(pages.item(0)){
with(marginPreferences){
columnCount = 3; columnGutter = "1p"; bottom = "6p" //"left" means inside; "right" means outside. left = "6p" right = "4p"
top = "4p" } //Add a simple footer with a section number and page number. with(textFrames.add()){
geometricBounds = ["61p", "4p", "62p", "45p"];
insertionPoints.item(0).contents = SpecialCharacters.sectionMarker;
insertionPoints.item(0).contents = SpecialCharacters.emSpace;
insertionPoints.item(0).contents = SpecialCharacters.autoPageNumber;
paragraphs.item(0).justification = Justification.leftAlign; }
} //Set up the right page (recto). with(pages.item(1)){
with(marginPreferences){
columnCount = 3;
columnGutter = "1p";
bottom = "6p"
//"left" means inside; "right" means outside.
left = "6p"
right = "4p"
top = "4p" } //Add a simple footer with a section number and page number. with(textFrames.add()){
geometricBounds = ["61p", "6p", "62p", "47p"];
insertionPoints.item(0).contents = SpecialCharacters.autoPageNumber;
insertionPoints.item(0).contents = SpecialCharacters.emSpace;
insertionPoints.item(0).contents = SpecialCharacters.sectionMarker;
paragraphs.item(0).justification = Justification.rightAlign; }
}
}
To apply a master spread to a document page, use the appliedMaster property of the document page, as shown in the following script fragment (from the ApplyMaster tutorial script):
//Assumes that the active document has a master page named "B-Master" //and at least three pages--page 3 is pages.item(2) because JavaScript arrays are zero-based. app.activeDocument.pages.item(2).appliedMaster = app.activeDocument.masterSpreads.item("B-Master");
Use the same property to apply a master spread to a master spread page, as shown in the following script fragment (from the ApplyMasterToMaster tutorial script):
//Assumes that the active document has master spread named "B-Master" //that is not the same as the first master spread in the document. app.activeDocument.masterSpreads.item(0).pages.item(0).appliedMaster = app.activeDocument.masterSpreads.item("B-Master");
Loading...
+ 138 hidden pages