Jun 20, 2002

The AppleScript Experience

Apple's AppleScript and AppleScript Studio are powerful programming tools; they give me the power to write programs, utilities, and scripts that would otherwise take considerable skill, time, energy, experience, and effort. Apple has done it once again with this editor and language that allow me to eschew the traditional route to writing programs.

The greatest thing about AppleScript and ASS is the ability to compile the scripts into actual machine code. Though it does take away the ability to edit the script on the fly, it saves about 2x the space (thanks to dynamic libraries) and increases speed upwards of 10x (thanks to AltiVec optimizations). This is far more powerful and advantageous than VBScript in Windows or REXX in OS/2. And since AppleScript can even integrate with Perl and shell scripts, it has edges no other OS or scripting language has. It was these awesome features that allowed me to delve into a project never before thought possible, thanks to Apple and Mac.

Being a student of Computer Science, I had just taken OS Fundamentals, an upper-division class that covered the lowest levels of an OS. We looked at the Linux, FreeBSD, QNX, and Mac OS X/Darwin's Mach kernels. Being a Mac user, I was especially interested in Mach and began reading about its history at Carnegie Melon under Avie Tevanian (now Apple's head of Software Engineering). It had also been used as the basis of many other operating systems, such as Apple's abortive MkLinux, IBM's still-born OS/2-for-PowerPC, and the under-developed GNU HURD.

Wondering how I could overcome the inherent slowness in the design of Apple's heavily modified Mach, I began researching on how it was designed. Apple's OS team, like any other software engineering project, had made a few trade-offs: strip out functionality to make hardware-to-software messaging from the upper OS levels lightning fast, or keep a lot of features in and push more of the OS's functionality toward the faster, lower run-levels. Wanting to further understand the fundamentals of Apple's Mach, I took to rewriting bits and pieces of it in ways I understood.

But C was a nightmare for me; as a Mac user, I was used to something a little friendlier. Quite frankly, C was a textual hive of demon spaghetti worming through my head with no semblance of order or efficiency. I needed to do this in a way I understood, in a language that I could deal with. And that is where AppleScript came in. I started up AppleScript Studio after downloading the December Developer's CD. After getting into ASS I never looked back at GCC or Pico ever again.

My first tries at getting a running my Mach kernel failed miserably but I learned quickly from my mistakes. The running cuts were abysmally slow, worse even than OS X itself, and I almost gave up the project then. But after compiling my code, things picked up ten-fold. With the speed problem hurdled, I set about tweaking my AppleScript code for talking to the hardware. This is where knowing how to use Apple's Vector Engine libs and Motorola's AltiVec calls came in handy, and before I knew it I had something that ran considerable faster than the native kernel it was being developed on!

In less than a month, I had a fully working, optimized Mach kernel written in AppleScript thanks to Apple's ASS. The moment of truth came when I replaced the regular, default Mach kernel with my own. Sweating, my breath coming in short gasps, I could barely stand the anxiety as my Power Mac 8600 rebooted… successfully! All thanks to Apple's wonderful scripting technology that is so much more than what it appears to be.

Now, I work at Apple on the OS Engineering team thanks to my low-level tinkering experience. I graduate college soon with a Master's degree in Computer Science. The experience offered to me by Apple's tools and technologies have allowed me to be where I am today, re-working vital bits of Darwin with AppleScript and enhancing the wonderful language itself (look for my name in the next AppleScript update you download!)

2 comments:

  1. By any chance, is your source available so people can learn how this magic was accomplished?

    ReplyDelete
  2. You smoke some really good crack.

    ReplyDelete