MACROMEDIA FREEHAND FOR IMAGES

While FreeHand is not completely scriptable, it nonetheless offers some very interesting capabilities for working with existing files via AppleScript.

In this chapter, we'll experiment with changing the attributes of existing graphic objects in a FreeHand document. We'll also look at creating and exporting graphical text, as well as exporting selected layers from a file. By exporting layers in sequence, you can generate many files with variations or element builds, where you toggle layers on and off and export bitmaps of these different combinations of visible layers. Finally, we'll look at using FreeHand's AppleScript support to batch process files into different file types.

Scripting FreeHand

Macromedia's FreeHand gained limited AppleScript support in version 7, and this remains the same in FreeHand 8. FreeHand's dictionary is unorthodox, incorporating nouns and verbs into single statements, which makes understanding its syntax more difficult than one might hope.

The focus of FreeHand's dictionary is concentrated in limited object and layer manipulation along with extensive file importing and exporting support. What does this mean for you? Don't expect to create documents from scratch with your scripts. Most useful FreeHand scripting revolves around manipulating existing documents and saving or exporting the results of your changes.

FreeHand scripting revolves around manipulating existing documents and saving or exporting the results of your changes.You'll need to make some preparations before tackling the scripts discussed in this chapter; these are outlined in the steps below.

To get ready for the following AppleScripts for FreeHand:

  1. Launch FreeHand on your computer.

  2. Choose New from the File menu to create a new document.
  3. Set your new document's default units to points.

  4. Draw a single rectangle and set its attributes in the Inspector palette to: x=10, y=10, w=350, h=50, and corner radius=0. Figure w3.1 shows what your Inspector palette should look like with the rectangle selected.

  5. Zoom to 100% using the key combination Command-1. Figure w3.2 shows what your document window should look like. Save your sample document where you can easily access it later.

We're ready to proceed with our scripts!



Fig. w3.1  The Inspector palette showing the attributes for the selected rectangle object
 


Fig. w3.2  The finished sample document in FreeHand, ready to be scripted

Scripting FreeHand

All the scripts that follow were designed and tested for FreeHand 8. FreeHand is published by Macromedia, Inc., who can reached at http://www.macromedia.com/.

Changing line weights in a document

FreeHand's support for object manipulation from AppleScript is limited. Its dictionary has two very specific commands for changing existing objects: ChangeFont and Change LineWidth. These commands have a very straightforward, if unorthodox, syntax that allows you to selectively change the stroke weights and typefaces for selected objects in a document.

We'll begin by modifying the line weight of all of the objects in our sample document (which in this case has only a single rectangle in it) in a repeat loop. This script (Code w3.1) loops 20 times, increasing the line weight by one each time and exporting each iteration to a JPEG file. These files could then be used to create an animation sequence. Make sure the sample document is open in FreeHand before running this script in the Script Editor. Figure w3.3 shows some examples of versions of our document created by the loop. Figure w3.4 shows FreeHand's dictionary entry for the Select LineWidth command.

To change the line weight of existing objects in a document:

  1. set myFolder to (choose folder "Select folder for saved images:")

    First we prompt the user to select a folder to save our exported JPEG images into, and save a reference to that folder in the variable myFolder.

  2. repeat with myWeight from 1 to 20

    We begin our repeat loop running from 1 to 20 with values stored in myWeight.

  3. Change LineWidth myWeight Min 0 Max 20 Search document

    Here we instruct FreeHand to search the entire current document for all objects with line weights between 0 and 20 points and change those objects' weights to the current value of myWeight.

  4. save in file (myFolder & myWeight & ".jpg" as text) as JPEG

    Now we're ready to have FreeHand save our current document as a JPEG in the folder we selected with a file name corresponding the line weight.

Code w3.1. This script sequentially changes the line weight of every object in a document from 1 point to 20 points, exporting each iteration as a JPEG file.

set myFolder to (choose folder 
"Select folder for saved images:")
tell application "FreeHand 8"
activate
repeat with myWeight from 1 to 20
Change LineWidth myWeight
Min 0 Max 20 Search document
save in file (myFolder &
myWeight & ".jpg" as text)
as JPEG
end repeat
end tell



Fig. w3.3  Some examples of our FreeHand sample document over the course of the script's run, showing the increasing thickness of the lines


Figure w3.4. FreeHand's dictionary entry for the command Select LineWidth.

Duplicating, modifying, and selecting specific objects

FreeHand allows you to be very discerning in the objects you'd like to select and modify using commands like ChangeFont and Change LineWidth as well as Scale and Rotate. It does this with a powerful Select statement. You can use the Select statement not only for modification, but also to selectively export elements from a document to a file with the SelectedOnly modifier to the save command.

In Code w3.2, we'll play with this ability by selecting only the original rectangle from our sample file out of all the other objects in the document. How, you might ask, are we going to get those other objects into this document? We'll use the limited scripting ability available in FreeHand to mass-produce a family of other rectangles with different sizes and line weights first. Make sure the sample file is open in FreeHand before running this script from the Script Editor. Figure w3.5 shows the results of this script. Figure w3.6 shows FreeHand's dictionary entry for Select.

To duplicate, modify, and select specific objects in a document:

  1. Select itsType Rectangle

    First, we select all rectangles, which in this case will be only our one original object.

  2. repeat with i from 1 to 10

    Now, we'll begin a repeat loop in which we'll rectangle, then scale it, rotate it, and change its line weight.

  3. Clone Selection

    With our rectangle as the current selection, we now have FreeHand duplicate it right on top of itself using the Clone Selection statement.

  4. set sizeX to random number from 20 to 200
    set sizeY to random number from 20 to 200
    Scale XScale sizeX YScale sizeY Search Selection

    Our cloned object will now be our current selection, so we'll set up two variables with random numbers between 20 and 200 and then send FreeHand a Scale command to modify our newly created rectangle to a random size.

  5. set myRotation to random number from 10 to 350
    Rotate myRotation Search Selection

    Our cloned object will still be selected after the Scale command, so now we'll set a variable with a random number from 10 to 350 and use it with the Rotate command to spin our object around.

  6. set myWeight to random number from 2 to 10
    Change LineWidth myWeight Search Selection

    Next, we'll put a random number from 2 to 10 into a variable and use it to change the line weight of our new object.

  7. Select LineWidth 1 Search document

    Lastly, we reselect our original rectangle, which we know will be the only object with a line weight of 1, and repeat.

Code w3.2. This script repeatedly selects and clones an existing rectangle, scaling and rotating the new object.

tell application "FreeHand 8"
activate
Select itsType Rectangle
repeat with i from 1 to 10
Clone Selection
set sizeX to random
number from 20 to 200
set sizeY to random
number from 20 to 200
Scale XScale sizeX Y
Scale sizeY Search Selection
set myRotation to random
number from 10 to 350
Rotate myRotation
Search Selection
set myWeight to random
number from 2 to 10
Change LineWidth myWeight
Search Selection
Select LineWidth 1
Search document
end repeat
end tell



Fig. w3.5  This sample document window is filled with different rectangles; our original rectangle (it has a one-point line weight) is selected.
 

Fig. w3.6  FreeHand's dictionary entry for the Select command

Creating and exporting graphical text in FreeHand

As stated earlier in this chapter, original object creation is not the strong point of FreeHand's AppleScript support. However, where there is a will there is a way. One of the tasks you're most likely to want to script in FreeHand is mass-producing graphical text for use on the Web or in other applications. There is a way to do this in FreeHand via AppleScript, but it has limitations. The greatest limitation of all is that you can only create black text. If that's OK for you, then read on!

Code w3.3 shows a sneaky way to use AppleScript to get FreeHand to systematically set type and export it as a nicely antialiased GIF file. Open the sample file you prepared earlier before you run this script in the Script Editor. Figure w3.7 shows a FreeHand document with a text object generated by this script.

To create and export graphical text:

Note: Mac OS 8.5 or scripting addition required!

This script requires Mac OS 8.5 or the scripting addition Jon's Commands. (See sidebar.)

  1. set myTitleList to {"Home", "About Me", "Scripting", "Playing"}

    First, we define the separate strings that we want to make into graphic text objects in FreeHand as items in the list variable myTitleList.

  2. set myFolder to (choose folder "Select folder for saved images:")

    Next, we prompt the user for a folder to save the produced GIF files in and store a reference to the folder in myFolder.

  3. repeat with myTitle in myTitleList

    We begin a repeat loop to pass through every item in myTitleList and store each in the variable myTitle.

  4. tell application "Finder"
    activate
    set the clipboard to myTitle
    end tell

    Now we set the clipboard with the text stored in myTitle. Since the clipboard is only updated when applications are switched, we need to execute the set the clipboard command in a program other than FreeHand so that FreeHand will update its clipboard after each time we put data into the clipboard.

  5. Select

    After activating FreeHand we'll have it select every object in the document (in this case, only our rectangle). We do this because FreeHand fails to paste objects properly if nothing is selected when the paste occurs.

  6. Paste top 40 left 20 bottom 20 right 350 Units Points

    Now we're ready to paste the text from the clipboard into our document, positioning it right inside of the rectangle already there.

  7. save in file (myFolder & myTitle & ".gif" as text) as GIF

    With our document complete, we'll save it as a GIF file to the folder referenced in myFolder.

  8. Select itsType AnyText
    Delete Selection

    Finally, we'll delete our text object by selecting it, then issuing a Delete Selection command.

Exporting selected layers from a document

One of FreeHand's really useful scripting capabilities lets you selectively hide and show a document's layers. Why would you do this? Say you have a complicated multilayered document with a base layer of graphics and other layers on top. With AppleScript, you could automate the creation of an infinite variety of files by selectively turning layers on and off before exporting.

Code w3.4 creates a number of files by turning layers in an existing multilayer document on and off before exporting. Figure w3.8 shows a folder containing files generated by this script.You'll need to open a multilayer document in FreeHand and set the variable myLayers to the appropriate number of layers before running this script.

To export selected layers from a document:

  1. set myLayers to 5

    We begin by setting the variable myLayers to the number of layers in the document our script will work with. Set this variable appropriately for the document you are using.

  2. repeat with k from 3 to myLayers+2
    Set LayerInfo Layer k without Visible
    end repeat
    Update

    Now we turn off all layers from 3 to the number defined in myLayers in a repeat loop and then refresh the screen with Update. We start with 3 because the background and guide layers normally occupy layer positions 1 and 2. By refreshing the screen, we make sure that only the layer we have selected is visible.

  3. repeat with i from 3 to myLayers+2

    We're ready to repeat through each layer.

  4. Set LayerInfo Layer i with Visible
    Update

    Next, we turn on only the current layer in the loop and refresh the screen.

  5. save in file (myFolder & i as text) as PICT
    Set LayerInfo Layer i without Visible
    end repeat
    end tell

    Finally, we save the currently visible layer as a PICT file, hide it again, and continue the repeat loop.

Code w3.4. This script toggles on each of five layers sequentially, exporting each to a separate file.

set myFolder to (choose 
folder "Select folder for
saved images:")
tell application "FreeHand 8"
activate
set myLayers to 5
repeat with k from 3 to
myLayers + 2
Set LayerInfo Layer k
without Visible
end repeat
Update
repeat with i from 3 to
myLayers + 2
Set LayerInfo Layer i
with Visible
Update
save in file (myFolder &
i as text) as PICT
Set LayerInfo Layer i
without Visible
end repeat
end tell



Fig. w3.8  A folder full of files generated by the script.

Batch converting files with FreeHand

Another of FreeHand's really useful scripting capabilities allows us to open and export many different image file types. As of this writing, FreeHand can open and export the following file types:

The script shown in Code w3.5 opens the files in a user-selected folder with FreeHand and saves them in Photoshop EPS format.

To batch convert files in FreeHand:

  1. set myFolder to (choose folder "Select folder for processing:")

    First we prompt the user for a folder to open and store a reference to the folder in myFolder.

  2. set myFolderList to list folder myFolder without invisibles

    Next, we gather a list of the files contained in the folder and store it in myFolderList.

  3. tell application "FreeHand 8"
    activate

    Now we're ready to talk to FreeHand. First, we bring it to the front.

  4. repeat with myFile in myFolderList

    Our repeat loop loads each item in the list of files stored in myFolderList into the variable myFile.

  5. open (myFolder & myFile as text)

    Now we open the current file in FreeHand.

  6. save in file (myFolder & myFile & ".eps" as text) as PhotoshopEPS

    Next, we save the file as a new file type, in this case a Photoshop EPS, in the same folder but with a new suffix.

  7. close saving no

    Then we close the file without saving it and continue our repeat loop.

  8. end repeat
    end tell

Code w3.5. This script traverses a folder full of files, creating another copy of each file in a new format, thanks to FreeHand.

set myFolder to (choose 
folder "Select folder for
processing:")
set myFolderList to list
folder myFolder without invisibles
tell application "FreeHand 8"
activate
repeat with myFile
in myFolderList
open (myFolder &
myFile as text)
save in file (myFolder &
myFile & ".eps" as text)
as PhotoshopEPS
close saving no
end repeat
end tell



Fig. w3.9  FreeHand's Output Options dialog window showing settings that help prevent errors during script execution.

Avoiding errors when trying to export from a script

FreeHand will generate Type 2 errors when using save in file from AppleScript under certain circumstances. If you encounter these errors, make sure the document's output options are set to match those shown in Figure w3.9. By using these output options, you may avoid Type 2 errors.