42 Astoundingly Useful Scripts and Automations for the Macintosh

Have yourself a musical command line…

…Make your scripting gay. From now on your errors will be miles away!

Jerry Stratton, December 22, 2021

Merry scriptmas: Merry Scriptmas! Let the bells ring!; Christmas

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.

To create a musical script, treat the notes file just as you would any other script you create: edit it as text, put the shebang at the top, mark it as executable… the edit script will do most of that for you just as for any other script you write.

Do you want to be able to type “v” on the command line and get the famous notes from Beethoven’s Fifth Symphony? Or “amen” to get the classic hymn ending notes? Obviously, you could write a script that then calls the piano script with the appropriate notes. But the shebang can do this for you, too. Here’s the standard hymn amen as a script:

[toggle code]

  • edit amen
  • #!/Users/USER/bin/piano
  • # common closing Amen melody
  • # Jerry Stratton, astoundingscripts.com
  • --key E-
  • #treble
  • 1  ["E A" | "E G"]
  • #bass
  • - "-A C" | "E B"

What about the infamous Beethoven line?

  • edit v
  • #!/Users/USER/bin/piano
  • #Beethoven’s famous fifth opening
  • #Jerry Stratton astoundingscripts.com
  • --key E-
  • --tempo 108
  • R 12
  • [- G G G 2 v+++ --E]
  • [-- G G G 2 v+++ --E]
  • [--- G G G 2 v+++ --E]
  • G G G 2 v+++ --E
  • 4 R

In each of these examples, if you’re not using the edit script to create new scripts, ignore that line and enter these as you would any other script you’re creating.3

Once you’ve typed these scripts in, you can use them as you would any other script, putting it at the end of a chain of scripts or using it as an alarm in another script. Or just typing it on the command line for a quick musical jolt.

  • sleep 5;v
  • backupFiles;amen
  • amen;amen;amen

But this is a Christmas post. How about a little Christmas cheer for your command line?

[toggle code]

  • edit bells
  • --key C
  • --tempo 200
  • [-- 1 C | C         | C              | C ]
  • E E 2 E | 4 E E 2 E | 4 E G -C. 8 -D | 4 E</p>
  • 1 R

That’s a nice, simple, Jingle Bells opener with one melody line and one bass line. But we can do better than that.

[toggle code]

  • edit bells
  • #!/Users/USER/bin/piano
  • #Jingle Bells intro
  • #Jerry Stratton astoundingscripts.com
  • --key D
  • --tempo 200
  • # bass
  • [4 -- D R D A          | D R D A            | D R D R               ]
  • # second bass
  • [4 -- D D 8 D +A +B +A | 4 D D 8 D +A +B +A | 4 D 8 D D 4 D 8 +A +A ]
  • # third bass
  • [4 -- D +A 2 D         | 4 D +A 2 D          | 4 D +A D +A          | 1 D ]
  • # harmony
  • [4 F 8 F F 2 F         | 4 F 8 A A 4 B A     | F 8 A A 4 -D E       | 1 F ]
  • # melody
  • 4 F F 2 F             | 4 F F 2 F           | 4 F A -D E           | 1 F

There are no new techniques in the music here that I haven’t already introduced in previous articles about the piano script. The only new technique is letting the shebang line handle the work of sending the notes to piano. Instead of typing something like this:

  • piano --key C --tempo 200 \[-- 1 C C C C ] E E 2 E 4 E E 2 E 4 E G -C. 8 -D 4 E

…or even worse, trying to type the more complex melody on the command line, you can just type bells and press return.

And you can do this with any melody you want, long or short, simple or complex. Have fun with your new toy, and Merry Christmas!

  1. Technically, it tells the shell what scripting language interpreter to run. The shell is not the computer, it just appears that way if you spend a lot of time on it!

  2. Swift does not accept pound signs as comments. Like C, Swift uses the pound sign for compilation conditions. Because Swift is also meant to be used as a scripting language, it ignores the pound sign plus an exclamation, but only it appears on the first line.

  3. From the book, here’s how you create and edit a script file from the command line if you don’t have the edit script:

    mkdir -p ~/bin
    touch ~/bin/amen
    chmod u+x ~/bin/amen
    open -t ~/bin/amen
    

    And, from there, type the script, starting with the shebang line.

  1. <- Star-Spangled Banner
  2. Holy, Holy! ->