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

Anyone who can count from 0 to 7 on his or her fingers and make 8 can learn to be a programmer. The business is not difficult; it is just tricky. — Peter Laurie (The Joy of Computers)

Using version control with AppleScripts—Wednesday, July 21st, 2021
Save Clipboard changes in Mercurial

Tracking an AppleScript in Mercurial.

In 42 Astoundingly Useful Scripts and Automations for the Macintosh I devoted one diversion to the importance of version control. One thing I left unmentioned except through omission is that AppleScripts created with Script Editor (as most will be, due to Script Editor’s verification and testing ability) can’t really be tracked in version control. They aren’t text files, so while changes will be noted, what those changes are will not. Change tracking pretty much requires line-oriented text files, and Script Editor files are not text files, at least in any sense meaningful to Mercurial or Git.

I have a lot of AppleScripts in my User Scripts folder for several applications, as well as a handful of AppleScripts in my Finder window toolbars and my Favorites folder. I’ve always been disappointed that I can’t track changes to them in Mercurial. And I’ve always been worried that I don’t have a backup of them in easily-readable text format.

While working on this problem, I noticed that I have a lot of scripts I’ll never read again because the format is no longer valid on macOS.

I’ve considered, and occasionally tried, keeping two copies of every script I write: when I’m done editing a script in Script Editor, copy the text to a text file and save that as a readable, future-proofed version. It always works for a very short period and then I forget, and the two sets of scripts get out of sync. As I was writing this post, I discovered an abandoned .hg folder in my user scripts folder, last touched on September 20, 2014.

It occurred to me while writing the Save Clipboard script that there are so many commands beginning with osa there must be one for getting the text of an AppleScript out of a .scpt file or .app folder.1 And to ask the question is to answer it: osadecompile does exactly that. This makes it trivial to write a script that keeps the two locations—the live location and the text repository—in sync. The text backup can then be tracked easily in any version control system, including Mercurial.

Why should everyone learn to program?—Wednesday, July 14th, 2021
Adapting to computers

Programming for all” is a catch-all term I use for anything about programming that I think is relevant to anyone using a computer. Mostly, I use it for simple scripts such as those in 42 Astoundingly Useful Scripts and Automations for the Macintosh. But there’s a deeper meaning more appropriate to modern life, which is that everyone uses a computer almost nonstop, even if you don’t own a desktop computer, a tablet, or a smart phone. Every time you interact with the larger world, there’s a computer involved. It’s how you get billed, it’s how you leave messages for people, it’s how the lights at the intersection work and how the votes get counted on election day.

If your refrigerator has a water filter, it uses a computer to tell you when it wants you to replace the filter, your car almost certainly has a computer that controls whether your airbag will deploy in an accident, and your washing machine likely has a computer that determines whether you’re allowed to open the lid.

Every time you find out you missed an important post in some social media platform you use, it’s because a computer algorithm did not display it in your feed—and that’s because a programmer decided on that algorithm. Every time you’re asked to reduce your electricity usage, or are forced to endure rolling blackouts, it is because decades ago some programmers used a computer model to argue that unless we switched from reliable, stored energy sources to unreliable, intermittent sources, catastrophic weather changes would ensue.

Computer programming and computer models affect every aspect of our lives, personally and politically. They can even shut down a thriving economy for a year. Understanding the nature of programming is essential to modern life.

When I first got into computers in the late seventies, there was an assumption in the books and magazines I read that schools would soon be required to teach everyone programming. From Larry Gonick’s warning1 that “In the computer age, everyone will be required by law to memorize the powers of two, up to 210” to Peter Laurie’s prediction2 that secretaries would evolve into mini-programmers, writing “small programs in Pascal or BASIC” on the server to manage the office, the assumption was that well into the future if you wanted to use a computer you’d have to write your own software.

The Star-Spangled Banner in MIDI—Wednesday, June 30th, 2021
Flag behind the ramparts of Fort McHenry

This is not the large flag, but it is a large flag flying over Fort McHenry, viewed from “over the ramparts”.

Everyone knows the story. Francis Scott Key, negotiating the release of American prisoners taken by the British, watches the British bombardment of Fort McHenry from one of the British ships. Fort McHenry’s flag was the John Hancock of flags: a signature bigger than any other, “a flag so large that the British would have no difficulty seeing it”.

It was so big that the fort had two flags: the large one, and a smaller one for normal use better able to withstand the fickle east coast weather. It was probably also easier to lower and raise. From what we know, they were flying the smaller one at the beginning of the fight. After the bombardment, in the dark of the morning, Major George Armistead ordered the big one raised for the British to see. It was an act of defiance deserving of a song. Key obliged. After seeing the great flag rippling in the sea wind over Fort McHenry at dawn, Key was inspired to write a four-stanza poem in honor of the bold gesture, The Defense of Fort M’Henry.

    • O! say can you see by the dawn’s early light,
    • ⁠What so proudly we hailed at the twilight’s last gleaming,
    • Whose broad stripes and bright stars through the perilous fight,
    • ⁠O’er the ramparts we watch’d, were so gallantly streaming?
    • And the Rockets’ red glare, the Bombs bursting in air,
    • Gave proof through the night that our Flag was still there;
    • O! say does that star-spangled Banner yet wave,
    • O’er the Land of the free and the home of the brave?
    • On the shore dimly seen through the mists of the deep,
    • ⁠Where the foe’s haughty host in dread silence reposes,
    • What is that which the breeze, o’er the towering steep,
    • ⁠As it fitfully blows, half conceals, half discloses?
    • Now it catches the gleam of the morning’s first beam,
    • In full glory reflected now shines on the stream,
    • ‘Tis the star-spangled banner, O! long may it wave
    • O’er the land of the free and the home of the brave.
    • And where is that band who so vauntingly swore
    • ⁠That the havoc of war and the battle’s confusion,
    • A home and a country should leave us no more?
    • ⁠Their blood has washed out their foul footsteps pollution.
    • No refuge could save the hireling and slave,
    • From the terror of flight, or the gloom of the grave,
    • And the star-spangled banner in triumph doth wave,
    • O’er the Land of the Free and the Home of the Brave.
Washington Monument flags

There’s something stirring about a flag at dusk or dawn.

It’s beautiful poetry, and like Amazing Grace is an example of how beautiful music can come from flawed people. On the one hand, Key is not as flawed as some of the era. He fought a decade-long fight to free 400 slaves in a contested will, defended individual slaves seeking their freedom, and publicly criticized the cruelty of slavery. But he also owned slaves himself and opposed full abolition of slavery. As District Attorney in DC he used the power of government to prosecute abolitionists on the grounds that according freedom of speech to abolitionists encouraged insurrection.

There’s a lesson for us there, today.

On the other, other hand, the furor over the phrase “hireling and slave” is a modern one.1 “Slave” is a half-anachronistic term nowadays. We see it in times past and think only of chattel slavery—and only one kind of chattel slavery—yet we still use the phrase today for many other meanings. It held these meanings well back in and before Key’s time, at least back to Shakespeare’s.

Big Sur and Astounding Scripts—Saturday, June 12th, 2021
Big Sur coastline

Just a quick note: I upgraded to Big Sur a few months ago. The scripts appear to need no changes from the current edition of the book.

I may still publish a new edition of the book, but the only change would be the boilerplate that mentions what operating system the book is for. Instead of referencing “macOS Catalina” on the indicia page, it would reference “macOS Big Sur”. Everything else would remain the same.

And I may not do this, because of the ever-present potential of meeting new bugs in either the Smashwords or Amazon review process when uploading a new file. In programming, “if it works, don’t fix it” is often very good advice, especially when dealing with what are essentially black boxes where it is difficult to know how the input affect the output.

In fact, having written that, I have convinced myself to not upload a new version with just the boilerplate changed. I’ll save that effort for when Monterey comes out.

Older posts