Friday, January 20, 2012

Learning Applescript

I'm a programmer, always have been, always will be. My current job doesn't involve much programming, so I enjoy doing projects on the side. About a year ago I got a MacBook Pro and have been looking forward to doing some programming there, but have been too busy with home improvement projects etc. I  wrote a program to manage backups from my server to a hard drive, but that was just a bit of Perl, nothing fancy.

After upgrading to Lion and buying an e-book about it, I learned that AppleScript is included, so I also picked up an e-book "Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X" and also Apple's language guide. I'm only about a third of the way through the Learn book, and I've done variations on several of the exercises.

Wow.

Wow.

I think AppleScript will be my new favorite language. The English-like syntax is OK - I don't really need that level of simplicity - but the power of what it can do ia amazing! Controlling scriptable applications is so powerful. I can envision doing lots of things with mail, Office programs... If one tried to do this in any of the C-based languages, it would take a lot of work. AS makes it simple, at least that's how it works so far.

With just what I've learned so far, I've pulled data on songs from iTunes, linked that to Finder to get data about the song titles, done simple searches... it almost lets you treat iTunes like a database.

Today's exercise involved events and setting up a simple client-server system. It turns out that AS itself is a scriptable app. I created a server script that runs in the background, executes a routine task every 10 seconds, and also responds to commands from a client. From a GUI dialog box in a client app, I can start, stop, and request services from the server app. The client is 11 lines of code. The server (which just speaks "initializing", speaks the current time on request, and speaks "quitting") is 22 lines of code.

And all this fits neatly into the Mac GUI... scripts can be dragged to the dock like any other apps.

Wow.

I can't wait to see what's in the remaining two thirds of the book!

No comments:

Post a Comment