Terminal Alias to Toggle Widgets
I mentioned in my last post about preferring a terminal alias for toggling Sonoma's desktop widgets.
I got a message asking for more details about that alias setup, so here's the relevant alias (two, actually) from my .zshrc file:
First, a function that toggles a defaults boolean value:
function toggleDefaultsBoolean() { domain="$1" key="$2" current_value=$(defaults read "$domain" "$key") if [[ "$current_value" == "0" ]]; then defaults write "$domain" "$key" -bool 'true' else defaults write "$domain" "$key" -bool 'false' fi}
Then, using that in my widgets
alias to acutally toggle the widget visibility:
alias widgets="toggleDefaultsBoolean com.apple.WindowManager StandardHideWidgets && killall WindowManager"
I split the toggling into a separate function for quick re-use. If I wanted to have a terminal alias to toggle hidden files in the folder, it would be very similar1:
alias hiddenfiles="toggleDefaultsBoolean com.apple.Finder AppleShowAllFiles && killall Finder"
I use ohmyzsh for my ZSH configuration. It's an easy and useful upgrade if you spend any time in the terminal on your Mac.
Though I'd be the first to admit that this one doens't feel as elegant since you have to kill Finder to take effect. Thankfully on a modern Mac, just use the keyboard shortcut cmd-shift-. to toggle hidden file visibility. ↩