Skip navigation.

Harold's Home

XML version of this site

PHP Scripts

CLI fun
Mail on 404
HB-NS (NewsScript)


APOD to Desktop
Dreamweaver Extensions


Other stuff
Central Grinder

OOOk Default:

VJ stuff
VJ Tools
Sample Movies


All articles in AppleScript

Folder Actions
Folder actions were introduced in Mac OS 8.5, released in October 1998. They have been in the Mac OS since then.

Folder actions, for the uninitiated, are AppleScripts that are attached to a folder (a directory if you like). When something happens to/in that folder the AppleScripts that are attached are run. You could, for example, create an AppleScript that opens every new pdf document added to it but leave the word documents alone. You could create a script that modifies all graphics files added to it, converting them to jpg for example. Or you could open a dialog window listing the number of kilobytes the folder contains every time it's opened.

As AppleScript is able to manipulate a lot of things in the system and also able to script a lot of applications the possibilities are virtually endless.

Now, after 8 years of having had the ability, I'm actually using one. The script is actually very simple and basically moves a file to a certain folder when it is downloaded from the internets. This saves me on average 15 seconds each time I download that file, which is a few times a week.
To say that I'm pleased would be an understatement. This may not be a great timesaver but the task of moving a file every few days to a deeply nested folder is repetitive and boring and I can now skip that entirely as the system does it for me automatically.

I'd love to be able to find other uses for folder actions but unfortunately my life isn't really that organised around files that appear or must be manipulated frequently.
Ah well, at least I finally made use of a system function that has been begging me to use it since 1998.

iTunes Print Playlist
Good friend Jurjan proudly told me he got a request for a new AppleScript that would print a nicely formatted iTunes tracklist for cd-inserts and the like.

Unfortunately for him this functionality has been built in in iTunes since version 4.5. Here's an AppleScript to bring up the print dialog making it look like you've done a lot of hard work, although it's probably easier to just hit Command+P.
tell application "iTunes"
    set theList to view of front window
    print theList kind track listing theme "Custom" with print dialog
end tell

Open this script in a new Script Editor window.

To open the dialog with the jewel case insert option selected use print theList kind cd insert with print dialog instead.

Good friend Jurjan has quietly released SleepyTime, an AppleScript that will allow you to use iTunes as a sleeptimer. The script will fade the volume while you get ready for sleep and reset it to it's original value just before quitting iTunes. (You can view the readme and source code by dragging the script onto the Script Editor.)

Nifty stuff, grab it while it's hot.

Films seen this year (number of)
We saw The Ladykillers today and while eating an excellent dinner at an Italian restaurant before we were wondering how many movies we've been to this year, and whether we've seen one every week on average.
As I have an iCal calendar which lists all the movies I've seen this year and I'm learning AppleScript I boasted that this information could probably be extracted via AppleScript.

Turns out I'm right:

tell application "iCal"
    set mydate to date "01-01-2004"
    tell calendar 2
        -- calendar number 2 is the films calendar, it contains entries for all the films I've been to this year
        set theStuff to count (get events whose start date is greater than or equal to mydate)
        display dialog "Number of films seen this year: " & theStuff
    end tell
end tell

Open this script in a new Script Editor window.

I saw 24 movies this year in the cinema this year. I wasn't at every movie the group went to but overall this evens out with the others methinks.

There's a couple of caveats I should add to the script above:
- I can extract the count of the number of events as this calendar lists only movies, therefore there is no pollution.
- I have four calendars: one for private appointments, one for movies I've been to, one for work and one for birthdays. The movies calendar is number two in the list hence the reference to the number two in the script.
- I have no idea of the syntax to get the names of the events, so I cannot extract the movietitles so far.
- This script was formatted using the utility "Convert Script to Markup Code" from Jon's Little Page of AppleScripts. If you're reading this in an aggregator such as BlogLines or NetNewsWire you're missing out on the syntax coloring (no big deal really, but just so you know).
- If you're using a Mac click the link to the script to open a new script editor window with the script.
- On compiling the code the script editor will replace 01-01-2004 with a local date-formatted string, here's what I see: set mydate to date "donderdag, 01 januari 2004 00:00:00".
screensot of final script

[Update 09-07-2004 03:37] I've figured it out. Here's the new and improved code:

tell application "iCal"
    set mydate to date "01-01-2004"
    tell calendar 2
        set theStuff to count (get events whose start date is greater than or equal to mydate)
        set theList to get events whose start date is greater than or equal to mydate
        set allFilms to {}
        set end of allFilms to "Films seen this year: " & theStuff & return & return
        repeat with aName in theList
            set end of allFilms to get summary of aName & return
        end repeat
        display dialog allFilms as string buttons "OK" default button "OK"
    end tell
end tell

Open this script in a new Script Editor window.

set newbie to yes
After much soul-searching and procrastinating I've decided that the next language I'll learn is Applescript. They say you should learn a new computer language every year. This is bullshit of course. You should never, ever, listen to THEM.

I thought about Python for a while but it's significant whitespace is frightening me.
I also thought about finally really digging into Perl as I got a book on that last year and never got further than chapter three. I'm still recycling the same old code I've been recycling for years and it's serving me pretty well so far.
The fact is that I'm now old enough to realise that I'll never be a real programmer. I'm a webmaster and knowing something about the big scripting languages is extremely useful but I won't be writing the next big CMS, nor do I want to.

As regular readers will have noticed I've been enamoured with Applescript for a while now and after struggling with some of the syntax this weekend I decided it was time for a more formal education.
So I've gotten me O'Reilly's AppleScript: The Definitive Guide because it's up-to-date and got good reviews on Amazon. I also read the sample chapter on the O'Reilly site and it looks thorough.

Besides, the beauty of Applescript is that is has grown immensely in the past few years. For instance the ability to integrate various commandline tools and scripts written in interpreted languages like Perl or Python is a big bonus.

Case in point. Here's the script I wrote this weekend:
-- check to see if the disk image is already mounted
if (list disks) does not contain "The Frozen Throne" then
  do shell script "hdiutil mount -quiet -noverify \"/Volumes/Riktor/Rescued Stuff/The Frozen Throne.dmg\""
end if

-- start The Frozen Throne
tell application "The Frozen Throne"
end tell

The script is an automated process of something I do quite frequently.
Whenever I play Warcraft I dislike locating and putting in the CD-ROM. Therefore I have created a diskimage that I mount on the desktop (Warcraft refuses to run without the CD). After the diskimage is mounted I start Warcraft to begin playing.

This script first checks whether the volume "The Frozen Throne" is already mounted or not. If it isn't then we mount the diskimage using the commandline tool hdiutil, which is faster than the Finder doing so as we can suppress all feedback and skip the verification process (it is known to be good). After that the script starts Warcraft.
Saved as an application this script is about thirty times faster than manually doing all the steps above.

Now here's the biggie. While the script above is pretty readable (although it is obfuscated a bit by the commandline tool language), Applescripts biggest failing is that the syntax is just too much like english and not enough like traditional programming languages. To assign a value to a variable you don't use $var = "value"; but you use set var to "value". It has numerous other oddities as well like in searching and replacing strings etc.
Anyway, Applescript has been called the most readable of all computer languages but the hardest to write. These claims are questionable if not downright false but they do contain a nugget of truth. Who could imagine that the syntax for getting the list of mounted volumes is (list disks)? Not me, that's for sure.

Anyway, expect more half-baked Applescripts in the near future as I automate my life. (Or watch me ditch Applescript at yet another language man was not meant to learn. Who knows, anything can happen!)

Open URL in iCab

I just realised that I never posted the original script from which the previous one was derived. It does the exact opposite, it opens the currently active URL in Safari with iCab.

tell application "Safari"
    set this_URL to the URL of document 1
end tell
tell application "iCab"
    GetURL this_URL
end tell

Notice the eery similarity in code.

[Edit: It was posted but in the wrong category, silly me.]

Open URL in Safari
This is probably of interest to one or two people out there but I'll post it anyway:

It's a small applescript application that will get the currently active document URL from iCab and open that URL in Safari. Handy if you're trying to view a page that refuses to work in iCab due to it's incomplete CSS rendering.

Here's the source for those interested:

tell application "iCab"
    set this_URL to the URL of window 1
end tell
tell application "Safari"
    open location this_URL
end tell

To get it to work just download, expand and save the file somewhere on your Mac and the drag the applet to your Toolbar Favorites.

Here's a screenshot of mine, I renamed it to -> Safari:

Note that with default security settings when you click this link you'll get a warning about starting an application. Just hit enter to continue.

Fun with Applescript's 'set'
Applescript is supposed to be the easiest language to read and about the hardest to get your head around if you're going to actually write it.

Every once in a while I get the feeling I'm missing out on something great when cobbling together a script. Luckily this moment often soon passes.

Here's a small script that will do all kinds of nifty aliasing and then deletes your Virtual Pet Rock (don't worry, he's hiding in the trash, just move him back manually).

Safari to iCab
I'm on a roll today.

If you've ever used Apple's Safari and enabled the debug menu there's a submenu there that allows you to view the current page in another browser (Opera, Netscape, Internet Explorer or Mozilla Firebird). What's lacking is my default browser: iCab.

Here's an Applescript you can put in your Libary/Scripts folder to view the current page in iCab:

tell application "Safari"
    set this_URL to the URL of document 1
end tell
tell application "iCab"
    GetURL this_URL
end tell

Paste the above into a new Applescript document and save as a normal script (no need to save as an application or whatever). Now you can easily access this script from the script menu.

Loop QuickTime
I wrote a new AppleScript that tells all QuickTime movies dropped on it to loop, great if you want to tell those 300 movies you have accumulated to loop (or not, it also works the other way round) and don't want to open them all up and set the property by hand and then save the files, which could take quite some time.
Works in Mac OS X 10.1.5 with Applescript 1.8.3 and QuickTime 5.0.2 or higher.

Applescript for BBEdit
I've been spending a lot of time in BBEdit recently coding PHP/mySQL based solutions for my daytime job as a webmaster for the virtual libraries of the Utrecht university of professional education (Hogeschool van Utrecht) so there's not much news on the Dreamweaver extensibility front. I did, however, write a small AppleScript that I thought I'd share with all of you that have BBEdit. It scripts the Prefix/Suffix Lines... Tool to add a tab to the beginning of all selected lines. This is useful if you've just added an if else clause or something and quickly want to indent a selection by a tab more. The script is cut and paste and available here.

Show all items | Read all items | Show topics

About, copyright, privacy and accessibility | Mail