The Cole Papers

Live debugging with AppleScript and Scripter: In this screen shot the script is in the left window; the arrow in the left margin of that window is pointing to the line currently executing. The top right window is a variable watcher. You can observe the values of variables used in a script as they change during execution of each command. The lower right window shows the XPress document and picture box created by this sample script.




AppleScript reduces the time and pain of zoning TV listings

Throughout the 1980s, Macintosh users proclaimed that the graphical user interface (GUI) would be the computing technology of the future. Well, the future is now, and the vast majority of computer users interact with their computers using some form of GUI.

Even as "experts" forecast Apple's impending doom (as they have for 20 years), across the world there is a growing community of Macintosh users who believe that in AppleScript, a user-level system for inter-application scripting, Apple has again developed the computing technology of tomorrow.

We're using it at the Los Angeles Times today.

With AppleScript, I can write a single script that can extract information from a spreadsheet application, format that information, drop it into a new document in a word processing program, change the text style, print the document to a PostScript file, distill the PostScript as a PDF, and upload the PDF to a web site -- all without any help from the user.

The applications don't have to be running on the same machine, and AppleScripts can even run as CGIs (Common Gateway Interface) over the Internet. In fact, there's a virtually unlimited potential for this technology.

AppleScript was introduced shortly after the release of Apple's System 7 in the early '90s, and since then has been known to script writers as Apple's Best Kept Secret. Although initially the technology was not well marketed or supported, it has gradually built a significant market, particularly in production environments.

Herewith is an overview of the AppleScript language and technology, followed by a case study in using AppleScript to facilitate pagination by automating workflow in one department of a major metropolitan newspaper.

AppleScript is based on AppleEvent technology. Applications on the Macintosh use AppleEvents internally to pass messages, move data or execute commands within a program.

AppleScript exploits this internal control structure to manipulate applications externally. A script, running as a stand-alone application or from within another application, can send commands, data and AppleEvents to objects within other applications.

One of the biggest strengths of AppleScript is the scripting language itself, which has an English-like syntax and employs a limited number of programming verbs. For example, a command that would direct Quark XPress to create a document would use this syntax:

tell application "QuarkXPress"
    make new document at beginning
end tell

In this script, "make" is a verb and "document" is a noun. The following snippet uses the same verb ("make"), with a different noun ("picture box"):

tell application "QuarkXPress"
  tell document 1
    make new picture box at the
      beginning of page 1
    tell picture box 1
      set the bounds to {"86.5 pt",
        "91 pt", "264 pt", "362 pt"}
    end tell
  end tell
end tell

These simple examples illustrate the fundamental logic and structure of the language.

Both commands are sent to the application within a "tell" statement. In the second example, a nested or layered structure is used. Every line between the first "tell" statement and the corresponding "end tell" is sent to the application. The lines within the second "tell" statement are directed to a specific object within the application, in this case the front-most document.

The "make new picture box at the beginning" command also specifies the layer in which the new box should be created ("beginning" refers to the top layer of an XPress document). The final "tell" statement is directed to the object just created and uses the verb "set" to specify the box's "bounds" (boundaries), its size and position on the page.

One clear advantage of the English-like syntax and the verb-object structure is that script writers are not forced to memorize a large number of commands. For example, the "Print" verb, which is the same in nearly every scriptable application, can be used with a reference to a single page, a range of pages or an entire document, as in this example:

tell application "QuarkXPress"
  set myDocument to the object reference
      of document 1
  tell myDocument
    set mySpread to the object reference of
      spread 1
    set myPage to the object reference of
      page 1
    set objectList to {myDocument,
      mySpread, myPage}
    if page 2 exists then
      set the end of objectList to the
        object reference of page 2
    end if
    repeat with thisObject in objectList
      print thisObject copies 2
    end repeat
  end tell
end tell

Reading the example above, it's easy to follow the logic:

The first line directs the commands that fall between the "tell" statements to XPress. The second line creates a variable named myDocument and sets its value to a reference to the document. The third directs all further commands to the document referenced in "myDocument".

The next two lines set the value of variables mySpread and myPage to references to those objects. Once all the variables are set they are combined into a list of variables designated with French braces -- {}.

In the next line the script determines whether the document has a second page, and if it does, adds that to the list of objects to be printed.

Finally, the script enters a repeat loop. The first time the loop is executed the variable thisObject is the first item of the list (in this case, a reference to the XPress document). The Print verb prints two copies of the first document, then the repeat loop is executed a second time. In this execution, the variable thisObject is a reference to the first spread of the front-most document.

Meeting the multiple-zone challenge
Each week, we produce 26 zoned editions of TV Times, our television listings magazine which is delivered to subscribers to the Sunday Los Angeles Times -- one of the nation's largest Sunday papers, at 1.3 million copies.

TV Times is produced in XPress on the Mac. Our listing information is generated at the data center of Tribune Media Services in Glens Falls, N.Y., and transmitted as raw text files with embedded style sheets.

Each edition's listings are geared to the cable TV channel lineup available in that edition's area. Listings in each edition are carefully edited and formatted to fit the page, and meet high quality standards. Each zoned edition also contains display ads which can be sold in specific zones or for every edition.

When the Times began a concerted effort to paginate the entire publication, several departments adopted different strategies and software packages. A system based largely on a WinTel platform was considered for TV Times, but although the proposed system was "paginated," the process was not automated. Going this route would have required considerable additional training while providing little or no labor-saving or cost benefits.

Since our production is MacOS-based, we turned to AppleScript, which enabled us to take great strides in automating production. The TV Times production staff uses AppleScript to build documents, reserve ad space, import editorial listings and features, manage printing and reprinting, import ads, and even error-check each document, providing a high level of quality assurance.

The first step in employing this technology in our production process was to build a system for pagination. "The Paginator" is a Hypercard stack -- written in-house -- that uses AppleScripts at every step.

Production begins when our edition planning department, using Layout/8000 on a DOS machine, imports ad reservations from the Neasi-Weber Admarc system on the company's mainframe and generates files of ad reservation data for each edition.

The Paginator imports the edition design and ad reservation data from the DOS system, then builds the nine documents used for each zoned edition. Occupying separate XPress documents are the Sunday-through-Saturday listings, movies and sports.

Calling in the ads
Once the documents have been laid out they're turned over to ad operations, where another AppleScript links the reserved ad space to the digital ad images (EPS or TIF files). These scripts are "droplets" -- stand-alone Scripts that execute when the ad coordinator selects the XPress documents in the Finder and drops them onto the script's icon.

The AppleScript opens each document, imports the ads, saves the documents and generates a report alerting the ad coordinator about any missing ads.

Flexibility is one advantage realized in using AppleScript over competing systems. The ad space reservation deadline falls about a week before production begins, while the deadline for ad art falls during production. With AppleScript, we are able to import the ad images into the documents as they become available. Often one or two ads miss the deadline, but will appear in all 26 editions. These ads can be quickly imported without disturbing the ads imported days earlier.

In addition, our ad and listing coordinators have been freed from the tedious and error-prone tasks of manually laying out the documents and importing ads, allowing them to take on other tasks and giving the Times the ability to add zoned editions without having to add resources.

After the ads have been imported and positioned, the documents are turned over to the editorial department, where AppleScripts are used extensively to speed production.

Error prevention is an integral feature
Listings used in TV Times are generated by Tribune Media Services and transferred by modem using AppleTalk Remote Access. AppleScript is used to download, unstuff and distribute the data to the appropriate folders on the Mac. An editorial listing coordinator then runs an AppleScript that imports the listings into the XPress documents.

Built into these steps are numerous error-checking and file verification procedures that occur automatically. Before AppleScript automation, the listing coordinators would open each XPress document and navigate to the correct file. With 26 editions of nine documents each, even a one percent error rate could not be tolerated. With AppleScript we're able to automatically compare the name of the document with the ID slugs in the data to give us an error rate that is virtually zero.

Next, the listing coordinators edit the documents. There are several AppleScripts employed during editing to speed the process. For example, one script scans the text in the document and deletes any listings for infomercials. Used when listings run longer than normal, this step saves as much as two columns of space for other listings.

After editing, an AppleScript droplet is used to error-check the documents. This script ensures that the correct dates and zone information are in the page folios and tabs, verifies that the correct editorial features are linked to the document, and checks that the correct listings have been imported into the document.

This step has eliminated the most common errors made during production.

Currently, our documents are printed to high-resolution imagesetters, camera-ready pages are filmed and images are burned onto plates for offset printing.

But that's changing. We are phasing in a computer-to-plate system -- our printer, Treasure Chest Advertising in the City of Industry, Calif., is using the Creo system -- and again we're relying heavily on AppleScript to automate the workflow.

We distill our documents using Adobe's PDF (Portable Document File) format and transfer the files to the printer using a dedicated T1 line. We will also make a PDF version of each edition available on our web site. We expect the final result to save significant production time, allowing us to produce more timely and current listings.

What's most remarkable about the computer-to-plate process is the impact AppleScript has in a cross-platform production environment. AppleScripts facilitate the flow of documents through the production phase at every step.

After editing, the XPress documents are printed by a script as individual pages to a UNIX spooler, where a PostScript file is generated. An AppleScript transfers the PostScript from the UNIX to the Mac, where another AppleScript sends the PostScript file to an Acrobat distiller and saves the PDF in the correct folder for each edition. (Eventually we plan to distill on the UNIX box.)

The PDF files are proofed, and after corrections or updates have been made, AppleScript is used to combine individual PDFs into a single PDF document for each edition -- which is then delivered to the printer.

Building on the success we've had with AppleScript, our commitment to this technology is growing stronger -- and at the same time the number of scriptable applications in release increases daily. The Paginator was written under a short deadline when the technology was new and virtually untested. Today, the technology has proven itself in many environments, and is gaining wider acceptance in the market as more and more developers are supporting this scriptability.

Despite all the turmoil and upheaval at Apple over the last few years, AppleScript is evidence that Apple remains an innovative and influential mover in the computer industry.

AppleScript has become an indispensable tool in our production process, and that is only a hint of the full potential of this technology.

-- Ed Stockly

Apple Computer Inc.,
(408) 862-5185;
Tribune Media Services,
(312) 222-4444; e-mail: tmc@tribune.com.

From THE COLE PAPERS, October 1997, Copyright © 1997, All Rights Reserved.

Top | ColeGroup.com | Consulting | Cole Papers | NewsInc. | Cole's Store | Miscellanea | Search
Copyright © 1990-2009, The Cole Group. All Rights Reserved. Contact us.
Modified date: 10/07/1997, 02:42:50 AM.
URL: http://www.colepapers.net/TCP.archive/Cole_Papers_97/TCP_97_10/tvlistings.HTML