So, in accordance with my new acquisition (G4 PowerBook) I’m considering learning AppleScript for scripting relatively simple routines, as many users seem to rave about it. (Then again, a lot of people rave about VB, too, which I’m perfectly ambivilent about.) I know whit about AS, and am wondering if it is worth spending the time on it. Currently, for my regep needs and such I generally use Perl, and I’ve been meaning to come up to speed on Python for a while, but is AppleScript worth the bother? Does it play nice with Perl or Python? Do Cocoa and Carbon offer APIs that are easily manipulated with Perl scripts? What is your general experience? What references, if any, would you recommend? (I generally go for the O’Reilly books, but I’m open to suggestions.)
AppleScript is a sweet little language. It’s easy enough to learn in a night or two, there are lots of prebuilt bits of code available, and many Applications come with built in support for scripting their functionality. That makes it really easy to automate some tasks that would otherwise require a massive coding effort.
The upcoming version of OS X, Tiger, includes a visual scripting environment called Automater. If your needs are indeed basic, that might be all you need to know. I don’t know how it interacts with Perl or Python, though I understanding calling these languages in Applescript itself is not a problem. (I myself am not a programmer, but am looking forward to Automater!)
Someone made a Firefox extension that puts a control panel for various media players into the browser. Included is support for iTunes, using the AppleScript interface. So it is definitely used for some interesting things.
Knowing “real” languages (yeah, I know it’s real in that it exists), I always find Applescript frustratingly difficult, at least for writing scripts from scratch. It’s super easy, though, to pick up for the purposes of editing other scripts. Go for it.
As Squink mentioned, one of the best things about AppleScript is the fact that so many apps are scriptable, so that you can automate huge amounts of stuff that’d be really difficult to do with a scripting system like perl. And it is really easy to learn. In many cases, you can record actions in an app and then use that as a starting point for modifying the scripts to do even more stuff. “Real programmers” often have disdain for it, but if you’re more interested in getting results than in being a stud coder, it’s the right way to go at times.
rackensack: On the other hand, Real Programmers are interested in making their machines do neat things, and Applescript seems like a simple way to do that. Application-specific and domain-specific languages will always have their place, especially if they can be used to automate a reasonably powerful system.
In the Unix world, we have something that sounds really similar: Shell scripts. A shell is the command prompt you type into at the command line, and from the earliest days the various shells have been scriptable with simple languages. The power of shell scripting is that you can use a simple language with lightweight syntax to control complex programs and remember drawn-out command sequences. Shell scripting has always had a place among the Real Unix Programmer’s toolkit.
(As a parting shot, Perl has very obvious ties to the scripting language of a specific family of Unix shells, and in some ways allows you to write portable shell scripts.)
If your a stud coder, you can write your own scripting additions in c that grab onto an unscriptable application’s window pointers, and hence control list, and send them bogus events. (at least you could in 8.5. I suppose pre-emptive multitasking has made that sort of kludgery tougher, but still, Applescript’s use of Apple events leaves a lot of room for creative hacking.)
Yeah, I get the point (and Derleth’s), but mine was that I’m not now and never have been a stud coder (or any kind of coder, really), but I’m able to create solutions to real-world problems using AppleScript, without having to invest months in getting proficient in a “real” language. There are lots of cases where it’s a bad choice for the problem at hand, but there are lots of others where it’s exactly the right choice.
I wish the dictionaries were better (some of them are horrifyingly opaque and nonhelpful) but many times I’ve succesfully scripted something without a clue as to what the real nouns and verbs and property-adjectives are for that program, just trying it in English and substituting other words for whatever terms tossed up error messages when I tried to compile.
For what it’s worth, I wasn’t trying to refer to myself as a stud programmer – quite the opposite.
I don’t have any disdain for it at all. I mean that it’s truly difficult to learn as a “real” programmer because it’s not built like an artificial language – it tries to be English. Therefore I don’t get my mind around creating things from scratch, because it’s not computery.
I guess I could just sit down and deliberately learn the language rather than just “pick it up” like I’m used to doing. For example, having studied and learned Pascal, it was super easy just to “pick up” C (yes, with a reference book) without having to “learn” it. PHP was also something that didn’t have to be “learned.” Access Basic and Visual Basic didn’t have to be “learned,” either. They all have the same basic concepts and things operate virtually identically in all of these languages.
AppleScript, though, is just so different that I’d have to actually sit down and practice it and go through example programs until I were able to just “get it.”
I can understand that – even though the opposite is true for me. I came to AppleScript with no real programming experience (other than PostScript, which is sort of sui generis, modulo the Forth-like aspects), but with a lot of HyperTalk work behind me. (HyperTalk was the script language built into Apple’s late and much-lamented – at least by folks like me – HyperCard application). The way AppleScript works is a lot like HyperTalk – and both are, as you say, a lot more like a natural language than a computer language, so they were easy for a non-programmer like me to “just pick up” as you put it, wiithout ever sitting down to formally study them. And that’s the point I keep coming back to – if you’re experienced writing code in other languages, AppleScript can be a challenge to deal with, but if you’re coming at it as a non-programmer who needs to accomplish something that involves causing existing applications to do interesting things they wouldn’t otherwise be able to do, it’s often the quickest road to achieving that goal.
I’ve looked at Applescript before, but didn’t really grok its purpose or its fucntions.
There’s something I do very frequently:
Use Command-Shift-4 to capture a screenshot of a selected region, then open the screenshot (Picture 1.pdf on my the Desktop) in Preview and export it as a jpeg.
Is this something I could streamline using Applescript?
We recently got a $20,000 contract to do the same thing over and over again with a crap load of different datasets. It took a week to write an Applescript to do it all, then left it going over night. For this reason alone, I have a great fondness in my heart for AppleScript.