New to Scripting......

Hey guys,

I’m reading this book on Applescripting… Trying to figure out a good project in order to experiment on what I’m learning. I thought of creating a script that helps me search and index mp3 files on my entire network. But it proved kinda complicated. Will work on it maybe in future when I’ve properly learned the language. For now trying to think of a more simple script project to work on.
How do you guys come up with ideas for scripts??? Is there a checklist of some sort???

Julz

Well, I’m an expert in being a non-expert, I suppose.

Where I started was, what are the tiniest things I want to automate just because I’d get something out of Bending the Computer to my Will and letting it do something I want it to do and it doesn’t out of the box. Think of the things that frustrate you as a user, where you think ‘I wish my Mac would just go and do this without being asked’.

The big thing that I love about the Mac was there’s a sense of being allowed-to-tinker that I never felt I got with Windows, so you can get it the way you want it, it’s forgiving, and you can choose what you want to do without it being channelled towards ‘you want to do something it doesn’t do out of the box, buy a product!’ the way Windows is.

A very good place to start, especially if you want to do stuff with your audio files, is Doug’s Applescripts for iTunes http://dougscripts.com/itunes/index.php: as a complete beginner, you can look at simple examples that aren’t too overwhelming, and will give you that all-important sense of ‘I can use iTunes to do things it doesn’t do “naturally”, even by writing a few lines of simple code’.

The thing I’m proudest of doing so far is learning how to fiddle with the details of an audio CD on iTunes so it does what I want it to do. The way iTunes stores CD details is in a database called CDInfo, and that’s what does the magic behind the scenes when you insert a CD your Mac knows about (or you connect to CDDB and it knows about that). This database seems to be just a bit fragile at times, especially if all your audio (like mine which is audiobooks, some ripped from CDs and some from CDs I burned myself from tape or Audible) doesn’t fit the pattern of ‘Songs from an Album’ which it expects to see. The most brain-dead thing it does is ‘every time you insert a long single-track CD, it puts in the tag metadata (ID3 things, which are what iTunes looks at for album/artist/whatever) for the last long-track CD it saw’. This really freaks people out, incidentally: the Toast forums are full of people who get scared when Toast pulls in junk metadata from CDDB this way. It is also fragile and occasionally breaks on its own, to the extent that occasionally I put in a CD and it doesn’t register until I delete the CDDB file and start again. And, of course, the way CDDB and CDInfo work involves looking at track lengths at the fraction-of-a-second level, because even if everything is in three-minute tracks the pattern of exact lengths are different. Except when they aren’t. Doesn’t happen often, but it’s annoying when it does (and one audiobook CD publisher in particular does exact five-minute tracks not broken for sense and pauses, which tend to get the wrong info entered on CDDB).

Is this a commercial project? No! Most people don’t have more audiobooks than they do music! The nearest I could find on the Internet to a program that did what I wanted it to do was a program called CDInfo, which works with the CDInfo database. The snag with that is if your CDInfo database pulled in junk data by getting confused about a CD or downloading the wrong info from CDDB, you’re then working from the wrong information.

So I made CDInfo write out the album details into an XML file, and did my best to learn about how to use XML from Applescript (which involved, for me, pretty much finding out about the DOM for the first time; if you’ve ever done anything with XML before that sort of step will be easier. If I could do it, however, any fool could). I was using XML, incidentally, because my boyfriend (a Windows programmer) pointed out that it’s ‘a better text file’ because you don’t have to interpret it in a line-by-line freeform way.

Now, if I insert the CDs of an audiobook, I can set the details once at the beginning for the things which stay the same, number all the tracks absolutely rather than by CD, and then write all that info out to an XML file (including the track durations down to fractions of a second, remember, because iTunes knows about that). Then I’ve got that information on file, so if iTunes ever loses its CDDB info again, I tell it which audiobook the disc belongs to, and it trots off and looks up the information in the XML file. Then it does what CDInfo and CDDB does, tries to match the durations with the durations of a CD ‘on file’, but I’m telling it to match against ten CDs belonging to that book, not ‘every CD ever inserted by me’ or in the case of CDDB ‘every CD ever inserted by anyone, ever’. And as for why the XML stage, it’s an attempt to keep the data clear of CDDB in case CDDB goes wrong again.

Am I telling you this because it’s something you might want to do? No! I’m telling you because it’s an example of somebody with about 0 coding experience learning how to do what they want as about their first coherent Applescript project (in the sense of ‘has its own data rather than just sticks two apps together’). I learned going along. And before I learned how to do that, I practiced sticking two apps together in even shorter programs.

And considering how I used Windows for about 20 years, including various attempts to learn Delphi, VB or VBA, and never once got the sense that I was allowed to tell my computer what to do, I feel that’s enough of an instance that the Mac works the way I think more than Windows does. Especially since my help was simply asking questions of a single mailing-list: I’ve never even met anybody who’s ever used Applescript. Doug never even responded to the stupid questions I sent him in e-mail (he’s a busy man, and had already given me loads of help by putting the site up), which means I not only had no-one to talk to locally, but had almost no help with the iTunes specifics other than what I could figure out by looking at code examples. All my own work, such as it is. If I can do it, anyone can.

So think about the things that frustrate you as a user, and in particular the things that have no commercial application whatsoever, so you’re unlikely to find that somebody else has done the work and you can buy it, then code from that. Take it slowly. Make it modular as far as possible (stick different bits in handler functions at the bottom of the file, so if it goes wrong you can look at one thing at a time).

Model: Mac mini G4
AppleScript: whatever’s in Tiger
Browser: Firefox 2.0.0.4
Operating System: Mac OS X (10.4)

Thanks for the email Woofb.
Ever heard of NT scripting??? Its nowhere nearly as powerful as Applescript and its quite limited, but it works very well.
I have a friend who is practically a windows freak. He does NT scripting on Windows XP and stuff and gets his PC to do alot of neat stuff. His best script is the one where he has over a 1000 songs in a complete network indexed and then provides a search and play capability. Its really so neat and works so fast that I kinda wanna do the same script for my Mac. (Hence my original post and the start of my learning applescript)

In the sense, what I’m saying is that there are ways of getting your Windows Pc to as you say, “bend to your will”; its just that you need to know how to do it. However there is one thing. With the invention of Vista, my friend’s NT scripting days are numbered since it does not support NT script. I’d suggest that he buys an old iMac and start applescripting, but he’s so into Windows & Gaming as well.

Sorry Woofb,

I need to correct myself there… I’m beginning to understand what you were getting at when you were comparing applescript in Mac to programming on Windows.

Well as far as I understand, you were basically saying that you get alot more control and command of the Mac through Applescript than you’d get if you were programming Windows. It gives you the sense that you are in charge (correst me if I’m wrong).
I do have to agree with you there. I had a conversation with my friend (the windows freak) and he said at times when he is scripting, sometimes he’s not sure what is going on behind the scenes when his scripts are running.

Well thats all and thanks alot for the help. Being a former Windows user for a long time, I understand why you wanted to switch to Mac. Windows can be quite frustrating at times and in my personal experience, always lets you down when you need it the most. (Crashing, Hanging, etc)

Julz

The big differences are: Applescript is designed for beginners (probably the first example file you will see is something like ‘tell application “Finder” eject disk’ or ‘tell application “iTunes” playpause’. This is at least somewhat intuitive).

Applescript makes your entire machine open to you on an object model of: Your Apps and Your Documents (including videos and audio), which assumes that you don’t want to reinvent the wheel, but you want to use Your Apps to open Your Documents. Comparatively, attempts to do easy VBA felt to me like: ‘Office owns your documents. If you ask nicely, it’ll let you open one’.

With resources like Doug’s Applescripts for iTunes (and many others), you can Google for simple free scripts to mine for Wisdom. While I was a Windows user I spent years Googling for simple free scripts, and kept tripping over blatant commercialism and the possibility of viruses. I spent the last three years before I switched to Mac googling increasingly-desperately for “Windows Media Player scripts for Windows” and “iTunes scripts for Windows”, without finding anything much, and kept finding “iTunes scripts for Mac”. My boyfriend tells me that ‘all you need to do is learn how to use a COM object in VB–easy peasy!’ To me, that adds an extra abstraction layer which you don’t get from ‘This is Your Audio App. It knows about playlists. Tell it to open, copy to or delete a playlist’. And you get a real if small sense of accomplishment the first time you realise you want to rename a bunch of audio files in iTunes according to a pattern, and you can watch the effect ‘ripple down’ through the playlist.

The lack of software diversity on the Mac has the subtle benefit that if you are looking for advice on how to script your apps, and you’re using iLife, Toast or the big graphic apps, there is advice out there. The nearest equivalent to that on the Windows platform is VBA for Office, but it’s obscured by the commercialism thing (it’s harder to find free help because there’s so much commercial training).

Applescript is free. It’s very tightly tied in to the OS, and developers are encouraged to hook it into their new stuff. There’s been nothing that ‘feels’ that way in Windows since Dos/batch: the Windows idea for easy programming is ‘get Visual Basic–oh, and you have to pay for that’. Because it’s free, and because you already have it, it gives you ‘permission’ to give it a go. Although my boyfriend says that the modern things like Javascript/C#/.NET are actually more free and ordinary that way.

It didn’t lock you into starting with a big IDE with a visual form and UI design, which I personally find liberating because what I want to learn is essentially small hobbyist projects that do simple things, not commercial apps. If I get more ambitious one day, I may try to find out about XCode/Applescript Studio. Until that point, I want to start with small things.

This may or may not make sense, and is definitely my opinion. But it’s the way the difference between the two environments feels to me, from a hobbyist’s perspective.

The book I’m following tell me ho to create script almost all the application in Mac… (itunes, Adobe products, etc…)

I bought myself a network edition of OS 10.3 Panther. It came with a Cd that had Xcode in it. Tried it out, so far considerign I do not know the language that much, I couldn’t make head nor tail of it. But its a very nice peace of development software. You can make applications using Applescript, Java, and C

I too might give it a good going over once I have mastered the language…