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

The major difference between a thing that might go wrong and a thing that cannot possibly go wrong is that when a thing that cannot possibly go wrong goes wrong it usually turns out to be impossible to get at and repair. — Douglas Adams (Mostly Harmless)

Rainbow Magazine preflight tool enhanced—Wednesday, May 4th, 2022
The Dragon’s Gold

I’ve been silently updating the Rainbow preflight tool (Zip file, 3.2 KB) over the last couple of years, but I just added a feature that I thought worth talking about. I was typing in Charles Husak’s “Quest of the Dragon’s Gold” from the August 1984 Rainbow and the line numbers are all over the place. But between pages 32 and 36 the line number jumps from 2790 to 2840, which seemed excessively odd even for this particular program.

I did a search on the obvious line numbers that might exist to see if there were any GOTOs, GOSUBs, or so on, to the potentially missing section, and didn’t find any. But given how uneven the line numbers were, that wasn’t a great check. It occurred to me that I could have the rainbow script keep track of (a) all of the line numbers in the listing, and (b) all of the line numbers referenced in the code, and then cross reference them.

This was fairly simple to do: there’s a subroutine to collect the line numbers, and a loop that provides a warning for every line number that doesn’t exist. Here’s the subroutine:

[toggle code]

  • sub collectLineReferences {
    • my $lineNumber = shift;
    • my $lineText = shift;
    • $existingLines[$#existingLines+1] = $lineNumber;
    • while ($lineText =~ m/(GO *TO|GO *SUB|THEN) *([0-9]+)/g) {
      • $lineReferences[$#lineReferences+1] = [$lineNumber, $1, $2];
    • }
  • }

And here are the warnings:

[toggle code]

  • #verify that all line number references go to a line that exists
  • %existingLines = map { $_ => 1 } @existingLines;
  • foreach $lineReference (@lineReferences) {
    • ($lineNumber, $referenceType, $referencedLine) = @$lineReference;
    • if (!exists($existingLines{$referencedLine})) {
      • print STDERR "\t$referenceType to non-existent line $referencedLine in $lineNumber\n";
    • }
  • }
Holy, Holy! Lord God Almighty!—Wednesday, April 13th, 2022
Early in the morning (Holy, Holy!)

Easter is coming, and Holy, Holy, Lord God Almighty, while not specifically an Easter Day song, is perfect for that morning sunrise. Reginald Heber wrote the lyrics specifically for Trinity Sunday, the first Sunday after Pentecost. The Easter season officially ends on Pentecost, after which we return to Ordinary Time; the first Sunday in which we return to Ordinary Time is Trinity Sunday, the Solemnity of the Most Holy Trinity.

Which is when we’re most likely to sing this song at Mass.

Holy, Holy is a very beautiful song, both its lyrics and its rousing melody. It’s the melody, of course, that I’m going to reproduce using the piano script.

Here are the full lyrics from Gospel Hymns Combined, an 1879 collection of hymns:

    • Holy, Holy, Holy! Lord God Almighty!
    • Early in the morning our song shall rise to Thee;
    • Holy, Holy, Holy! Merciful and Mighty!
    • God in three Persons, blessed Trinity!
    • Holy, Holy, Holy! all the saints adore Thee,
    • Casting down their golden crowns around the glassy sea;
    • Cherubim and Seraphim falling down before Thee,
    • Which wert and art, and evermore shall be.
    • Holy, Holy, Holy! tho’ the darkness hide Thee,
    • Though the eye of sinful man Thy glory may not see,
    • Only Thou art Holy, there is none beside Thee,
    • Perfect in pow’r, in love, and purity.
    • Holy, Holy, Holy! Lord God Almighty!
    • All Thy works shall praise Thy name in earth and sky, and sea;
    • Holy, Holy, Holy! Merciful and Mighty!
    • God in three Persons, blessed Trinity!
    • Amen.

Modern hymnals often change these lyrics, which I’ll talk about later on my blog, but these are the longest-running lyrics and the most inspirational.

Reginald Heber’s religion wasn’t Catholicism; he was an Anglican. But the songs he wrote, especially this one, have spread throughout the hymnals of Christianity. In the text comparison I used while researching the lyrics, you can find Methodist hymnals, Lutheran hymnals, Presbyterian, Baptist, and Moravian hymnals.

Place a QR code over an image in macOS—Wednesday, March 23rd, 2022
QR codes on the macOS command line

You can caption an image with more than just text. I was looking at a QR code the other day and wondered, how hard would it be to create one of my own for my postcard project? So I created this command-line script (Zip file, 4.0 KB) to take an image and some text, and create a QR image from it. Having done that, it was a relatively simple step to layer the QR code over an existing image from my Mac.

I usually just drag a photo from the Photos app onto the command line to generate a captioned image or a QR-coded image.

imagefilepath to image to overlay QR code on
texttext to encode into QR square
--align <alignments>align QR code to top, bottom, right, left, center, right-center, or left-center
--aztecuse Aztec code generator
--bgcolor <color>QR code background color (formats: r,g,b,[f]; FFFFFF[FF]; grey)
--fgcolor <color>QR code foreground color (formats: r,g,b,[f]; FFFFFF[FF]; grey)
--helpprint this help and exit
--level <level>set QR code correction level low, medium, quartile, or high
--opacity <0-100>set the opacity on the background image
--ratio <10-90>ratio of QR code to image size; current: 25.0
--savefilename to save as
--width <size>create an image this many pixels wide

QR codes can be created on their own with just the text that should be encoded. I created the bare QR code for this blog post using:

  • qr "“Be cautious, but be adventurous and the rewards will be tremendous.”—James S. Coan, Basic FORTRAN, p. 83" --save caution.png

The image can be saved as anything; be aware that if you don’t specify a file to save as, it will save it as “QR.png”, and it will erase any existing “QR.png”.

I created the image for this blog post using:

  • qr https://hoboes.com/qr keyboard.jpg --save keyboardQR.jpg --align left

The code can be aligned vertically and horizontally. Most of the alignments are self-explanatory; left-center and right-center align horizontally to the center of the left half of the image or to the right half of the image, respectively.

Alignments make no difference if you’re creating a QR code on its own, without a background image.

Have yourself a musical command line…—Wednesday, December 22nd, 2021
Merry scriptmas

Merry Christmas! It’s time for another programming toy under your weekend scripter Christmas tree. And what could be more Christmasy than a script that doesn’t just play music but is music?

If you’ve read 42 Astounding Scripts (and if you haven’t, the ebook is on sale until the end of the year) you’re familiar with the “shebang line”, that pound-exclamation line at the top of every script. It usually looks like #!/usr/bin/something. It tells the computer what scripting language interpreter should run this script.1

A scripting language interpreter is nothing more than a command-line program that accepts files as input and interprets them as code to do something. When you put a shebang line at the top of a file and mark that file as executable, you’re telling the computer that whenever you run this script you want it to run the script file through the interpreter named in the shebang line.

If you put #!/usr/bin/python at the top of the script file, that means it should be interpreted as Python code, by running whatever program is at /usr/bin/python. If you put #!/usr/bin/perl, it should be interpreted as Perl code, by running whatever program is at /usr/bin/perl.

But that’s all the shebang line is doing: it’s telling the computer that this file must go through the interpreter at that path. The interpreter interprets the code in the file, nothing more than that. As long as the program specified in the shebang can handle getting the file as input, it will work. This is why most scripting languages, even those that use other characters for comments such as // or /* … */, also accept the pound symbol for comments: so that they won’t be confused by the shebang line if they’re called as a shell scripting language. Even AppleScript handles pound signs as comments, specifically for that reason.2

The piano script from 42 Astounding Scripts can accept files of notes as input. It can ignore lines beginning with hashes. The piano script can be used as a shebang line interpreter.

Older posts