42 Astounding Scripts

MacOS uses Perl, Python, AppleScript, and Automator and you can write scripts in all of these. Build a talking alarm. Roll dice. Preflight your social media comments. Play music and create ASCII art. Get your retro on and bring your Macintosh into the world of tomorrow with 42 Astoundingly Useful Scripts and Automations for the Macintosh!

Jerry Stratton

Language is a slippery thing, and sorting out sloppy semantics may always lie beyond the ken of electronic brains. — Ben Bova and Don Myrus (The Best of Omni Science Fiction No. 4)

Check42 for verifying Astounding Scripts—Friday, September 18th, 2020
Check42 example run

As I wrote in the intro to 42 Astoundingly Useful Scripts and Automations for the Macintosh, there are two ways to get a script from the book to your computer. You can copy and paste it from the ebook. Or, you can go the traditional route and type it. It was old-school books with the latter sensibility that inspired me to write 42 Astounding Scripts, but I recognized that there were serious issues with that model, mainly to do with catching typos.

The best old-school computer magazine I read back in the day was The Rainbow for the TRS-80 Color Computer. Content-wise, it was very much like 80-Micro, which was the second-best: filled with amazingly cool BASIC programs, tutorials, and hardware projects. But unlike every other computer magazine that included BASIC code, The Rainbow had a program called rcheck and later, rcheck+.

Typos were inevitable typing in even short programs; the rcheck programs were designed to tell you not only that you had a typo but where the typo was, within a defined block of BASIC lines.

The rcheck+ program was so useful I’ve rewritten it in Perl, partly for the old-school community but mainly for myself.

It’s much more difficult to write something like that for modern scripting languages. BASIC had line numbers, which meant there were no blank lines. BASIC didn’t have indentation, which meant there was no worry about tabs vs. spaces. It didn’t even have tab characters in the code, at least on the Color Computer. And because memory was so tight, there weren’t very many spaces, either. There wasn’t even upper and lower case for the most part.

Still, I very seriously considered including a program like rcheck in 42 Astounding Scripts. Ultimately, I decided that it was likely to cause more problems than it solved. But last week I had the epiphany that an rcheck-style script could provide a literal line-by-line checksum, in fact a series of checksums that separate indentation from code, and that I could provide a zip file of those checksums.

Thus was check42 (Zip file, 107.4 KB) born. This script takes a script name as an argument and checks each line of that script against a corresponding line in a checksum file. It looks at (a) the level of indentation, (b) the number of characters in that line of code, (c) the sum of the ASCII values of those characters, and (d) an md5 hash of those characters.

Catalina vs. Mojave for Scripters—Wednesday, September 16th, 2020
Swift conditional at end

As the author of a book filled with scripts for the Macintosh, I was worried about the differences between scripting for Mojave and scripting for Catalina. When testing the Catalina update of the book, it turned out there wasn’t much difference. For the most part, what worked in Mojave worked in Catalina.

The most obvious changes, of course, were those having to do with iTunes. Catalina broke the iTunes app into several apps. The new app for music is called Music. This means that AppleScript scripts need to change from:

  • tell application "iTunes"


  • tell application "Music"

The application id has also changed, from “com.apple.iTunes” to “com.apple.Music”.

The application id is useful if, for example, you want to do some stuff if the Music app is running, but skip out if the Music app is not running. The normal means of talking to an application in AppleScript will start the application up, which means it can’t be used to check whether the application is running: it always will be, after using “tell” on it. The application id can check the status of the app without opening the app:

[toggle code]

  • property iTunes : "com.apple.Music"
  • if application id iTunes is running then
    • display dialog "Music is running"
  • end if

The other new iTunes successor apps—Podcasts and TV—don’t seem to be scriptable via AppleScript.


Within other scripting languages, there’ve been more interesting, if obscure, changes. In 42 Astounding Scripts I have a Swift script for playing alert sounds.

[toggle code]

  • //wait until the sound is done before going on
  • while sound.currentTime < sound.duration {
    • usleep(100000)
  • }

Waiting is necessary because if the script exits before the sound is finished, the sound will stop. The obvious method of checking if the sound is still playing—NSSound.isPlaying—doesn’t work for alert sounds as it does for audio files. It remains true until calling NSSound.stop(), which makes it useless for deciding when to call NSSound.stop().

Play music faster in the Music App—Friday, September 11th, 2020
Photos Scripts menu

Every app on macOS has a Scripts Folder, and you can open it from the scroll-iconed Scripts Menu in the menu bar.

When I updated 42 Astoundingly Useful Scripts and Automations for the Macintosh for Catalina, I removed this script. Its purpose was to speed podcast playback, and the Podcast app, in Catalina, has that ability built in. That made this script less useful and less astounding, so I removed it and replaced it with a backmasking script, that is, one that plays music backward to reveal hidden messages.

But I promised to make the speed playback script available, and here it is. You may find it useful if you’re still on Mojave or earlier, for example, and are still listening to podcasts through iTunes. Or you may just find it enjoyable to turn your favorite music into Alvin and the Chipmunks.

I’m not judging.


This is a very simple script. It takes the currently-playing (or currently-paused) track in iTunes, gets both the file location and the current play position, and then opens the track in QuickTime Player to start playback from that position at the higher speed. I have it set to play back 60% faster (1.6x). Change the preferredSpeed property at the top of the script if you prefer a different speed.

AppleScript Editor: Enable Script Menu

To enable the Scripts Menu, you’ll need to check the appropriate boxes in AppleScript Editor’s preferences.

Paste this code into AppleScript Editor and save it as “Speed Playback” in your iTunes Scripts Folder. It will appear in iTunes under the scroll-iconed script menu in the menu bar.

Here are the important variables in the scripts:

alreadyPlayedHow much of the track we’ve already listened to in iTunes.
preferredSpeedThe multiplier to playback speed.
trackLocationThe filepath to the track.

And here is the script code:

42 Astounding Scripts, Catalina edition—Wednesday, September 2nd, 2020

I never upgrade my phone just before a trip, and I never upgrade my computer during a major project. Two weeks ago, however, I finally put The Dream of Poor Bazin to bed. You can buy it on Amazon or any of the usual eBook sites.

So last week, I finally got around to upgrading to Catalina.

The upgrade went pleasantly well, and very quickly. It may have been the easiest macOS upgrade yet. I have now updated 42 Astoundingly Useful Scripts and Automations for the Macintosh for Catalina. If you have the ebook, the updated version is already available at the store you bought it from, if not already downloaded to your e-reader.

If you have the print version, or if you’re just interested in what had to be changed for Catalina, I’ll have more about the specific changes later, but for the most part the scripts continued to work after the upgrade. As expected, any scripts that talk to iTunes needed to reference Music instead of iTunes. Less expected, one of the sound-effect scripts needed to change how it detected that the sound was done playing. And in the completely weird, the Contacts app started adding strange characters around some of the data it provides to the JavaScript engine.1

One more thing: the Speed Playback script is no longer necessary. Sped-up playback in iTunes was meant for speeding playback of podcasts. Podcasts now have their own app and that app includes the ability to speed podcast playback, making this script superfluous in Catalina.

You can still use the Speed Playback script on your music files, of course, if you enjoy hearing your favorite artist sound like a chipmunk. But the book is titled “astoundingly useful scripts”, not “moderately amusing scripts” despite my glee in getting Fiona to quote Hunter Thompson in the voices script. I decided to remove Speed Playback and replace it with something both astoundingly useful and moderately amusing. I’ll post the code for Speed Playback in a few weeks.

Older posts