An Automation Golden Age

Did you know I have a newsletter? I post some, but not all of my newsletter’s content to this blog. Here’a a recent one.

An Automation Golden Age

I’ve mentioned several times on my podcasts that we’re experiencing a renaissance in automation, particularly on the Mac. This shift isn’t driven by a single tool but rather by the interoperability of a collection of tools.

AppleScript has been available on the Mac for decades, offering significant automation opportunities if you want to learn it. AppleScript allows users to connect applications and work with the same data to accomplish unified tasks. However, for many, learning AppleScript was a challenge. Programmers found it too different from traditional programming languages, and non-programmers struggled with its syntax. As a result, AppleScript adoption remained relatively small.

Apple and Sal Soghoian introduced Automator in early 2005 to address this, bringing drag-and-drop automation with its original version. Meanwhile, tools like Keyboard Maestro and Hazel, developed outside of Apple, have been actively filling the gaps in Apple’s automation solutions for years.

Then came Shortcuts ( Workflow). Initially developed for iOS, Shortcuts is now firmly embedded in the Mac ecosystem. It’s a spiritual (if not direct) descendant of Automator, and in recent years, these tools have learned to work together. You can run Keyboard Maestro macros from Shortcuts, and Shortcuts can be triggered from within Hazel. Users can now mix and match these tools to create robust automation chains, combining the strengths of each.

For those willing to invest the time to master—or at least gain a working knowledge of—these tools, few tasks on the Mac can’t be automated today.

The next big shift in this process is the integration of artificial intelligence. AI is already proving useful in helping generate automation, but if Apple Intelligence can fully tap into user data while still protecting user privacy and integrate it with Shortcuts, we could see a new era of powerful, personalized automation. This leap could be as significant as the jump from AppleScript to Automator. Of course, this depends on Apple getting both Apple Intelligence and the integration right, but I suspect this is already on the big whiteboard in Cupertino.

Shortcuts and Apple Intelligence both use the Intents system to work their magic. Developers who build for Shortcuts benefit from Apple Intelligence and vice versa. With this common architecture, I believe Apple will eventually tighten the connections between Shortcuts and Apple Intelligence. It won’t happen overnight, but over the coming years, I expect this combination to become the next frontier of automation in the Apple ecosystem.

AppleScript to Toggle Desktop Widgets

I’m a fan of the new Sonoma Desktop Widgets. However, I also make a living making screencasts and having all those widgets on screen can sometimes be a pain. There is a setting you can toggle under the Desktop & Dock settings:

The problem with this, of course, is that it’s a pain in the neck to get there. Who wants to do all that mousing and clicking? I wanted to automate this button so I can run a script that sets me up for screencasting and, among other things, turns off Desktop Widgets. So here’s a script that does just that. This was built with help from the AppleScript Sensei himself, Sal Soghoian. Below is a link to the zipped script file, as well as the script in plain text. I run it as part of a Keyboard Maestro macro that I’ve tied to a keyboard trigger. Enjoy.


tell application "System Settings"
	activate
	reveal anchor "Widgets" of pane id "com.apple.Desktop-Settings.extension"

	repeat
		set currentPane to get current pane
		if currentPane is pane id "com.apple.Desktop-Settings.extension" then
			exit repeat
		else
			delay 1
		end if
	end repeat
end tell

delay 1

tell application "System Events"
	tell process "System Settings"
		set targetControl to checkbox "Show Widgets" of group 6 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
		click targetControl
	end tell
end tell

tell application "System Settings" to quit

AI-Generated AppleScript

I enjoyed this article from Dr. Drang about the robot-created AppleScript. I think AppleScript will be one of the most difficult languages for AI models to write because it was created to make it more human-readable, and that makes it quirky.

The other thing about AppleScript that will likely trip up the AI models (it certainly trips me up) is the modular nature of the language. Every app that implements AppleScript uses its own dictionary calls. From one app to another, these dictionaries vary greatly, and every script involving a new app requires a bit of spelunking.

Good luck with AppleScript, Robots, you’ll need it.

Using AppleScript to Open a Specific Mailbox in Apple Mail (MacSparky Labs)

Shortcuts for iOS 16 is adding a feature that lets you open a specific Mailbox in Apple Mail on your iPhone or iPad. Shortcuts for Ventura, however, doesn’t have that function. What gives? Never fear. Sparky figured out a way to duplicate the feature using AppleScript…

This is a post for MacSparky Labs Level 3 (Early Access) and Level 2 (Backstage) Members only. Care to join? Or perhaps do you need to sign in?

Using Keyboard Maestro and AppleScript to Eject External Drives

I’ve been using a laptop + monitor setup for a while now, and I like it more than I expected. The last time I tried this, I had lots of problems getting the Mac and monitor to talk to each other when I’d plug it in. I prefer to keep the laptop closed when doing this (often called “clamshell” mode), and for years macOS did not like that.

Maybe it is the Apple silicon switchover or improvements to macOS, but that’s no longer a problem. I plug it in and start typing.

However, another friction point is disconnecting. I have a Time Machine and external storage drive connected to my Mac when attached to the monitor. It’s important that I properly dismount those drives before unplugging. (Just yanking a drive out of your Mac is a terrible idea.)

So … dismounting. There are several ways to pull it off. You could go in the Finder and eject the drive under “Locations” in the Sidebar. There is a handy little Eject button next to each removable drive. (In my case, Dagobah is my Time Machine Drive, and Batuu is my extra storage.) This solution works but is tedious.

There are also some excellent apps to solve this problem. My favorite is EjectBar. This is a menu bar app that, once you click it, lets you eject from the menu bar, saving you a trip to the Finder. A power tip with EjectBar is to Command-Click the menu bar icon, and it automatically ejects all connected drives.

I wanted, however, something more automated. So I made a simple AppleScript that ejects all external drives. Here’s the script:

try
tell application "Finder"
eject the disks
display dialog “Successfully ejected disks.” buttons {“Close”} default button “Close”
end tell
on error
display dialog “Unable to eject all disks.” buttons {“Close”} default button “Close”
end try

As AppleScripts go, this one is pretty basic.

I run this script as part of a Keyboard Maestro Macro. First, it quits all applications. I do this because I have very particular screen setups when I connect to the big monitor. I prefer to close everything out when disconnecting. (I have separate scripts for setting up screens on the laptop screen or the monitor when I plug back in.) Then I eject the drives by running the above AppleScript. I use the Hyper Key plus Q to activate it (⇧⌃⌥⌘Q). I’ve also mapped it to a button on my Stream Deck, which is how I usually activate it. This is the method that has stuck with me. When I’m ready to take the laptop, I push the button, wait a moment, and then unplug and head out. It really isn’t much trouble at all.

Here’s a download link to the Keyboard Maestro Macro.

If you’d like to get better at Keyboard Maestro, I’m putting up a $5 discount code on the Keyboard Maestro Field Guide for one week. Use code KMLAPTOPWARRIOR. 🙂

And here’s a screenshot of the Macro:

Script Debugger 8

It’s a sad commentary on my AppleScripting lately that I just realized ScriptDebugger released version 8 several months ago. Script Debugger is the AppleScript tool that Apple should have released. It’s been in development for 26 years, and it continues to impress.
The most significant change in Script Developer 8 is is support for macOS Big Sur and M1 Macs. Script Debugger 8 runs natively on M1 Macs and generates native standalone script applications for M1 and Intel Macs.
Other new features in Script Debugger 8 include support for Dark Mode, display themes, a reimagined facility for generating standalone script applications that supports code signing and notarizing.
My favorite feature continues to be its fantastic dictionary support. Anytime I hit a thorny problem with AppleScript, the ScriptDebugger dictionaries come to my rescue.
Script Debugger offers a fully functional 20-day free trial. Script Debugger switches to “Lite” mode when the free trial ends, where some of Script Debugger’s advanced features are disabled. Just using the free Lite mode, Script Debugger is far superior to Apple’s Script Editor.

Updates and Improvements to the Salutation AppleScript for Apple Mail

My relationship with Apple Mail isn’t love so much as familiarity. However, every time I think about walking out on Apple Mail, I think about our long and complicated relationship, and how much work I’ve put into it with things like my salutation AppleScript.

The Updated Script

Using a combination of AppleScript and TextExpander, I’m able to automatically insert an email salutation by typing “xhi”. This is really useful and something I run multiple times a day. The original post and explanation on how it works is still good. However, the script has altered a bit. You can download the updated version, called “xnm” (“X Name” in my head) here. I suggest running it as an embedded TextExpander snippet so you can combine it with a variety salutations like “Hi”, “Hello”, “Dear”, “Hey”, and whatever else floats your boat. Also, you can download the TextExpander group with both the “xnm” and “xhi” snippets here. That should be all you need to get this rolling.

But What About Multiple Recipients?

Reader Mark Bramhill had that exact question and came up with two variations of this script. The first will make a salutation for everyone in the “To:” field. If you regularly (or occasionally) send email to multiple recipients, this variant is for you.

Download Multiple Recipients AppleScript

That, however, wasn’t enough for Mark. He made a separate AppleScript that counts the number of recipients. If there is just one recipient, it inserts that person’s name. If there is more than one (including looking at the cc: field), the script instead writes “Y’all”. I thought both of these scripts were pretty clever.

Download the “Y’all” Script

Language Specificity

I also had several emails from non-english speakers about the script not working. The trouble is in this line:

tell text field "To:" of window 1

As you can see, the script is language-specific. In English, the script looks for the “To:” field, but in other languages, that field has a different name. In Dutch, for instance, it’s “Aan:”. If your native language isn’t English, you’ll need to fix that line appropriately.

I do love how this script has taken on a life of its own. This isn’t a testament to anything particularly bright on my part but the utility of a script that auto-salutes every email you send. This feature is so useful that I’m shocked it isn’t baked in to all email applications.

The AppleScript Email Extractor

I spent several hours putting together a super-nerdy Keyboard Maestro script that saves an Apple Mail message as a PDF and then uses some AppleScript to pull data out of the message and use it in the file name. I’ve already added the video, the Keyboard Maestro Script, and the AppleScript to the Keyboard Maestro Field Guide. Below you can watch the video and download the AppleScript here.