Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Design > Adobe Creative Suite

  • Print
  • + Share This
This chapter is from the book

The Obligatory "Hello World" Tutorial

Tradition dictates that the first project you attempt with a new computer language be the "Hello World" project (look up the history on http://en.wikipedia.org). The project usually entails creating a small program to write "Hello World" onscreen. Not very exciting maybe, but tradition is tradition. Thus, our first project will be to draw, with the aid of AppleScript, a circle in Illustrator (the world) with a blue fill and no stroke. Our script will then write the word hello centered in the circle with a white fill.

To begin the tutorial, carefully type the following script in Script Editor:

tell application "Adobe Illustrator"
   activate
   make new document with properties {height:792, width:612, ruler origin:{0.0, 0.0}}

   make new ellipse in document 1 with properties {position:{256.0,
   446.0}, width:100.0, height:100.0, fill color:{class:CMYK color
   info, cyan:84.0, magenta:63.0, yellow:0.0, black:0.0}, stroked:false}

   set theText to make new text frame in document 1 with properties
   {kind:point text, contents:"hello", position:{306.0, 396.0}}

   set properties of text of theText to {fill color:{class:CMYK color
   info, cyan:0, magenta:0, yellow:0, black:0}, text font:text font
   "Myriad-Roman", size:32, justification:center}
end tell

Click the Run button (if Illustrator isn't already open, Script Editor will automatically open it for you). You should see the Hello World circle and text as shown in Figure 4.6.

Figure 4.6

Figure 4.6 The output of your Hello World script.

After you've typed the text in Script Editor, click the Compile button (which resembles a hammer). This causes the editor to check the script's syntax to ensure that it's consistent with the AppleScript language. If you get an error message, make sure you typed it in exactly as shown.

Creating a New Page

Take a look at the preceding script and see if you can make some sense of it. The line tell application "Adobe Illustrator" directs the script to Illustrator. It's a way of saying, "Take this note to Illustrator and have it do the things that follow." The last line of the script is end tell which closes what's called the tell block. Because an AppleScript can change which application it's referring to mid-script, application tell blocks provide a means of indicating where the instructions for a particular program are contained.

The second line (activate) brings Illustrator to the front so that you can watch the show.

The third line starts with make new document—which is precisely what the script does. Documents have properties, and this specifies its height and width in pixels (AppleScript's default). Since the Macintosh has 72 pixels per inch, we can see that this creates a page that is 11 inches tall (792 pixels divided by 72 pixels per inch) and 8.5 inches wide (612 pixels divided by 72)—or a letter-size page. The ruler origin {0.0, 0.0} portion of the script sets the horizontal ruler to start in the top left corner and the vertical ruler to start in the lower left corner—important because all commands will use this origin to set or return positioning properties.

Building the Circle

As you've already learned, all objects have properties. In this case, the ellipse, or circle, is drawn using five characteristics, or properties. The first property is its position (256.0, 446.0), which is its x,y coordinates on the page expressed in pixels. (You could substitute inches for pixels by typing 3.5 inches, 6.2 inches.) Our circle will be drawn 256 pixels (3.5 inches) from the left edge of the page and 446 pixels (6.2 inches) down from the top of the page.

The second and third properties we'll use to draw our circle are its width (100.0) and height (100.0). Again, these measurements are expressed in pixels and will result in a circle about 1.4 inches in diameter (100 pixels divided by 72). If we wanted an ellipse, we'd only have to specify two measurements.

The fourth property of our circle is its fill color, which we build by specifying its CMYK mix (though we could also use an RGB mix or even a swatch color).

The fifth and final property is the stroke. Known as a Boolean data type, the stroke can only be one of two values: If its value is "true," our circle will have a stroke; if it's "false," it won't. So stroked:false means no stroke.

There are many other properties you can set, and if you don't specify a property, AppleScript will use Illustrator's defaults. For instance, one of the circle's properties is "fill overprint." Again, it's a Boolean data type, and the default is false. Since the circle is not on top of another object, the default of no overprint is fine—and we don't need to specify it.

Adding the Text

Let's look at the line that starts with set theText. "theText" is a variable name I created so that I could refer to the text by that name in the next line. Variables are nothing more than placeholders—like the x's and y's in your high school algebra class—used to store values that change over time. We use variables all the time in our daily lives—take, for example, the expression "my car." You may have many cars over the years ; "my car" simply serves as the label (or variable name) for the vehicle that's sitting in your driveway right now.

In Figure 4.7, the variable text shows up in green thanks to color-coding (you can change the color in Script Editor's Preferences), which helps you quickly understand a script's structure. In this example I've also used blue to indicate language and application keywords, and black for operators and values.

Figure 4.7

Figure 4.7 The Hello World AppleScript in Script Editor. The events that result from running the script are shown in the lower pane because the Event Log button has been selected. The bar separating the two panes is draggable to adjust the size of or hide either window.

A text frame is Illustrator's scripting term for any text created using the Text tool. The three types (or "kinds") of text frames are point (text anchored by a single point), area (text within an object), and path (text attached to a path). We can see that the three properties specified in this script are kind (point text), contents ("hello"), and position (306.0, 396.0).

We now want to set the fill color, font, size, and justification of the word hello. These are not properties of a text frame but of the text contained within the frame. Thus, in the next line (starting with set properties), we set those properties for the text of theText. This may seem weird, but remember that everything is contained within something else, and certain objects have certain properties. The fill color is set the same way it was for the circle. The font thing is a little interesting because text font:text font looks like a mistake but it's not. The first "text font" is the property and the second "text font" is the class of the value. Size and justification are self-explanatory.

During the early stages of scripting, you'll spend some (OK, a lot) of your time trying to figure out how to address objects (containment) and what properties you can change. This is where using (read: stealing) someone else's scripts (with that person's explicit or implied consent, of course) can come in handy. If you can find a script that does approximately what you want, it's often easier to adapt code that someone else has written than to write your own from scratch. Not only do you save yourself time and grief, it's a great way to learn.

Playing with Your Script

Now it's time to make a copy of your script (copying and pasting it into a new window) and start moving and changing things—in short, play. Some of the most important lessons we learned as children were acquired through play. And playing is a great way to learn scripting, too. For instance, what would happen if you were to change the ellipse bounds? Would its shape change? And what if you were to change the contents of the text frame to, say, your name? Or if you were to change the font and color? What would happen if you were to change the text frame position, add other text frames, or change the "stroked" property to "true"? Try to get into the mode of experimentation. If you screw up, your script won't work, but nothing will catch fire. Just as you would do when editing an image, make a copy so that you can't destroy all your previous work (in this case, typing in all that code).

Here's something cool: In Illustrator select just the circle and then go back to Script Editor and type the following in a new window:

tell application "Adobe Illustrator"
   get properties of selection
end tell

Click the Result button (in the middle at the bottom of the window), and you'll get back a wealth of information about our little circle (Figure 4.8).

Figure 4.8

Figure 4.8 After running this script, the bottom pane will display information that Illustrator records for your circle. If the bottom window is hidden, drag the horizontal bar (with the dot in the center of it) up to reveal the Result text.

You'll get an idea of what Illustrator tracks for every object and what you potentially have access to. Unfortunately, you can't modify the Result window's tangle of text in Script Editor to make it more readable. Still, it's worth taking some time to wade through it, seeing if you can use any of the information there to change the circle. For instance, toward the bottom of the window you'll see that the name property has a value of nothing (name:""). Let's go ahead and name our circle Gaia, which means "living planet." To do that, return to Script Editor, create a new script window, and type the following (with the circle still selected in Illustrator):

tell application "Adobe Illustrator"
   set name of selection to "Gaia"
end tell

Click the Run button in Script Editor, then go to the Layers panel in Illustrator and expand Layer 1 to see Gaia in the lineup (Figure 4.9). Note that Illustrator automatically gave the text frame a name matching its contents—"hello."

Figure 4.9

Figure 4.9 The circle is now named Gaia, as shown in the Layers panel. In future scripts you can refer to it by name.

If you run the "get properties of selection" script again (with the circle selected in Illustrator), you'll see that the value for the name property is now, in fact, Gaia. In future scripts you can refer to our little planet by its name—which means you could write and run something like the following (the circle doesn't need to be selected now since you're referring to it by name):

tell application "Adobe Illustrator"
   set the opacity of path item "Gaia" of document 1 to 50.0
end tell

This sets the circle's transparency to 50 percent. Just to confuse you, in the Illustrator application this property is called transparency. But when scripting Illustrator you have to refer to it as opacity. It seems Adobe used the scripting word transparency already as a GIF property and had to call it something else for path items.

Now, let's change the text as follows:

tell application "Adobe Illustrator"
   set contents of text frame 1 in document 1 to "bye"
end tell

For our last trick, let's do something more interesting. Although I'm sure I'm not the first to discover it, I've found that scripting lets you animate objects in the host program (in our case, Illustrator). The following script will fade the circle Gaia from white (or zero opacity) to full blue (100 percent opacity). You can also make objects fade in or out and move incrementally (as if sliding), and you can incrementally change text (to make it look as if someone were typing it manually). Here's the fade-in script:

tell application "Adobe Illustrator"
   activate
   repeat with i from 0 to 100
      set opacity of path item "Gaia" of document 1 to i
      redraw
   end repeat
end tell

The preceding script introduces the concept of loops, which are useful for controlling the flow of scripts. Loops generally repeat a task until a condition is met, usually changing something incrementally with each pass. In this case, the stop, or end, condition occurs when the variable i equals 100. (For no particular reason, the letter i is commonly used in AppleScript for loops. I could have just as easily used the letter g or z, or a variable name like LoopCounter.) In AppleScript, a loop starts with repeat and ends, appropriately enough, with end repeat. The part that says with i from 0 to 100 means that it will loop 100 times (actually 101 times, since we're starting with 0 instead of 1).

For the first go-round, i has a value of 0; the second time it's 1; and so on. At the end of the set opacity line, we see the variable i again. In our example, i is doing double duty, acting as both a counter for the loop and as the opacity percentage for the circle. If we didn't use variables, we would have to write 101 set opacity lines in the script, which would look something like the following:

set opacity of path item "Gaia" of document 1 to 0
set opacity of path item "Gaia" of document 1 to 1
set opacity of path item "Gaia" of document 1 to 2

and so on up through 100.

If we decided that we wanted to change the progression of opacity and had, in fact, written 101 lines of code, we'd have to go back and rewrite the whole thing. Since the script uses a variable, however, all I would have to do to change it would be to alter the values. For instance, if I wanted the circle to fade out rather than in, all I'd have to do is change one line, which would look like this:

tell application "Adobe Illustrator"
   activate
   repeat with i from 100 to 0 by -1
      set opacity of path item "Gaia" of document 1 to i
      redraw
   end repeat
end tell

You'll notice that I added by –1, which tells the script to count backwards by 1. I didn't have to write by 1 in the first script because AppleScript defaults to the value of 1 when counting loops. If the animation fades out too slowly for your tastes, try setting the count to –2 or –5 and see what happens. Can you make it fade in faster?

  • + Share This
  • 🔖 Save To Your Account