Workshop #2: Mac OS 8.5 and CGI Scripting

AppleScript and Mac OS 8.5: Introduction

Mac OS 8.5 Box The new Mac OS 8.5 upgrade delivers a number of significant improvements to AppleScript. Most important among them is that AppleScript 1.3 and its suite of standard scripting additions are now almost completely PowerPC native code--this results in speed improvements of up to 400% on Power Macintosh machines. (The AppleEvent Manager, curiously, is still in 680x0 code, but will likely become native or be replaced in a future release.) A number of adjustments and improvements were made to the language itself, new scripting additions have been added, more Mac OS components have become scritpable, and a few new ways to use AppleScripts have been introduced. This workshop will go in-depth on a few of the new scripting features of Mac OS 8.5--for more information, see AppleScript Help (accessible from the Help Center menu item or in Script Editor) or the AppleScript web site.

Language Improvements

A few improvements have been made to the language itself to better support more systems and more uses. When writing scripts in 8.5, you'll likely notice that all references to data type string are changed to plain text when the script is compiled; don't worry, this is completely "normal." Apple added the data type international text--a format for working with Unicode and non-Roman scripts that can normally be treated the same as a string--and changed the name of string to plain text to better reflect the difference. Either term can be used, and both types of strings can be treated the same way under normal circumstances.

Also, a number of common units of measurement have been implemented as primitive data types; this means one can convert among units of measurement using the same methods used to coerce variable types:

18 as inches --result: inches 18.0

result as centimeters --result: centimeters 45.720000000009

Obviously, attempting to convert between different classes of measurement (such as from length to volume) will return an error. You also cannot perform math with unit values; instead, convert them to matching unit types and then to numbers:

set x to 200 as grams

set y to 10 as kilograms
set z to (x + y) as ounces --error: "Can't make pounds 150.0 into a number"

set x to 200 as grams

set y to 10 as kilograms
set z to ((x as pounds as number) + (y as pounds as number)) as ounces

This table lists all the unit types and classes available:
Class of measureUnits supported
lengthkilometers, meters, centimeters
miles, yards, feet, inches
weight and masskilograms, grams
pounds, ounces
liquid volumeliters
gallons, quarts
volumecubic centimeters, meters
cubic inches, feet, yards
areasquare meters, kilometers
square feet, yards, miles
temperaturedegrees Fahrenheit
degrees Celsius

(Notice that valume and liquid volume are different classes and therefore can't be converted between. Why they didn't allow the ability to convert between cubic centimeters and liters is beyond me, though.)

Though the methods for dealing with dates are still a bit unwieldly, the situation has been made a bit easier by the addition ofthe built-in constants minutes, hours, days, and weeks: 60, 3600, 86400, and 604800, respectively. Since the Mac internally uses an integer number of seconds to represent date and time, these terms can be used to more easily work with relative dates by using the following style of syntax:

(current date) + 24 * hours

--result (when I ran the script): date "Friday, November 13, 1998 9:42:36 AM"

This example uses the idle handler (called whenever the Mac gives AppleScript a chunck of processor time and it's not executing anything else) to stop a script after 30 minutes of inactivity:

global endDate

set endDate to (current date) + 30 * minutes
on idle
     if current date is equal to endDate then tell me to quit
end idle

On to New Scripting Additions and Components...


Last updated 11.12.98 by Rick Roe for MacWarriors
Copyright 1998 ACM@UIUC