by Stephen Swift
AppleScript In Mac OS X: A Brave New World
[Author's Note: This marks the 6th installment of AppleScript: The Macintosh Autopilot. If you find any of this tutorial not making sense, check out the some of the earlier installments.]
May 22nd, 2002
After an hour of Mac OS X, you decided that Mac OS 9 is as outdated as System 7. You're just about to snuggle in when you realize that something doesn't feel right. You're missing your AppleScripts, right? I know the feeling. Don't despair! Once inside Mac OS X, AppleScript is easy; however, the transition can be a little rocky. That's why I'm providing you with these instructions on updating your scripts for Mac OS X.
Upgrade! The most recent version of AppleScript for Mac OS X is 1.8.1. It offers the best compatibility for your scripts between the systems and most scripting versatility. My tips pertain to this version and may not work on other AppleScript versions.
COMPILE, COMPILE, COMPILE
Mac OS X should recognize your scripts. Most of them will need a recompile to verify that everything is hunky-dory in this new system. Luckily for you, Apple's AppleScript Swiss army knife, the Script Editor, hasn't changed aside from the new Aqua face lift.
Convert your small scripts (10 lines or less) first. If you try to convert huge scripts, you'll just run into error after error. What fun is that? Pay attention to what Mac OS X balks at. I recommend looking at scripts that deal mostly with 3rd party applications. Scripts that use Eudora's dictionary, for example, should have little problems. Scripts that interact with the Finder a great deal will need the most tweaking. After all, the Finder is the application that has changed the most from Classic to Mac OS X. Read the section on compatibility to learn more on what you can do to smooth out this problem.
Pay attention to scripts that work with applications that do not run in the Mac OS X environment such as Photoshop. You have a few options here. If your script needs to access Photoshop repeatedly, I recommend to check the box in the Finder's Get Info window of the script to specify that it open in the classic environment. However, if you only need to use Photoshop for a little part in your script, AppleScript can access the Classic programs while running in the Mac OS X environment. Yes, that is cool.
Run A Script
I find the easiest way to run my scripts in Mac OS X is through the Script Menu. If you save your scripts as applications, they should be saved in a universal-format that will run in Classic (under Mac OS X), OS 9, or OS X. However, you could still use commands that would trip up one of the operating systems. Make sure the commands you use are compatible and the syntax works with each system. Again, I'll get to that soon.
What about scheduling scripts? Well, I haven't heard from iDo yet, but there is a program called Script Timer that will run scripts at any time you want. It runs both in Mac OS X and 9. For more information on all its cool features check out their Web site.
I find the number one problem to be trying to make a script that will work in Mac OS 9 and X perfectly. As Mac OS X and AppleScript develops, this becomes easier. You must test your script on both systems. Sometimes, you may have to end up creating two different versions of the same script.
The Obvious Pit-Falls:
- Mac OS X does not yet have pop-up folders or folder actions.
- There is no button view, but now there are columns.
- Mac OS 9 has no dock or UNIX kernel.
- Any other major change that people praise or gripe about. :-)
I can't list every syntax change partly because I haven't encountered all of the changes yet. Here's what you can do:
- Download the Essential Sub-routines help file for Mac OS 9 and Mac OS X. These are great tools for the "I can't remember how to..." questions. Compare how each system expects the commands to be given to it. They cover much of the basic syntax.
- Look at AppleScript examples from Apple. They have iPhoto, QuickTime, Sherlock, TexEdit, and iTunes examples. Also look at their toolbar scripts to get a feel for how the Finder handles folders and windows. All these are great reference material. When your script is giving you errors, check Apple's examples to see how they wrote their code.
- Search Apple's built in help for explanations on how to use AppleScripts in Mac OS 9 and X.
Here's a can of worms if I ever encountered one! You need to check each script to see if it uses any Scripting Additions. The Standard Additions and ones from Apple should be fine. You'll run into problems if your script uses third party additions.
If you want to use scripting additions found in the Classic environment, your best solution is to stay in the Classic environment. Save your script as an application and make sure to check the "open in Classic" option in the Finder's Get Info window. If you need to access classic scripting additions and send commands to Mac OS X, then I will direct you to an in depth analysis of your situation.
Check the Scripting Additions Web site regularly to see what additions have been ported over to the Mac OS. They can be installed in many places such as the Scripting Additions folder or in a specific user's Scripting Additions folder. Most Mac OS X scripting additions should come with documentation that explains how to use them in much more detail. Not many additions have been ported over, making it somewhat difficult for the advanced scripter to switch over to Mac OS X. Hopefully, we'll see more additions soon!
Oh boy! I didn't get to cover a lot of really neat and new Mac OS X specific AppleScript goodies; it's important that everyone has made a smooth transition from Mac OS 9 first. To those of you who didn't need to do a lot converting for your old scripts, familiarize yourself with the new system. Look at the examples and try to create your own scripts. Look forward to building Mac OS X scripts from scratch, taking advantage of new commands! Also, I'll take you on a tour of the AppleScript Studio and show you what you need to know to create really beautiful scripts. There will still be a lot of relevant information for those of you working in Mac OS 9; however, you should seriously think about upgrading if only to fiddle with the new system. It is really amazing. If anyone gets stuck, please send an e-mail to Stephen@macobserver.com and I'll help you out!
AppleScript on Mac OS X is a learning experience for everyone. There will be new features and problems. I hope everyone is as excited as I am as we bravely explore this new world.
Comments or Questions? Is this column going to slow or fast for you? Do you want to script something, but don't know how? Do you need something explained or have a question about a script? My E-mail address, email@example.com, is open 24 hours a day, 7 days a week.
Most Recent AppleScript Columns
Break Your Script Into Pieces with Subroutines
Repeat Yourself With AppleScript
Back to The Mac Observer For More Mac News!