GNU Emacs is a text editor that famously takes a kitchen-sink approach to features and functionality. If you’re game to learn Emacs Lisp, it’s easy to add even more features yourself. I use it for most of my system- and programming-related tasks.
I finally fixed an issue that has been bugging me for a long time:
getting Emacs to track macOS’s Dark Mode setting and switch
automatically using LionyxML/auto-dark-emacs. It has always
resulted in AppleScript errors in the past — often the eloquent
AppleScript error 1
, an articulation of which Hemingway would have
been proud (and probalby Emacs’ fault, not the package’s). At one
time, I was able to work around the issue by starting Emacs from the
Terminal like some sort of peasant, but eventually, that stopped
working as well.
The problem is Emacs doesn’t by default have permissions to control System Events and is shy about asking, unlike typical macOS apps, which on install shamelessly ask for half a dozen different permissions with the frequency of a teenager asking for the car keys. What’s more, there doesn’t seem to be an easy way in Sequoia of going into System Settings and manually granting the privilege ahead of time. But piecing together bits of information folks have posted on various forums gave me a procedure I should have been able to come up with on my own:
-
Find the script in the code causing the error. In this case it was
tell application “System Events” tell appearance preferences if (dark mode) then return “true” else return “false” end if end tell end tell
-
Paste the script into Script Editor, make sure it works, and save it as a file. I chose
~/dark.scpt
-
Run the following in Emacs:
M-x shell-command RET osascript ~/dark.scpt
-
The following should pop up:
“Emacs” wants access to control “System Events”. Allowing control will provide access to documents and data in “System Events”, and to perform actions within that app.
-
Allow this permission.
auto-dark-mode
should now work properly
Now I can switch macOS to Dark Mode without leaving Emacs searing my
eyeballs like a blazing Tungsten filament while I once again puzzle
out the Emacs incantation to manually change its theme. I’m enjoying
the default themes auto-dark
uses as well;
fniessen/emacs-leuven-theme for light and
alexpana/emacs-wombat for dark.