CGI and AppleScript

Within an AppleScript CGI, you can process data or instructions entered into a form. You can initiate a database search, assemble text, produce charts or graphics, all driven from the user’s choices. Or you can send an e-mail, assemble new HTML for subsequent Web pages or deal with runtime errors.

A Sample AppleScript CGI

Suppose you maintain a regularly-updated database of information in a database that includes a table of numeric values, and you want to provide, on demand to your Web users, the table in the form of a chart. The following Classic Mac OS script uses three familiar applications: FileMaker Pro, to hold the data, DeltaGraph Pro, to make a chart of the data, and Clip2Gif, to store the chart in a GIF file. You can use redirection, provided by the Location URL in reply_header, to point to the Web server to the GIF file. The script is intended as inspiration, not as a working sample.

property crlf : (ASCII character 13) & (ASCII character 10)

property reply_header : "HTTP/1.0 302 FOUND" & crlf & ¬

  "Server: WebSTAR/1.0 ID/ACGI" & crlf & ¬

  "Location:" & crlf & ¬

  "URI:" & crlf & crlf

property error_header : "HTTP/1.0 200 OK" & crlf & ¬

  "Server: WebSTAR/1.0 ID/ACGI" & crlf & ¬

  "MIME-Version: 1.0" & crlf & "Content-type: text/html" & crlf & crlf

-- CGI Handler

on «event WWWΩsdoc» path_args ¬

  given «class post»:post_args, «class meth»:method, «class addr»:client_address


    -- Body of CGI goes here.

    -- Interpret CGI arguments here.

    -- Obtain user’s choices from the CGI arguments.

  -- retrieves data from database

  tell application "FileMaker Pro"

    Open alias "Macintosh HD:Databases folder:Web DataBase"

    set numRecs to Count of Record in Document 1

    -- make tab-delimited data for Deltagraph

    set dataString to "Month" & tab & "Amount" & return

    repeat with i from 1 to numRecs

      copy dataString & Cell "month" of Record i & tab & ¬

      Cell "amount" of Record i & return to dataString

    end repeat

  end tell

  -- creates desired chart

  tell application "Deltagraph Pro"

    Data dataString

    Plot Options Text Font "Palatino" Text Size 12 Colorstyle "blue"

    Set Axis Lengths for X 100 for Y 100

    Output PICT

    set dataChart to Plot chart chartType

  end tell

  -- makes GIF file for the redirect

  tell application "clip2gif"

    save dataChart as GIF in file "Macintosh HD:home.html"

  end tell

  -- returns reply data to the server and exits CGI handler

  return reply_header -- reply and exit

  -- creates page of HTML text in event of error

  on error errNum number errMsg

  set return_page to error_header &¬

    "<HTML><HEAD><TITLE>Error Page</TITLE></HEAD>" &¬

    "<BODY><H1>Error Encountered!</H1>" & return &¬

    "An error was encountered while trying to run this script." & return

  set return_page to return_page &¬

    "<H3>Error Message</H3>" & return & errMsg & return &¬

    "<H3>Error Number</H3>" & return & errNum & return &¬

    "<H3>Date</H3>" & return & (current date) & return

  set return_page to return_page & ¬

    "<HR>Please notify the webmaster at " & ¬

    "<A HREF=\"\"></A>" &

    " of this error." & "</BODY></HTML>"

  return return_page

  end try

end «event WWWΩsdoc»

Excerpt from CGI and AppleScript, Dr. Dobb’s Sourcebook, Nov/Dec, 1995.