Tải bản đầy đủ (.pdf) (50 trang)

Tài liệu InDesign CS5 Bible- P18 pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (769.61 KB, 50 trang )

Chapter 36: Working with XML
805
Exporting XML Files
When the InDesign document has the proper elements properly tagged to the document’s content,
you’re ready to export it to an XML file for use by a Web site’s content management system or
other XML database. Choose File ➪ Export or press Ô+E or Ctrl+E to open the Export dialog box.
In this dialog box, give the XML file a name in the Save As field (InDesign automatically adds the
file name extension
.xml
), select a folder in which to place the file, and — most important —
choose XML from the Format popup menu (on the Mac) or Save as Type popup menu (in
Windows). Then click Save.
The Export XML dialog box appears. It opens with the General pane. This pane is simple:
l
Include DTD Declaration: Select this check box to include the DTD information in the
exported file. Otherwise, the XML database needs to import the DTD file separately.
l
View XML Using: Select this check box and select a Web browser or Web-supporting
application in the adjacent popup menu to preview the XML file in that browser or
program.
l
Export from Selected Element: If an element is selected in the Structure pane, the
Export from Selected Element check box is available; if it’s selected, it exports the XML file
only from that element on.
l
Export Untagged Tables as CALS XML: To convert InDesign tables not tagged with
XML attributes to the CALS XML format, select this check box.
l
Remap Break, Whitespace, and Special Characters: To convert special characters to
their XML codes, select this check box.
l


Apply XSLT: If this check box is selected, InDesign applies the XSLT style sheet either
from the XML file or from a separate file; you specify which by using the popup menu to
its right.
l
Encoding: Use this popup menu to choose the text encoding mechanism — a way of
representing international characters across different computer systems. Your content
engineer or Webmaster will tell you whether to select UTF-8, UTF-16, or Shift-JIS.
In the Graphics pane, you tell InDesign how to handle the output of any tagged pictures. Your
options are as follows:
l
Image Options section: Here, you indicate what images to copy to the Images subfolder
that InDesign creates. Your choices are Original Images, Optimized Original Images, and
Optimized Formatted Images. You can select any or all of these. The optimized images are
converted to GIF or JPEG for use on the Web unless you specify GIF or JPEG in the Image
Conversion popup menu rather than leave the default setting of Automatic; the formatted
images crop the images to reduce file size.
l
GIF Options section: Here, you choose the color palette in the Palette popup menu, with
choices of Adaptive (No Dither), Web, System (Mac), and System (Win). Ask your
Webmaster what to use; usually, you select Adaptive (No Dither). For very large images,
50_607169-ch36.indd 80550_607169-ch36.indd 805 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part IX: Programming InDesign
806
interlacing can make on-screen display over the Web seem faster (by building the image
line by line rather than waiting until the whole image has been transferred to the browser
before anything appears); if you want to have such interlaced display of GIF files, select
the Interlace option.
l
JPEG Options section: Here, you have options similar to those for GIF files. In the Image

Quality popup menu, choose the desired image quality (Low, Medium, High, and
Maximum). In the Format Method, choose Progressive to have the file displayed progres-
sively (similar to the interlace option for GIF files) or Baseline to display it all at one time.
Unless your images are very large, keep the default Baseline.
When you export an XML file with graphics, InDesign automatically embeds XMP media manage-
ment properties with the graphics’ tags. You can view those properties by choosing File ➪ File Info
or pressing Option+Shift+Ô+I or Ctrl+Alt+Shift+I and then choosing the Advanced pane. This
information may be of interest to your content engineer but has no real meaning for the page
designer. However, the content engineer may want to fill out the information in the File Info dialog
box’s other panes — Description and Origin — into which you can add comments, creator infor-
mation and contact details, copyright information, credit and source information, keywords, and
copyright URLs.
Exporting to IDML
Another way to export to XML format is by using the InDesign Markup Language (IDML) format.
IDML is based on XML, so developers can work on InDesign layouts without needing InDesign.
Note that IDML is not exactly XML, so only IDML-aware programs can use the IDML format; these
are typically special apps or plug-ins to XML-aware apps meant to work specifically with InDesign
files. For example, a company might add IDML capabilities to a catalog publishing system so that it
can generate InDesign files directly from its catalog databases.
Cross-Reference
IDML is also the format used to save an InDesign CS5 layout in a format that InDesign CS4 can open, as
Chapter 4 explains.
n
By using XML as the IDML file format’s basis, all of InDesign’s capabilities and content attributes
are available to developers for, for example, layout automation, database publishing, and content
management system applications. This supplements InDesign’s existing ability to expose all its
internal XML capabilities via scripting, so other XML- and script-aware apps can work with
InDesign directly. Note that none of this affects the designer using InDesign in terms of using
InDesign itself.
You export an InDesign layout to IDML format by choosing InDesign Markup (IDML) in the

Format popup menu (on the Mac) or Save as Type popup menu (in Windows) in the Export dialog
box (choose File ➪ Export or press Ô+E or Ctrl+E). There are no options; InDesign just creates the
IDML file when you click Save.
50_607169-ch36.indd 80650_607169-ch36.indd 806 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 36: Working with XML
807
Summary
InDesign lets you tag content within documents with XML tags, so you can transfer that content to
an XML database or Web content engine from which it can be used in a variety of media, format-
ted as appropriate for each. You can also import XML content into InDesign, automatically placing
content into the appropriate frames if you’ve created an appropriate template and tagged it with
the right XML tags. But InDesign’s XML tools are only the beginning — you need an XML database
and content-creation and content-presentation tools to use the XML data derived from InDesign
documents.
For organizations creating complex content creation and management workflows using XML,
InDesign exposes all of its functionality as XML rules that can be scripted, essentially letting you
flexibly control the creation of InDesign files based on XML programming and XML data attributes.
It also has the new InDesign Markup Language (IDML) file format that is based on XML, so devel-
opers can create and work with InDesign-compatible files in their own apps, using XML as the
standard description for any elements they work with across apps.
50_607169-ch36.indd 80750_607169-ch36.indd 807 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
50_607169-ch36.indd 80850_607169-ch36.indd 808 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
809
CHAPTER
Using Scripts
IN THIS CHAPTER
Installing and accessing scripts

Exploring JavaScript
Exploring AppleScript
Exploring VBA
Writing scripts
Learning more about scripting
S
cripting automates many features in InDesign — it’s essentially a way
to program InDesign to do specific actions. Because InDesign uses
standard script languages, you can also run scripts that work with
multiple programs in concert, including InDesign. (All the applications must
support the same scripting language, of course.) For example, you might use
scripts to automate database publishing, such as to run a database search,
export data to a text file, import that file into InDesign, and then apply the
appropriate formatting.
InDesign supports three scripting languages:
l
JavaScript on both Mac and Windows
l
AppleScript on the Mac only
l
Visual Basic for Applications (VBA) on Windows only
Because only JavaScript is supported by both platforms, I recommend you
use it wherever possible so that your scripts can work in cross-platform envi-
ronments. InDesign doesn’t force you to choose just one scripting language,
so you could keep using old AppleScript or VBA scripts created for previous
versions of InDesign, as well as new scripts written in JavaScript.
As you become comfortable with scriptwriting, you’re also likely to discover
that virtually everything you do with InDesign is a repetitive task. The more
you can free yourself of this kind of work by using scripts, the more time
you have to be creative. The possibilities are endless. But before you get too

excited, remember that scripting is programming, so most layout artists stay
clear of it, using scripts only if they have a programmer available to write
them.
51_607169-ch37.indd 80951_607169-ch37.indd 809 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part IX: Programming InDesign
810
Using Scripts
Accessing scripts is easy; they show up in the Scripts panel (choose Window ➪ Utilities ➪ Scripts or
press Option+Ô+F11 or Ctrl+Alt+F11) if you’ve placed scripts in the
Scripts
folder inside the
folder that contains the InDesign application, as shown in Figure 37.1. Scripts don’t have to be in
the
Scripts
folder — they can be anywhere on your computer — but to use them outside this
folder means you have to double-click them from your desktop rather than have access from
within InDesign.
FIGURE 37.1
The Scripts panel in InDesign and its flyout menu
Script tips
When writing scripts, you can associate a script to a specific InDesign menu action so that when a
user chooses that menu item, a script runs automatically (technically, by assigning a script to a
MenuAction command’s Prenotify or Postnotify property when writing the script’s code). In
InDesign, be sure that Enable Attached Scripts is also enabled in the Scripts panel’s flyout menu.
When running scripts, keep the following tips in mind:
l
You can undo all of a script’s action by using the InDesign Undo command (choose
Edit ➪ Undo or press Ô+Z or Ctrl+Z) by ensuring that Undo Affects Entire Script is
enabled in the Scripts panel’s flyout menu. Otherwise, the undo command reverses just

the last action within the script, requiring you to undo several times to roll back the com-
plete script action.
l
By enabling the Enable Redraw flyout menu option in the Scripts panel, you can force
InDesign to redraw the display while a script is running. This ensures that any changes to
the document are immediately visible.
New Feature
You can now access InDesign’s Tools panel and its tools via scripts in InDesign CS5.
n
51_607169-ch37.indd 81051_607169-ch37.indd 810 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 37: Using Scripts
811
Script locations
Whether you create your own scripts or get them from vendors or other users, you need to save
them so that InDesign knows they exist. You have a choice of two locations:
l
The
Scripts
folder inside the InDesign application folder. You can usually find it as
Applications:Adobe

InDesign

CS5:Scripts
in Mac OS X, and
Program

Files\Adobe\InDesign


CS5\Scripts
in Windows. This makes the script available
to all users of your computer. If you want the script to start when InDesign launches,
place it in the
Startup

Scripts
subfolder.
l
The
Script

Panel
folder. This makes the script available only to that specific user
(note that it will appear in a folder called
User
in your Scripts panel). The path varies
based on your operating system:
l
On the Mac, the path is
Users:
username
:Library:Preferences:Adobe

InDesign:Version

7.0:en_US:Scripts:Scripts

Panel
.

l
In Windows XP, the path is
Documents

and

Settings\
username
\Application

Data\Adobe\InDesign\Version

7.0\en_US\Scripts\Scripts

Panel
.
l
In Windows Vista, the path is
Users\
username
\AppData\Roaming\Adobe\
InDesign\Version

7.0\en_US\Scripts\Scripts

Panel
.
l
In Windows 7, the path is
Users\

username
\AppData\Roaming\Adobe\
InDesign\Version

7.0\en_US\Scripts\Scripts

Panel
.
The script shows up in the Scripts panel. If it doesn’t show up immediately, quit and reopen
InDesign.
Tip
To use scripts developed for InDesign CS4, create a new folder called
Version

6.0

Scripts
inside the
Scripts
or
Script

Panel
folder and move your old scripts there. Likewise, scripts developed for InDesign
CS3 should be placed in a folder called
Version

5.0

Scripts

. These scripts then work in InDesign CS5.
n
Scripting principles
No matter what scripting language you use, there are several basic principles to observe. These fall
into four basic categories:
l
Grammar: All languages — including programming languages such as Pascal and C++, as
well as scripting languages — include grammatical components that are used in standard-
ized sequences. In English, we combine nouns, verbs, adjectives, adverbs, and so on to
create sentences. Everybody knows the meaning of “The weather is especially nice today,”
because it uses common words in a sequence that makes sense. The sentence “Nice is the
especially today weather” has the right components but it’s arranged in the wrong
sequence, so the meaning is lost.
51_607169-ch37.indd 81151_607169-ch37.indd 811 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part IX: Programming InDesign
812
l
Statements and syntax rules: In JavaScript, AppleScript, and VBA, verbs, nouns, adjec-
tives, and prepositions are combined to create statements; statements are combined to
form scripts. Verbs are also called commands and methods; nouns are called objects; and
adjectives are called properties. Syntax rules specify how statements and scripts must be
constructed so that a computer can understand them.
l
Object hierarchy: All three scripting languages use a structural element called an object
hierarchy. It’s a fancy term for a simple concept. An object hierarchy works like a set of
boxes within boxes. A large box contains a smaller box, which contains a smaller box,
which contains a smaller box, and so on, until you reach the smallest box, which contains
nothing and is the final level in the hierarchy of boxes.
l

InDesign hierarchy: InDesign contains its own hierarchy, which lends itself nicely to
scripting. A document contains pages, pages contain frames, and frames contain text and
pictures. You can create scripts that perform actions at any of these levels. In other words,
with scripts you can create documents, add pages, add items to pages, and modify the
contents of frames, right down to a particular character in a text frame. You can think of
this hierarchy in InDesign as a chain of command. You can’t talk directly to an item that’s
at the bottom of the chain. Rather, you must first address the top level, and then the next,
and so on, until you’ve reached the item at the bottom of the chain. This is analogous to
the way you use InDesign: You create new documents, add pages, place text and graphics
on the pages, and, finally, modify the contents of the frames containing those items.
If you’re thinking about dabbling with any of the scripting languages supported by InDesign, the
following words of both caution and encouragement are in order. First the encouragement: You
don’t necessarily need programming experience, scripting experience, or a pocket protector to
begin creating scripts. A bit of curiosity and a touch of patience will suffice. Now the caution:
Scripting is essentially a euphemism for programming (that is, figuring out the right commands
and then typing them for the application to execute). Writing scripts isn’t a matter of choosing
commands from menus, and clicking and dragging them, or entering values into fields; nor is it
like writing a limerick. If you’re starting from scratch, know in advance that you have to learn
some new skills.
Learning to create scripts is like learning to swim: You can read books, documentation, and articles
until your head spins, but eventually you have to get a little wet. The best way to learn about
scripting is to write a script. So put on your swimsuit and dive in.
Be forewarned: There’s something almost narcotic about creating scripts, and it’s not uncommon
for novice scriptwriters to get hooked. Don’t be surprised if what starts out to be a 15-minute look-
see turns into a multihour, late-night programming episode.
Note
Because scripting languages differ, you can’t always duplicate the functionality of a specific script in one lan-
guage into a script written in a different language.
n
51_607169-ch37.indd 81251_607169-ch37.indd 812 4/22/10 8:09 PM4/22/10 8:09 PM

Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 37: Using Scripts
813
Note
Adobe has a nearly 2,000-page scripting guide available as a PDF file. It comes with your InDesign or Creative
Suite software, residing on the installation DVD. You can find additional resources at
www.adobe.com
.
n
Exploring JavaScript
JavaScript is a scripting language developed by Netscape Communications, based on Sun
Microsystems’s Java language that was meant to let Web browsers manage resources on far-flung
servers by running scripts to control the servers from a desktop. JavaScript soon became a popular
scripting language because, as does Java, it runs on so many types of computers, including
Windows, Mac, and Unix. But because it is based largely on the object-oriented approach taken by
professional computer languages such as C and C++, it can be difficult for nonprogrammers to use.
There are lots of JavaScript editor programs available. Most of these are developed by individuals
and small firms, so the list is always changing. I recommend you use the Google search engine
(
www.google.com
) and search for JavaScript editor to find the most current programs. A great
script-editing program for Mac users is Bare Bones Software’s venerable BBEdit; you can get more
information at
www.barebones.com
.
Learning the language
JavaScript is a very complex language based on object-oriented programming, which abstracts
items and attributes as objects that are then grouped, changed, or otherwise manipulated. This
means that JavaScript is less English-like than other scripting languages because it requires you to
spend a fair amount of time setting up the objects before you can manipulate them.

myObject.strokeTint = newValue;
This example shows that there is a current object named
strokeTint
being set to a new value;
the actual value for
newValue
is set earlier in the script.
What you need to write and run scripts
You need a program that can display, edit, and test your JavaScript; there is no bundled JavaScript
editor in Windows or Mac OS X. Such editors typically format the JavaScript code for you, indent-
ing it automatically, graying out comments, and highlighting certain keywords.
You can use a word processor or text editor to write and edit scripts, but such programs can’t
check the syntax or automatically format the script text to help show nested loops, conditional
branches, and so on. Also, you can usually use an HTML editor such as Adobe Dreamweaver in
which to edit JavaScripts, though they also typically don’t provide any debugging tools to help you
track and fix coding (syntax) errors. (Figure 37.2 shows a JavaScript script being edited in
Dreamweaver.) In this case, you need to open the error window in your browser as you test the
code and see if it identifies the error location to help you find it in your HTML editor.
51_607169-ch37.indd 81351_607169-ch37.indd 813 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part IX: Programming InDesign
814
FIGURE 37.2
A JavaScript program viewed in Adobe Dreamweaver
Running your script
The easiest way to run a script is to double-click it in the Scripts panel within InDesign, but you
also can simply double-click the script file; note that InDesign may need to be open. Also, while
you’re developing the script, you can run the script directly from the application in which you
created it — again, InDesign may need to be open. If you’ve done everything correctly, you see
Before you venture too far into scripting, you should review the JavaScript-related information provided

with InDesign:
l
JavaScript documentation and tools: Sun places the very technical JavaScript documentation
on its Web site at
/>.
l
InDesign scripting documentation: The InDesign installation DVD contains a 2,000-plus-
page PDF file that explains scripting for InDesign. This document, although a bit on the tech-
nical side, is a valuable resource. It includes an overview of JavaScript scripting and the
object model, as well as a list of InDesign-specific scripting terms and scripting examples.
If you want still more information about JavaScript, several books are available, including Beginning
JavaScript, Fourth Edition, by Paul Wilton and Jeremy McPeak and JavaScript Bible, Seventh Edition, by
Danny Goodman et al. (both from Wiley Publishing).
Getting More Information on JavaScript
51_607169-ch37.indd 81451_607169-ch37.indd 814 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 37: Using Scripts
815
InDesign become the active program, and then the actions you put in your script take place. Voilà
and congratulations! You can now call yourself a scripter without blushing. That’s all there is to
creating and running a script.
Saving your script
When you’re finished writing and testing a script, choose Save from the script editor’s File menu.
Name your script and choose its storage location. Move or copy the saved script to one of the two
locations described earlier in this chapter.
Exploring AppleScript
AppleScript is a scripting language developed by Apple and initially released with System 7.5 that
can be used to control Macs, networks, and scriptable applications, including InDesign. The
AppleScript language was designed to be as close to normal English as possible so that average Mac
users — specifically, those who aren’t familiar with programming languages — can understand

and use it.
Note
InDesign can run text-only AppleScripts in addition to compiled (binary) ones.
n
Learning the language
Many of the actions specified in AppleScripts read like sentences you might use in everyday con-
versation, such as:
set color of myFrame to "Black"
or
se t applied font of myCharacterStyle to "Times"
What you need to write and run scripts
The script editor provided with the Mac OS lets you write scripts. In Mac OS X 10.6 Snow
Leopard, you find the AppleScript Editor inside the
Utilities
folder inside your
Applications
folder (at the root level of your hard drive). In Mac OS X 10.5 Leopard, you find
the Script Editor inside the
AppleScript
folder inside your
Applications
folder.
Note
Mac OS X 10.6 Snow Leopard calls the editor AppleScript Editor, whereas Leopard calls it just Script Editor.
I use the general term script editor to refer to both. They work the same way.
n
51_607169-ch37.indd 81551_607169-ch37.indd 815 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part IX: Programming InDesign
816

An uncompiled script is essentially a text file, so you can actually write scripts with any word pro-
cessor. The script editor, however, was created for writing AppleScripts and includes several handy
features for scriptwriters.
Tip
To see what AppleScript commands InDesign supports, you can view the InDesign object model — the sup-
ported commands — within the script editor; just choose File ➪ Open Dictionary and then scroll to the
InDesign application icon and click Open.
n
Checking for syntax errors
The next step is to determine whether the statements are correctly constructed. Click the Check
Syntax button. If the script editor encounters a syntax error, it alerts you and highlights the cause
of the error. If the script’s syntax is correct, all statements except the first and last are indented and
a number of words appear in bold, as illustrated in Figure 37.3. Your script has been compiled and
is ready to test.
Running your script
To run a script from InDesign, double-click the script in the Scripts panel. Alternatively, double-
click the script file itself (InDesign may need to be open). When developing the script, click the
Run button in the script editor; InDesign becomes the active program, and your script is running.
Before you venture too far into scripting, you should review the AppleScript-related information pro-
vided with the Mac OS and with InDesign:
l
Mac scripting documentation and tools: Apple places the AppleScript documentation on its
Web site at
www.apple.com/applescript
. In your hard drive’s
Applications
folder,
you should have a folder called
AppleScript
that contains the Script Editor program, along

with a folder of example scripts and the AppleScript Script Menu that adds the Script menu to
the Finder. Apple also offers a professional AppleScript editor called AppleScript Studio for
download at its developer Web site,
www.apple.com/macosx/features/xcode/
. Apple
makes AppleScript Studio part of its Xcode scripting tool, so you need to get Xcode to get
AppleScript Studio.
l
InDesign scripting documentation: The InDesign DVD contains a 2,000-plus-page PDF file
that explains scripting, including AppleScript programming, for InDesign. This document,
although a bit on the technical side, is a valuable resource. It includes an overview of Apple
events scripting and the object model, as well as a list of InDesign-specific scripting terms
and scripting examples.
If you want still more information about AppleScript, several books are available, including AppleScript
by Mark Munro, and Beginning AppleScript, by Stephen G. Kochan (both from Wiley Publishing).
Getting More Information on AppleScript
51_607169-ch37.indd 81651_607169-ch37.indd 816 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 37: Using Scripts
817
If you have trouble getting a script to run, double-check the name that InDesign uses for itself. It
might use InDesign® CS5 or simply InDesign® (yes, the name may include the registered trade-
mark symbol). If you run a script from AppleScript (instead of just double-clicking it) and
AppleScript can’t find InDesign, it gives you a dialog box with which you find the InDesign pro-
gram. When you’ve found and selected the InDesign application, AppleScript finds out what
InDesign’s file name is and uses that in your script.
FIGURE 37.3
This script editor window contains sample AppleScript text.
Saving your script
When you’re finished writing and testing a script, choose Save from the script editor’s File menu.

Name your script, choose its storage location, and choose Compiled Script from the Format popup
menu. Move or copy the saved script to one of the two locations described earlier in this chapter.
If you save the script in Application format and want to edit your script later, you must open it by
dragging and dropping it on the script editor. This is because Application-format scripts are
designed to immediately run when double-clicked. You would choose the Application format
when creating scripts for use by others because chances are you don’t want them to open the script
in the script editor but instead simply use the script by double-clicking it like any other applica-
tion. (It doesn’t matter whether the script is in Application format if you run it from InDesign’s
Scripts panel.)
51_607169-ch37.indd 81751_607169-ch37.indd 817 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part IX: Programming InDesign
818
Locating more AppleScript tools
A few software utilities are also available for AppleScripters. The most widely used is Script
Debugger, from Late Night Software ($199,
www.latenightsw.com
); it’s an interface develop-
ment tool that quickly creates AppleScript-based applications that have the standard Mac look and
feel. Apple also offers its AppleScript Studio as a free download to developers who register at the
Apple site (
www.apple.com/macosx/features/xcode
); this is more capable than the basic
Script Editor that comes with Mac OS X. AppleScript Studio is part of Apple’s Xcode scripting tool,
so you need to get Xcode to get AppleScript Studio.
Exploring VBA
VBA, and its subset version VBScript, is Microsoft’s technology for writing your own programs,
both those that run in other programs (scripts) and those that run by themselves (custom applica-
tions). InDesign works with both VBA and VBScript. The Visual Basic language that underlies both
VBA and VBScript is not meant for everyday computer users; knowledge of programming is very

useful in taking advantage of this technology. Although based on the Basic language developed in
the 1970s to help new users write their own programs, it has evolved a lot since then and is no
longer so simple.
Learning the language
Many of the actions specified in VBA have some degree of English, such as:
set myTextFrame = InDesign.Documents.Item(1).Spreads.Item(1).
TextFrames.Add
or
mySelection.RotationAngle = 30
As you can see, that degree of English is slight. The first code segment, for example, means to add
a text frame to the first spread in the first document. The second means to rotate the selected
object by 30 degrees.
What you need to write and run scripts
To use InDesign scripting in Windows, you need Microsoft Visual Basic or an application that con-
tains VBA; these include Microsoft Office, Microsoft Visio, and AutoCAD. In Microsoft Office, you
can run the Microsoft Script Editor by choosing Tools ➪ Macro ➪ Microsoft Script Editor, which
lets you create scripts, edit them, test your code, and fix errors. Figure 37.4 shows the editor with
a sample script. (Note that the script editor’s dialog box has the self-important name Microsoft
Development Environment rather than Microsoft Script Editor.)
51_607169-ch37.indd 81851_607169-ch37.indd 818 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 37: Using Scripts
819
FIGURE 37.4
The Microsoft Development Environment window contains sample VBA text. When you work on a
script, the Microsoft Development Environment applies indents automatically.
Before you venture too far into scripting, you should review the VBA-related information provided by
Microsoft and with InDesign:
l
Microsoft scripting documentation and tools: Microsoft has a lot of information on VBA,

VBScript, and Visual Basic on its Web site. Unfortunately, it’s not well organized and thus is
hard to find and understand. There’s no tutorial that simply explains how a new scripter
needs to get started. However, you can search on the Microsoft site for VBA, VBScript, and
Visual Basic to get links to documents that may prove useful.
l
InDesign scripting documentation: The InDesign DVD contains a 2,000-plus-page PDF file
that explains scripting in InDesign, including VBA programming. This document, although
definitely on the technical side, is a valuable resource. It includes an overview of VBA script-
ing and the object model, as well as a list of InDesign-specific scripting terms and scripting
examples.
If you want still more information about VBA and its two sister technologies, several books are avail-
able, including VBScript Programmer’s Reference, Third Edition, by Adrian Kingsley-Hughes, Kathie
Kingsley-Hughes, and Daniel Read; VBA For Dummies, Fifth Edition, by John Paul Mueller; and
Mastering Microsoft VBA, Second Edition, by Guy Hart-Davis (all from Wiley Publishing).
Getting More Information on VBA
51_607169-ch37.indd 81951_607169-ch37.indd 819 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part IX: Programming InDesign
820
You can also write scripts in VBScript, a VBA subset, in a text editor such as WordPad. You need
Microsoft’s free Windows Scripting Host (
WSCRIPT.EXE
), which is usually installed with
Windows, and you can download it from Microsoft’s Web site.
There’s a third choice for your scriptwriting: You can also use the full Microsoft Visual Basic product.
Note
To use InDesign scripting in Windows, your user profile must have Administrator privileges.
n
Tip
To see what Visual Basic commands InDesign supports, you can view the InDesign object model — the sup-

ported commands — within the Microsoft’s Visual Basic product; just choose Project ➪ References and then
select Adobe InDesign CS5 Type Library in the References dialog box and click OK.
n
Running your script
There are a few ways to run a VBScript. You can double-click the script in the Scripts panel within
InDesign or double-click the script file itself from a folder. When developing scripts, you can run
the script directly from the application in which you created a VBA or Visual Basic script, such as
the Microsoft Script Editor. (VBScripts can be run from the Scripting Host application.) In all these
cases, InDesign becomes the active program, and your script is running.
Saving your script
When you’re finished writing and testing a script, choose Save from the script editor’s File menu.
Name your script and choose its storage location. Move or copy the saved script to one of the two
locations described earlier in this chapter.
Creating and Running Scripts
At this point, I’m assuming that the appropriate scripting software is installed on your computer.
If this is the case, you’re ready to begin. For your first trick, you’re going to make InDesign roll
over — sort of. Actually, you’re going to rotate an EPS graphic. First, you’ll prepare InDesign for
its role. Launch the program and then create a new document (do not select Automatic Text
Frame). In the middle of the first page, place an EPS graphic. Make sure that it remains active
after you place it.
Writing simple scripts
The following three scripts, taken from Adobe’s InDesign script examples, do the same thing in
JavaScript, AppleScript, and VBA — they rotate an EPS graphic and its frame.
51_607169-ch37.indd 82051_607169-ch37.indd 820 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 37: Using Scripts
821
Cross-Reference
All the scripts in this chapter are available for download at
www.InDesignCentral.com

. Just cut and paste
them from the text files into the appropriate scripting editor. Adobe’s user forums are also a good place to go
for scripting help.
n
Type the lines that follow this paragraph exactly as they’re written for the scripting language you’ve
chosen. Type a return character at the end of each line. Note also the use of straight quotation
marks instead of curly typesetter’s quotes (the script editor does this for you). Be very careful when
you type the text: Typos are script killers.
Note
In the code samples below, the ¬ symbol indicates where a line of code is broken to fit the page width; in the
actual script, there would be no ¬ character, and the line that follows would be combined with the line that
now ends in ¬.
n
JavaScript
var myL ink, myEPs, myFrame;
if(app.documents.length != 0){
if(app.activeDocument.links.length != 0){
for(var myLinkCounter = 0; myLinkCounter < ¬
app.activeDocument.links.length; myLinkCounter ++){
myLink = app.activeDocument.links.item(myLinkCounter);
if(myLink.linkType == "EPS"){
myEPS = myLink.parent;
myFrame = myEPS.parent;
myFrame.rotationAngle = 30;
}
}
}
}
This script first searches through all the items on the page and checks whether any are EPS; if so, it
sets the rotation of the item to 30. (Thanks to Adobe’s InDesign scripting guru, Olav Martin Kvern,

for developing the JavaScript code example.) Note the sequence of actions: First you verify that
there is a nonempty document open, and then you check the content type of objects. For those
objects of the EPS type, you activate the frame and then apply the rotation to that frame.
JavaScripts require you to set your own variables (the
var
statements) that define what the object
is. (Here,
myLink
is used for each object,
myEPS
is the object that contains the EPS attribute, and
myFrame
is the parent frame containing the EPS graphic.) You work with those variables to see
what their attributes are and then change those attributes.
If you’re in an adventurous mood, try substituting the statement
myFrame.rotationAngle

=

30;
in the preceding script with each of the following statements:
myFrame.shearAngle = 30;
myFrame.verticalScale = 200;
51_607169-ch37.indd 82151_607169-ch37.indd 821 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part IX: Programming InDesign
822
If you want to get really fancy, combine all the
myFrame.
statements into a single script so that

you can use the script to make all the changes at one time.
AppleScript
tell applic ation "Adobe InDesign CS5"
activate
set myPageItems to {EPS, oval, rectangle, polygon}
set mySelection to selection
if class of item 1 of mySelection is in myPageItems ¬
and (count mySelection) > 0 then
if class of item 1 of mySelection is EPS then
set myFrame to parent of mySelection
else
set myFrame to item 1 of mySelection
end if
set rotation angle of myFrame to 30
end if
end tell
Note
Be sure that a document is open and that you have selected at least one frame of any type before starting the
script. Also in the script itself, make sure to type the name of your InDesign program exactly as it appears in
the Finder. Because you’re free to rename your program, the name may not match the name in the first line of
the script.
n
If you’re feeling daring, try substituting the statement
set

rotation

angle

of


myFrame

to

30

in the preceding script with each of the following statements:
set shear angle of myFrame to 30
set vertical scale of myFrame to 200
Here again, you can get fancy by combining all the
set
statements into a single script so that you
can use the script to make all the changes at one time.
VBA
Dim myInDesign As InDesign.Application
Set myInDesign = CreateObject("InDesign.Application.CS5")
Set mySelection = myInDesign.Selection
If TypeName(mySelection.Item(1)) = "EPS" Then
mySelection.Parent.RotationAngle = 30
Else
mySelection.RotationAngle = 30
End If
You might satisfy your adventurous streak by substituting the statement
mySelection.
RotationAngle

=

30

in the preceding script with each of the following statements:
51_607169-ch37.indd 82251_607169-ch37.indd 822 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 37: Using Scripts
823
set the color of the current box to "Blue"
set the shade of the current box to 50
set the width of the frame of the current box to 10
set the box shape of the current box to ovular
Go to town if you want: Combine all the
set
statements into a single script so that you can use the
script to make all the changes simultaneously.
Tip
Perhaps you noticed the chain of command used in the preceding scripts. First the script addresses InDesign,
then the active document (layout), and finally the active frame. If you understand this concept, you’ll be script-
ing like a pro in no time.
n
Labeling items
As you can see from the examples in the previous section, scripts often refer to items by their type
and location in the document. But there’s another way to refer to objects that makes sure you can
select an item precisely: You can label, or name, an item. You do so in the Script Label panel
(choose Window ➪ Utilities ➪ Script Label). The process is easy: Select the object and then type a
name in the panel. That’s it!
When writing scripts, you refer to the labeled object as follows. In these examples, the label is
TargetFrame, and don’t worry that the samples seem to do different things — they in fact are unre-
lated examples, not variations of the same command.
JavaScript
with(app.docum ents.item(0).pages.item(0)){
myTargetFrame = textFrames.item("myTargetFrame");

}
AppleScript
select (page item 1 of page 1 of myTargetDocument whose ¬
label is "TargetFrame")
VBA
Set myAsset = myLibrary.Assets.Item("TargetFrame")
Writing conditio nal scripts
Some scripts simply automate a set of tasks in documents whose content is predictable. But more
often than not, documents differ, so you need conditional statements to evaluate certain things to
see whether they are true before applying a script’s actions. Otherwise, you get an error message
when something turns out not to be true. As a simple example, a script that does a search and
replace needs to have a document open and a frame selected. If no frame is selected, the script
won’t know what to search, and the user gets an error message.
51_607169-ch37.indd 82351_607169-ch37.indd 823 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part IX: Programming InDesign
824
The same issue arises for repeated series of actions, where you want the script to do something for
all occurrences. The script needs to know what to do when it can’t find any more such occur-
rences. As an example, look at the following script, which counts all open documents. For it to
work, at least one document has to be open, so the script checks first to see whether in fact any
documents are open and then delivers an error message that the user can understand if none is
open. The rotate-EPS-graphic script earlier also used a conditional to make sure there was an EPS
graphic in the document. Notice that in all three scripting languages, you use the command
if
to
set up such conditionals.
JavaScript
if (app.document s.length==0){
alert("No InDesign documents are open!");

}
Note
JavaScript uses == for comparing values (as in the example above) and = for assigning values. Visual Basic and
AppleScript use = for both purposes.
n
AppleScript
tell application "Adobe InDesign CS5"
activate
set myNumberOfDocuments to (count documents)
if myNumberOfDocuments = 0 then
display dialog "No InDesign publications are open!"
end if
end tell
VBA
Dim myInDesign as InDesign.Application
Set myInDesign = CreateObject ("InDesign.Application.CS4")
If myInDesign.Documents.Count
MsgBox "No InDesign publications are open!"
End If
End Sub
Another form of conditional is what’s called a control loop, in which an action occurs either for a
specified number of iterations or until a condition is met. The following scripts show an example
of each for each language. Note the use of comments in the scripts — a handy way to document
what you’re doing for later reference. In JavaScript, a single-line comment begins with
//
, whereas
a multiline comment begins with
/*
and ends with
*/

. In AppleScript, a comment begins with
--

and continues until you press Enter or Return. In VBA, it begins with
Rem
followed by a space,
and it continues until you press Enter or Return.
51_607169-ch37.indd 82451_607169-ch37.indd 824 4/22/10 8:09 PM4/22/10 8:09 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

×