Apple Logo Itsamac Hosting
Mac OS Journal
EditorialsColumnsFeaturesReviewsArchives/StaffSubscribe
 
Table of Contents From the Desktop Connect Feature Column Special The CoXFiles The Gaming Landscape The Surf Report The Database Guru
The AppleScript Foundry Medicine Man Shop Talk Review - LiveMotion Review - Kawasaki ATV Powersport Review - Mixman Pro Review - Freeway Review - iMediaKey Behind the Scenes
   
 
Itsamac.com
Red Light Runner
Applelust.com
     

The AppleScript Foundry
August 2000 || Volume 01, Issue 01

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
Best Lobster
First place
Joe's Diner
Mary's Restaurant
Second Place
Mom's Truck Stop
Third place Chéz Maurice
(Then Best Restaurant, etc.)

Entertainment Categories
Best Theater
First Place
MacWorld Expo Keynote
Second Place
World Wide Developer Conference
Third Place
The Microsoft Trial
(then Best Comedy, etc.)

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:

Award Files

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
      Open the award file
      read a record from the file If it is "eof" then we are done!
            If it is a category record, select the correct category folder
            If it is an award name (Best Whatever), set the award message
            If it is a place (first, second or third), select the correct file template
            If it is none of the above, it must be the name of the recipient of the award.
                  Open the template file and create the document.
                  Save it
                  Close the template
      Loop back to "read a record"

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
set category_flag to "categories"
set award_flag to "best"
set place_flag to {"first", "second", "third"}
set input_line to "blank"
set place_file to "blank"
set done to false

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 award_folder to choose folder with prompt "Where is main award folder?"

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
  try
    read file_num as text before return
  --read award_text as text until a carriage return
  if ((the number of characters of the result) is greater than 2) then
    set input_line to the result
    if word 1 of the input_line is not "eof" then
      -- Look for the end of file marker

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) then
          set 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.

        else
          if (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."

          else
            if
((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.

            else
              
set 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).
   open
reference -- List of objects to open.
    [use doc prefs yes/no/ask] -- Specifies whether document preferences should be used (as opposed to Application preferences).
    [remap fonts no/ask] -- Specifies whether or not to remap any fonts which were used in the document but which don't exist in the current system.
    [do auto picture import yes/no/ask] -- Specifies whether or not to re-import any missing pictures.

make: Make a new element
  make
    new type class -- the class of the new element
    [at location reference] -- the location at which to insert the element
    
[with data anything] -- the initial data for the element
    [with properties record] -- the initial values for the properties of the element
Result: reference -- to the new object(s)

save: Save an object.
  save reference -- The object to save.
    [in alias] -- The file in which to save the object.
    [as type class] -- The file type of the document in which to save the data.
    [template boolean] -- If TRUE, save this document as a template.
    [include preview boolean] -- If TRUE, include a picture preview with this document.
    [EPS format Mac black and white/Mac Color/Mac DCS/Mac DCS2/PC black and white/PC Color/PC DCS/PC DCS2] -- The type of format to use for the Save Page command.
    [EPS data ASCII EPS/binary EPS] -- Whether to save the EPS data in ASCII or Binary format for the Save Page command.
    [OPI omit TIFF/omit TIFF and EPS/include images] -- Whether to omit nothing, TIFF only or TIFF & EPS images for the Save Page command.

close: Close an object
  close
reference -- the object to close
    [saving yes/no/ask] -- specifies whether changes should be saved before closing
    [saving in alias] -- the file in which to save the 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
              -- Create the text box and add the Award name

              make new text box at the beginning with properties Â
{bounds:{"2.924\"", "4.222\"", "4.313\"", "9.803\""}, name:"T1"}
              tell text box "T1" set story 1 to award_msg
                set the color to "none"
                set the size of paragraph 1 to 48
                set the font of paragraph 1 to "Apple Chancery"
                set the leading of paragraph 1 to 47
                set justification of story 1 to centered
              end tell

                -- Now create a text box for the recipient's name

                make new text box at the beginning with properties Â
{bounds:{"5.285\"", "4.222\"", "6.674\"", "9.803\""}, name:"T2"}
              tell text box "T2"
                set story 1 to input_line
                set the color to "none"
                set the size of paragraph 1 to 48
                set the font of paragraph 1 to "Apple Chancery"
                set the leading of paragraph 1 to 47
                set justification of story 1 to centered
              end tell
            end tell

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
            -- Create a unique name for the output file that includes
              -- the award, place, and recipient

if length of output_file is greater than 31 then set output_file to characters 1 thru 31 of output_file
          -- Trim the file name if it is too long
            save front document in ((cat_folder as text) & ":" & output_file)
            close front document saving no
          end tell
        end if
      end if
    end if
  else
    set done to true
    -- Let's get out of this loop!
  end if
end if
  on error error_msg
    close access file_num
    display dialog error_msg
    -- If something goes wrong, close the input file and tell the user what happened.
  end try
end repeat

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:

Kevin's Icon Kevin Bradley - kevin@macosjournal.com
Kevin's Page - Feedback Form

back Mac OS Journal forward
 
 
   
© 2000 - 2004, MacOSJournal.com. All rights reserved. No part of this publication may be reproduced in any way without prior, expressed permission from the Publisher. It is the sole property of MacOSJournal.com and its writers, who retain copyright to their own works. If you wish to link to us, please see our Privacy Statement for conditions. Apple, Macintosh, and Mac are trademarks of Apple Computer, Inc, with whom we are in no way affiliated or endorsed.
Hosting provided by itsamac.com -- Macintosh Powered Web Hosting
Serve Different