Writing Applescript CGIs

CGI stands for Common Gateway Interface.

CGI in a nutshell - click the submit button on a web page with a form, and the browser passes the form data to the server, which in turn passes the form data to a program called a CGI. The CGI processes the data and returns a complete HTTP response (HTTP headers + HTML) to the server, which passes this response to the browser, which displays the HTML just as if it was a static page.

The following discussion assumes you have some familiarity with HTML and Applescript. If you have never created a web page using raw HTML, and have never written an Applescript, you may be confused. Learn those other skills first. Also, some experience with Web serving is very helpful, even if it is just Apple's Personal Web Sharing (MacOS 8/9).

CGIs do not work correctly with Applescript 1.6. Use Applescript 1.8.3 instead (available thru your SoftWare Update Control Panel).
Applescript CGIs do not work with Apache/MacOS X. James Sentman is working to provide a fix http://www.sentman.com/acgi/; please encourage and support him in his efforts.
WebSTAR V has Apple Event CGI support; however, it appears to be deficient. The applet must be running OR the OSX machine running W*V must be logged in as user "webstar". I consider these to be wrong behaviors, but I have not tested the latest W*V.

  1. Create a plain text file with the following content. Save it in the Web Pages folder (in the Documents folder if using Mac OS 9.1) with the name [spitback2.html].
    <html> <head> <title> Handle CGI Example </title> </head> <body> <h1>Handle CGI Example</h1> <form method="post" name="test" action="spitback2.acgi"> <input type="text" name="texttest" size="52" value="This is a text field."><br><br> <textarea name="area" cols="48" rows="4" wrap="soft">This is a textarea.</textarea><br><br> <table cellspacing=2 border=1><tr> <td>Check me, I'm fun! <input type="checkbox" name="checktest" value="You checked me!"> </td> <td> Left<input type="radio" name="radiotest" value="left"> Right<input type="radio" name="radiotest" value="right"> </td> <td> <select name="selecttest"> <option value="first">First <option value="second">Second <option value="third">Third </select> </td></tr></table> <input type="file" name="graphicchoice" size="30" accept="image/*"> <input type="hidden" name="hiddentest" value="This is a hidden field."><br> Enter a password (make up something): <input type="password" name="pswd" value=""><br><br> <input type="submit" value="Click for an example of the handle CGI scripting addition"> </form> </body> </html>

  2. Create an Applescript with the following code. Save it in the Web Pages folder with the name [spitback2.acgi]. Choose the "Stay Open" and "Never Show Startup Screen" options and save it as a Classic Applet.
    -- change the [option-return]s to the character produced by typing return -- while holding down the option key property crlf : (ASCII character 13) & (ASCII character 10) property http_10_header : "HTTP/1.0 200 OK" & crlf & "Server: PWS" & crlf & [option-return] "MIME-Version: 1.0" & crlf & "Content-type: text/html" & crlf & crlf property br : "<br>" property br2 : "<br><br>" property brr : "<br>" & return property br2r : "<br><br>" & return property rtn : return property rtn2 : return & return property pageTitle : "Unprocessed Results of handle CGI request" -- the following construction prevents email readers from parsing the html (I hope) property hht : "<ht" & "ml><he" & "ad><ti" & "tle>" property thb : "</ti" & "tle></he" & "ad>" & rtn & "<bo" & "dy>" & rtn on handle CGI request URL_path [option-return] searching for searching_for [option-return] with posted data posted_data [option-return] of content type content_type [option-return] using access method access_method [option-return] from address client_address [option-return] from user userName [option-return] using password password [option-return] with user info user_info [option-return] from server server_name [option-return] via port server_port [option-return] executing by script_name [option-return] referred by referred_by [option-return] from browser browser_name [option-return] using action action_used [option-return] of action type action_type [option-return] from client IP address client_IP_address [option-return] with full request full_request [option-return] with connection ID connection_ID set newhtml to http_10_header & hht & pageTitle & thb set newhtml to newhtml & "<h2>" & pageTitle & "</h2>" & rtn set newhtml to newhtml & "<b>handle CGI request</b> " & URL_path & brr set newhtml to newhtml & "<b>searching for</b> " & searching_for & br2r set newhtml to newhtml & "<b>with posted data</b><br><font size=2>" & posted_data & "</font>" & br2r set newhtml to newhtml & "<b>of content type</b> " & content_type & brr set newhtml to newhtml & "<b>using access method</b> " & access_method & brr set newhtml to newhtml & "<b>from address</b> " & client_address & brr set newhtml to newhtml & "<b>from user</b> " & userName & brr set newhtml to newhtml & "<b>using password</b> " & password & brr set newhtml to newhtml & "<b>with user info</b> " & user_info & brr set newhtml to newhtml & "<b>from server</b> " & server_name & brr set newhtml to newhtml & "<b>via port</b> " & server_port & brr set newhtml to newhtml & "<b>executing by</b> " & script_name & brr set newhtml to newhtml & "<b>referred by</b> " & referred_by & brr set newhtml to newhtml & "<b>from browser</b> " & browser_name & brr set newhtml to newhtml & "<b>using action</b> " & action_used & brr set newhtml to newhtml & "<b>of action type</b> " & action_type & brr set newhtml to newhtml & "<b>from client IP address</b> " & client_IP_address & br2r set newhtml to newhtml & "<b>with full request</b><br><font size=2>" & full_request & "</font>" & br2r set newhtml to newhtml & "<b>with connection ID</b> " & connection_ID & brr set newhtml to newhtml & "<hr>Results generated on " & (current date) & rtn set newhtml to newhtml & "</body></html>" as string set return_page to newhtml return return_page end handle CGI request

  3. Open the Web Sharing control panel. Select the Web Pages folder as the "Web Folder," and spitback2.html as the "Home Page." Click [Start].

    Web Sharing Control Panel

  4. Open your browser and point it at "http://127.0.0.1" (the loopback address). You should see the HTML page you saved in step 1. Fill out the form and click the submit button.

    Handle CGI Example page

    If all goes well, you should see a breakdown of the full CGI request.


    CGI results page

    Congratulations! You have just implemented an Applescript CGI. This example is just the extreme bare bones however. Future topics which I will present include:
    • Parsing the posted_data (or searching_for if using GET)
    • Error handling
    • Speed and Efficiency
    • Creating portable CGIs
    • Debugging
    • Redirection
    • Security
    • Returning data in pieces
    • Pitfalls
    This Space
    For Rent

    If you have found this page to be helpful, please send me an email to encourage me to continue this teaching series.

    Credit and sincere appreciation is due Jon Weiderspan, who provided the seminal work on Applescript CGIs.