CHAPTER 2: Scripting Features Working with Script Labels 17
The label property can contain any form of text data, such as tab- or comma-delimited text, HTML, or
XML. Because scripts also are text, they can be stored in the
label property.
Page items can be referred to by their
label, just like named items (such as paragraph styles, colors, or
layers) can be referred to by their
name. The following script fragment demonstrates this special case of the
label property (for the complete script, see ScriptLabel):
var myDocument = app.documents.add();
var myPage = myDocument.pages.item(0);
var myX1, myX2, myY1, myY2, myRectangle;
var myPageWidth = myDocument.documentPreferences.pageWidth;
var myPageHeight = myDocument.documentPreferences.pageHeight;
//<fragment>
//Create 10 random page items.
for(var i = 0; i < 10; i++)
{
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 count = 0;
for(var i = 0; i < myPage.pageItems.length; i++)
{
if(myPage.pageItems.item(i).label == "myScriptLabel")
{
count++;
}
}
alert("Found " + count + " 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):