![]() |
| |
![]() |
||
|
![]() |
|||||||||||||||||||||||||||||
|
Welcome back to another episode of "The AppleScript Foundry!" Every month in the AppleScript Foundry, I'll be sharing what I know about scripting. Since the object of this column is to get people who are new to scripting up and running, I will take a hands on approach, explaining new terms along the way. However, it is not my goal to talk down to the reader - If you want harder stuff, just write me! You can reach me at kevin@macosjournal.com.
Quark Scripting As promised, this month we will look at scripting one of the most often used Mac applications, Quark Xpress. I worked for a newspaper for a while, and every year they polled the readers to select the best businesses in several categories ("Best Restaurant," "Best Nightclub," etc.). Each category had first, second, and third place winners, and in some cases there were 2 or 3 way ties! This made for a lot of certificates that had to be produced quickly. Enter my friend, AppleScript! With over 200 awards to be made, this was NOT a task I was going to tackle without help. It actually took longer to print the pages (in full color, of course) than it did to create them using a script. The Production Manager sent me a file with the names of the winners, and with just a little work, I edited the text file to look like this: Dining Categories Entertainment Categories eof The file was saved as a simple text file (like you get from SimpleText or BBEdit) named "Awards file." This is the "database" that will drive our script. Note the last line, that reads, "eof." This is our "end of file" marker to halt processing of the file. Anything placed after it will be ignored. Next, create a Quark page for each category using a background of your choosing. I used a simple TIFF background and a static text box describing the category ("Best Lobster," for example). Then mark each one with a text box as "First Place," "Second Place," or "Third Place" and save them with the names "first," "second," and "third" in a directory named for the category it falls under. Here is an example using the two categories above:
This is all the prep work that was needed. Now, for writing the script! Here is a quick sketch of what we want to do: Initialize
any variables This is the basic logic of the program. Now let's code it. First, let's set some constants. This makes it easy to adapt the program to other input files: -- Initialize variables
Next, let's have the user select the award text file and the main folder where the files are located: set award_text
to choose file
of type "TEXT" with prompt "Where is
the award file?" set file_num to open for access award_text "Open for access" is from the Standard Additions dictionary, as are "choose file" and "choose folder." "Open for access" opens a file and returns a file reference number that we will use later to read records from the file and to close the file when we are done with it. The "choose" commands allow the user to select files or folders for processing. At this point, we can begin looping through the file, reading records and creating certificates. We will use a "repeat" loop for the processing. repeat until
done To prevent errors from crashing our script which would leave our file open, we will put the contents of the repeat loop inside a "try" statement. If there is an error, we close the file and alert the user. We look for lines that are longer than 2 characters to allow for blank lines in the input file. Then we save the input in a variable and check to see if we are at the end of the file. if (last word of input_line is equal to category_flag) thenset cat_folder to (award_folder & (word 1 of input_line)) -- Set cat_folder to the folder containing our -- certificate templates for this category First we look for a category and build a path to the template files. elseif (first word of input_line is equal to award_flag) then set award_msg to input_line -- The input line is the award text Then we look for the award message, like "Best Hamburger." elseif ((number of words of input_line) is greater than 1) and  (second word of input_line is "place") and  (place_flag contains (first word of input_line)) then set place_file to first word of input_line -- The input line tells us which template to use Place_file is a list that contains "first," "second," and "third." If the first word of the input line is contained in place_file, then we select the correct template. elseset the_file to ((cat_folder as text) & ":" & place_file) -- Create the full path to our template file tell application "QuarkXPressª" activate open file the_file use doc prefs yes -- open the template Now we open the template and begin telling Quark what to do. If you remember, we can open Quark's dictionary in Script Editor and find out what sorts of things it knows how to do. Here are the entries we are using: open:
Open the specified object(s). make:
Make a new element save:
Save an object. close:
Close an object That's correct! Other than "set," we are only using four commands unique to Quark Xpress. The rest of our script is either plain AppleScript or Standard Addition commands. Here is where we hand Quark the text and format it: tell
front document -- Now create a text box for the recipient's name make
new text box at the beginning with
properties  We use the phrase "at the beginning" with the "make" statement because we have to tell the document where to create objects. "At the beginning" is basically a place holder. We are almost done. Now we have to save the new document and close it. Here is the remainder of our script: copy
award_msg & " " & place_file
& " " & input_line to
output_file if length
of output_file
is greater than 31 then
set output_file to
characters 1 thru
31 of output_file
close access file_num If you want to take a closer look, I have created a downloadable file that contains the script, the award file, and the template files. The file is in StuffIt format in the Public folder of my iDisk at Mac.com. Go to Apple's iTools page and logon to my iDisk with the username kevinbradley. The file is 1.5 mb, so depending on your connection it may take a while to download. Also, for more help, you can check out Rob Vanderwerf's "Learn AppleScript for Publishing and Prepress" web site. He has tutorials and examples and he was a big help when I was creating this script. Here is a list of places you can go to get more info on AppleScript:
|
||||||||||||||||||||||||||||||
|