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

Solving problems is a practical art, like swimming, or skiing, or playing the piano: you can learn it only by imitation and practice. — George Polya (Mathematical Discovery)

Adding parenthetical asides to photograph titles on macOS—Wednesday, February 12th, 2020
Mustang sales photos

Sadly, the only photos I have of my old Mustang are of getting ready to sell it. Which doesn’t merit a “Mustang” keyword.

In 42 Astoundingly Useful Scripts and Automations for the Macintosh I have a script for changing the titles of all selected photographs to a sequential title. That is, “Yosemite — 1”, “Yosemite — 2”, “Yosemite — 3” and so on. I use this regularly after coming home from a vacation. But recently I’ve had a need for leaving the title as is but adding a parenthetical. So that if I have a collection of photos that I didn’t take, I might want to add “ (photo by dad)” to the end of each, changing, for example, “Flag in the Snow” to “Flag in the Snow (photo by dad)”.

This is a much simpler task. It doesn’t require sorting the photos, because it isn’t adding a sequential number to the title. Nor does it require appending a different text (the sequence number) to each photo. The same text is appended to all photos.

Except that it makes no sense to change, for example, “Jerry at Adams Avenue Street Fair 2003” to “Jerry at Adams Avenue Street Fair 2003 (Adams Avenue Street Fair)”. So the script does check to make sure that the text being added does not already exist in the title.

[toggle code]

  • -- add a parenthetical to selected photo titles
  • -- Jerry Stratton astoundingscripts.com
  • tell application "Photos"
    • set selectedPhotos to selection
    • set photoCount to count of selectedPhotos
    • --change this number if you regularly change more than 50 photos at a time
    • if photoCount > 50 then
      • display alert "There are " & photoCount & " photos selected." buttons {"Change", "Cancel"} cancel button "Cancel"
    • end if
    • set baseParenthetical to the text returned of (display dialog "Text for parenthetical" default answer "")
    • set parenthetical to " (" & baseParenthetical & ")"
    • repeat with selectedPhoto in selectedPhotos
      • if the name of selectedPhoto does not contain baseParenthetical then
        • set the name of selectedPhoto to name of selectedPhoto & parenthetical
      • end if
    • end repeat
    • say "Finished!"
  • end tell
Hello World in Amber—Wednesday, January 22nd, 2020
Hello World

I considered this for the cover, but the seventies amber was too over the top even for me.

  • ~/bin/asciiArt face.png --width 120 --palette "hello world" --sequence 0.8 --lighten 3.9 --save hello --bgcolor .995,.621,.037 --fgcolor 1,1,1

The amber I used is the color of many of the screens from the era. And many of those that were just white on black, you could buy screen overlays to make them be amber (or green). The excuse was that it was easier on the eyes, but I think the real reason is that amber and green made their screens look more like people thought a computer ought to look.

We still have that today: one of the most popular Terminal color schemes is green on black. It’s what I eventually went with for the Astounding Scripts cover.

But this amber ASCII art does highlight another feature of the ASCII art script in 42 Astoundingly Useful Scripts and Automations for the Macintosh: the ability to wash out an image, to brighten it, so as to highlight the areas with the most contrast. The original image had an off white fireplace and a window in the background. Lightening helped remove that, but it also helps when making an image like this to use a photo editor, such as GIMP or GraphicCoverter, to remove everything around the focus of the image. In the end I used the magic wand in GraphicConverter to get rid of the background. The lightening got rid of the lines on my face, highlighting the silly Incredibles Kleenex mask, hair, and shirt.

The argument --lighten 3.9 multiplies every color in the image by 3.9; the argument --sequence 0.8 tells the script to not only use the palette in order (so that it spells “hello world” instead of assumes that those letters are in order by density) but to cut off the sequence when the combined grayscale exceeds 0.8. If you look at the script itself, you’ll see that the default cutoff is 1.0; since there is no way for a greyscale to exceed 1.0 (colors range from 0.0 through 1.0), normally there is no cutoff at all.

Technically, there’s no need to have both a sequence cutoff and a lightening. Providing a low sequence cutoff should have the same effect as providing a high --lighten. I found it easier to conceive of a lightening of the image than a reduction of the cutoff, however. Partly this is because if the lightening brings the greyscale above 1, I have the script set it to 1, so that a non-sequential palette continues to work. A greyscale above 1 would fail, because the palette is normalized to range from 0.0 to 1.0. This also allows for inverting the greyscale value by subtracting it from 1.0; this would make no sense if greyscale values could exceed 1.0.

Rainbow Magazine BASIC program preflight tool—Wednesday, January 8th, 2020
Vicious Vic by Jay R. Hoggins

Vicious Vic appears to be a variation on Robot War, with vikings in place of robots.

There are a lot of typing mistakes I make that are easily detectable: extra characters from fat fingers, mistyped parentheses, and even mistyped numbers. In an attempt to make typing BASIC programs from The Rainbow easier, as well as improve my ability to use Textastic with rcheck, I wrote this script. I wrote it somewhat haphazardly, adding new checks when I noticed that an error was detectable.

The basic idea is that since each line wraps at 32 characters, when I hit the end of the line in the Rainbow listing, I hit return. The script detects whether a line in the text file is a continuation of the previous line or a new line by looking for the line number. This solves the problem of finding a text editor that wraps at 32 characters.

But it also opens the possibility of doing some preflighting. If a line in the text file is not the final subline in a BASIC code line, it must be 32 characters long. If it isn’t, I mistyped something. And with my fat fingers, this is the most common typo. Most typos are discovered merely by making sure that each subline except the final one is exactly 32 characters.

There are also warnings, things that are probably a problem but might not be. These do not halt the program, and are displayed at the bottom of the results. They are not piped to a file, so they appear in the terminal if you’re piping.

For example, if the full BASIC line is longer than 249 characters, I probably mistyped something. That’s the maximum number of characters you can type on a new BASIC line on the Color Computer. It’s a warning, not an error, because it is possible to get more characters into a line, and it occasionally happens with the one-line and two-line contest winners, since they’re specifically trying to stuff as much into a line as possible. More than 255 characters is an error. I haven’t seen anything longer than that.1

It also checks the line number. If it isn’t greater than the previous line number, I mistyped something.2

And it checks for other things that I’ve noticed are detectable. If there aren’t the same number of close parentheses as open parentheses in the full BASIC line, that’s a problem.3 It looks for mistyped and missing colons, where possible.

Have a Merry Scripting Christmas with Persistence of Vision—Wednesday, December 25th, 2019

Some of the best Christmas mornings I remember are mornings when I holed myself away and built things. On earlier Christmases, it could have been Legos, or off-brand erector sets, or a model train. Later, it might have been building characters or adventures using the new Dungeons and Dragons Basic or Expert boxed set I found under the tree.

When sitting at the computer, every day was Christmas, and in many ways it still is. As you can see from the majority of this blog, I’m still finding joy in the ASCII art script. So why not a touch more joy on Christmas morning? Consider this my erector set gift to you.

Except for this image, all of the ASCII art created using the asciiArt script in 42 Astounding Scripts came from photographs or drawings. That is, a human hand was involved in its creation.

This Christmas image, on the other hand, was nearly completely scripted. Obviously, the version with Linus’s Bible quote over the Christmas scene came from the asciiArt script, but the Christmas scene itself came from the Persistence of Vision raytracer. The only hand-placed part of the image was the phrase “Merry Christmas” in the upper left.

Persistence of Vision is a lot of fun to play with, Christmas morning or any morning. Or, as was often the case for me (and still often is) late into the night.

Remember to install brew and then “brew install povray” as described in the bubble cake example if you haven’t already. To create an image from this POV-Ray script file, run the povray program on it, as you would any other command-line script or program.

  • $ povray Christmas.pov

This will create, by default, an 800 by 600 PNG image with the same filename as the .pov file but with the extension .png. In this case, if you name the file “Christmas.pov” as I did, the file will come out “Christmas.png”.

You can adjust the width and/or height using the Width= and Height= command line options.

Older posts