GettingStarted
Illustrator JavaScript 3
8. If no document is open, set the message to reflect that case.
else
{
msgType = "No document Open.";
}
9. Finally, print the message.
alert( msgType );
10. Save the file.
11. Run the script, as described in
above.
The alert box appears and shows the types of all the selected items.
Example 2: Acting on All Objects in a Document
This next example shows how to use the the
pageItems
class to access all the objects in an Illustrator
document, regardless of whether there is a user selection. (For a complete list of objects accessible
through the pageItems Class, see the
JavaScript Reference Pg 119
.)
This script navigates through all the
pageItem
objects in the active document and prints their types.
1. Open a new blank document in your text editor, and save it as
Example2.js
.
2. Declare variables.
var msg = "";
3. Check if a document is open. If it is, get any objects in it.
// Check if a document is open and get all items in it
if ( app.documents.length > 0)
{
var pgItems = app.activeDocument.pageItems;
4. If there are any objects in the array, loop through them to get their types and add them to the message.
// If PageItems exist
if ( pgItems.length > 0 )
{
var msg = 'PageItems in the active document are - ' ;
for ( var i = 0 ; i < pgItems.length ; i++ )
{
msg += '\nItem ' + i + ': ' + pgItems[i].typename ;
}
}
}
Note that you can use either single or double quotes to write strings. Just make sure you close the
string with the same type of quote.