Variable Names; Assigning Values To Variables - Adobe 27510753 - InDesign CS2 - PC Manual

Scripting guide
Hide thumbs Also See for 27510753 - InDesign CS2 - PC:
Table of Contents

Advertisement

Adobe InDesign CS2 Scripting Guide
The same operation in JavaScript is more similar to VBScript than it is to AppleScript (in InDesign JavaScript,
is a reference to the InDesign application). To refer to an existing frame:
app
var myTextFrame = app.documents[0].spreads[0].textFrames[0];
//or
var myTextFrame = app.documents.item(0).spreads.item(0).textFrames.item(0);
or to assign a variable as you create a frame:
var myTextFrame = app.documents[0].spreads[0].textFrames.add();
//or
var myTextFrame = app.documents.item(0).spreads.item(0).textFrames.add();
Note:
In JavaScript, all variables that are not preceded by var are considered global by default—that is, they
are not bound to a specific function; var is not required, but we recommend that you use var in any
script with more than a single function. In AppleScript and VBScript, variables are local unless specifi-
cally defined as global variables. This means that the variables do not persist outside the function in
which they are created.
In general, in any of the languages, it's best to use local variables and pass specific values to functions/
subroutines/handlers rather than defining and using global variables. In JavaScript, you define a variable as a
local variable using var , as shown in the preceding example. To specify that several variables in a JavaScript
function are local variables, you can list them at the beginning of the function; for example:
function myFunction(){
var myNumber, myString;
}

Variable names

Try to use descriptive names for your variables, such as firstPage or corporateLogo , rather than x or c .
This makes your script easier to read. Longer names do not affect the execution speed of the script.
Variable names must be a single word, but you can use internal capitalization (such as myFirstPage ) or
underscore characters ( my _ first _ page ) to create more readable names. Variable names cannot begin
with a number, and they can't contain punctuation or quotation marks.
You might also want to give your variable names a prefix so that they'll stand out from the objects,
commands, and keywords of your scripting system. All of the variables used in this book, for example, begin
with my .

Assigning values to variables

In AppleScript, use set to assign any value type to a variable:
set myDocument to active document
set myString to "X"
In VBScript or Visual Basic versions other than VB.NET, use Set to assign object references to variables, but not
to string, array, or number variable types:
Set myDocument = myInDesign.Documents.Add
myString = "X"
In VB.NET, you do not need to use Set for object variables:
myDocument = myInDesign.Documents.Add
myString = "X"
Scripting Basics
11

Hide quick links:

Advertisement

Table of Contents
loading

This manual is also suitable for:

Indesign cs2

Table of Contents