AppleScript Expedience

 

Quick Links

Intro | Finder Scripts | QuickTime Player Scripts | Mailsmith Scripts.

Why AppleScript?

If you're using a Mac and not using AppleScript you are missing out. AppleScript is one of a handful of programming languages for the Mac's 'Open Scripting Architecture'. It's the one that Apple make themselves, and is therefore the most widespread. Other languages available include JavaScript, and the scripting language for the Quickeys automation software. If you'd like to learn AppleScript, you could do a whole lot worse than to run through Bill Briggs' excellent primer.

I've been a casual AppleScripter for several years. I've got to the point where I have a bunch of scripts which I use every day, and I can't contain my enthusiasm for this technology. I've come to rely on it so much that when I sit down at someone else's Mac, I quickly find myself hunting for the script editor application so I can do this or that task, such as changing the creator types of files, which might otherwise require a piece of dedicated shareware, or several hours of manual work. I've also become quite critically aware about the scriptability (or not) of the applications that I use, in some cases making purchasing decisions on those grounds. .-There are some tips for finding Script Editor and some other useful tools below.

The scripts given here are relatively unglamorous, but they are useful, unlike some 'wacky but useless' scripts you will find on the net. They do the job, they are editable (source code included) and I've used them pretty much every day, so they've had a chance to be polished, if not to perfection, then to a decent level of useability. Icons are provided to indicate what kind of script they are.

Some of them require - or at least work best with - OSAmenu, which is discussed below. The main reason for this is that they require the Finder selection as a target object, so if you were simply to make an applet of the same code, double-clicking it would set the Finder selection to the applet itself, which is of questionable usefulness.

Icon Explanation

Denotes that the script concerns, manipulates or enhances the standard Script Editor application.
Denotes that the script is compiled but (unless indicated) also includes source code. Should be attached to a script runner application such as OSAmenu, or run from the script editor.
Denotes that the script is compiled into an application/applet but (unless indicated) also includes source code.
Denotes that the script is compiled into a 'droplet' application/applet but (unless indicated) also includes source code.
Denotes that the script is available only as plain text, which should be compiled on the fly, or opened in a script editing application and compiled there. In some cases, the terminology on specific machines precludes the distribution of scripts which are pre-compiled.

Denotes that the script is intended (or highly suitable) for running from OSAmenu or another system level script runner application.
   
   

Enjoy!


Finder Scripts

creatorcode

filetype

Two, almost identical similar scripts to do that fiddly job of setting the creator and file type of a given file (or a group of files). Yes, there are dozens of shareware utilities out there which do the same job, but these are free, and of course you can mess around with the code to make them do more specialised tasks.

They will give you the four letter codes for the creator code and file type of the file(s) selected in the Finder or the file(s) dropped onto it. You can either copy the code to the clipboard, choose to 'inherit' the code of another file or change it manually to something else. Runs equally well from OSAmenu or as a droplet.

Whodunnit

Working on a similar principle to 'creatorcode', this droplet or OSAmenu script will open the folder containing the application which created the selected file(s).

Example; You have a photoshop document on the desktop which requires a lot of RAM, and you want to increase Photoshop's memory partition. You know where to find Photoshop, but can you really be bothered to dig into your Applications folder to get at it? With this script, you wouldn't need to. Just select the document, run 'Whodunnit' and the Photoshop application folder opens.

Rename Frontmost Folder Window

Another super simple one. You have a folder window open in the Finder and you want to rename that folder. What do you do? Open the container of the window, locate the folder and then rename it? Nope, just run this script from OSA menu.

Trash Folder of Frontmost Window

Again, without messing about, you can trash the folder whose window you are looking at by running this script from OSAmenu.

Close Windows in Path to Frontmost

You've been navigating through a series of folders, drilling down without using the option/alt key (tsk tsk), and leaving all manner of windows open along the way. The desktop is a mess! Now you want to close any windows you opened on the way to this one you are looking at. A quick visit to the OSAmenu will get the job done.

Select Whose Name Contains

You have a file window open with hundreds of files, distinguishable only by some non-alphabetical naming convention. You can run this script from OSAmenu to select any files whose name contains a given string of characters. These files can then be deleted, labeled, moved, copied or whatever you need to do.


Essential AppleScript Tools

Script Editor

Alas, most people have their script editor application buried and forgotten in its default location, which is as follows:

Up to and including OS 9.0.x

Macintosh HD:Apple Extras:AppleScript:Script Editor

OS9.1.x and OS 9.2.x

Macintosh HD:Applications (Mac OS 9):Apple Extras:AppleScript:Script Editor

On OSX, you'll find it at

Macintosh HD/Applications/AppleScript/Script Editor

If all these paths are alien to you, just search for the thing by name using Sherlock, but you're gonna have to grok paths sooner or later.

OSA Menu

->

Another absolutely essential utility is Lazerware's OSA menu . This allows you to run AppleScripts from the menu bar. Scripts can be organised on a per-application basis, or saved in a universal scripts folder, accessible to all open applications.

If you're running OSX, Apple provide a similar utility called Script Menu.

The big advantage here is that you don't need to go to the Finder, or to the Script Editor application in order to run a script, and scripts are automatically executed in the context of the frontmost application. This is especially useful in the Finder, because you can make a selection in the desktop, or open a given window, and then run a script from the menu bar which uses the Finder selection or frontmost object as input. There are some very useful examples of this below.

If you'd rather use the pre-OSX control strip to run your AppleScripts, you could try ScriptStrip, or Nivek Research's OSA Script module. If you know of any other third-party system-level script runner utilities, please let me know.


Random musings and rantings

The Mac is (traditionally anyway) the computer for non-geeks, so why would Mac users want to even think about programming? Well., good question, and I've come to realise that a large number of people will never be able to grok it, but scripting is really much easier than systems programming using a compiled language like C or Pascal, so the community of scripters is at least partly made up of people who don't just do programming - who have other skills, who typically have a very good idea what the software they use is not letting them do easily - so they use scripting for small, dedicated tasks. Some of these tasks are of no interest to other people, some of them have more broad application, and it's the latter that I want to focus on here.

Now of course, the Mac is being invaded by long-haired UNIX geeks who've never used Photoshop in their lives. The graphics folks are all at sea, and the UNIX geeks are doing their best to be reassuring. Well, it's not entirely a bad thing, but in OSX we've already seen a dilution in the dedication to useability that was always the Mac's reason to exist. Many would argue that stability should come first, which I can go along with, as long as we get on to useability second. I'm still waiting for them to finish the stability stuff.

One thing which is a shame is that there's no such thing as AppleScript on UNIX - except on OSX of course - so most UNIX programmers go to a scripting language they know better. Shell scripting, PERL or whatever. We might expect AppleScript with its cranky hypertalk-like syntax will get elbowed further into the margins it occupied in the old Mac world.

Fortunately, the DTP community kicked up so much of a stink about the importance of AppleScript when Apple where transitioning to OSX that Apple have realised that it is one of the main unique selling points which keeps Macs running in print-production houses worldwide. (There's nothing quite like it on Windows). Consequently, AppleScript has become a central pillar of OSX, almost as important as Java, indeed, Apple's latest development tools allow AppleScript to be used to create bona-fide OSX applications with a nice user interface and everything, something which was relegated to third parties on previous systems. Where AppleScript once looked as if it were destined for the same dustbin as OpenDoc, Apple is now taking AppleScript more seriously, and that's a Good Thing.

AppleScripts needn't be long and complicated. A few lines can often achieve amazing results, especially compared with the manual equivalent. Having said that, if all AppleScripts were only a few lines long, this page wouldn't exist, because everyone would understand everything about it immediately. No, a typical AppleScript which might be said to have 'general use' would be from 50 to 100 lines long. They can be much longer, but Apple's original Script Editor only supports scripts which contain less than 32k of text, about 32000 characters, which presents a arbitrary limit - at least as far as portability is concerned. Script Editor 2.0 (released at the start of 2003) represents a massive improvement, allowing larger scripts.