Álvaro Ramírez
22 November 2024 Toggle macOS menu bar from you know where
I'm a fan of macOS's auto-hide menu bar setting. Unless I'm reaching out to a menu item, I don't typically need to have a visible menu bar, so I set auto-hide to "Always".
On rare occasions, I turn this setting off (say for a screen grab). While reaching out to macOS Control Center is OK, I wanted to quickly toggle it from the comfort of Emacs via M-x
fuzzy search.
We can leverage AppleScript to toggle the setting on and off. In the past, I would write shell scripts for this kinda thing and invoke from the command line. These days, I dump them into a dwim-shell-command and quickly forget about its implementation. From then on, I just M-x and fuzzy search for some magical incantation (I'm looking at you ffmpeg). I got a bunch of these things…
(defun dwim-shell-commands-macos-toggle-menu-bar-autohide () "Toggle macOS menu bar auto-hide." (interactive) (dwim-shell-command-on-marked-files "Toggle menu bar auto-hide." "current_status=$(osascript -e 'tell application \"System Events\" to get autohide menu bar of dock preferences') if [ \"$current_status\" = \"true\" ]; then osascript -e 'tell application \"System Events\" to set autohide menu bar of dock preferences to false' echo \"Auto-hide disabled.\" else osascript -e 'tell application \"System Events\" to set autohide menu bar of dock preferences to true' echo \"Auto-hide enabled.\" fi" :utils "osascript" :silent-success t))
…and that's all there is to it.