From the commandline, I found the domain for the user defaults preferences that governs menu extra items:
[format]defaults read com.apple.systemuiserver[/format]
This contains about 15 or so keys, one of which is:
[format]“NSStatusItem Visible com.apple.menuextra.volume” = 1;[/format]
Naturally, I tried:
[format]defaults write com.apple.systemuiserver “NSStatusItem Visible com.apple.menuextra.volume” -bool false[/format]
which set the key to a zero (false) value, but even after killing the SystemUIServer process, the volume icon remained visible, and the value of the key have been restored to unit (true) value.
I’ll leave that with you to see if you can discern what I have overlooked. It may be useful to know the additional two keys exist in the same domain (where my system has visible in the menu extra bar the corresponding icons for each system UI element seen below in the menuExtras array):
[format]“NSStatusItem Preferred Position com.apple.menuextra.volume”
menuExtras = (
“/System/Library/CoreServices/Menu Extras/Bluetooth.menu”,
“/System/Library/CoreServices/Menu Extras/User.menu”,
“/System/Library/CoreServices/Menu Extras/AirPort.menu”,
“/System/Library/CoreServices/Menu Extras/Clock.menu”,
“/System/Library/CoreServices/Menu Extras/Battery.menu”,
“/System/Library/CoreServices/Menu Extras/Volume.menu”
);[/format]
I didn’t edit those two keys, as I assumed the setting of its visibility ought to have been sufficient.
However, here’s an AppleScript that, in my view, typifies one of the few examples where UI scripting can solve a problem without creating more of its own. It only manages to be a graceful script to execute by the nature of how System Preferences is completely happy to remain invisible whilst still providing full accessibility to its UI:
use Prefs : application "System Preferences"
use sys : application "System Events"
property pane : a reference to pane id "com.apple.preference.sound"
property anchor : a reference to anchor "effects" of my pane
property process : a reference to application process "System Preferences"
property window : a reference to window "Sound" of my process
property MenuExtraVolume : "Show volume in menu bar"
property checkbox : a reference to checkbox MenuExtraVolume of my window
Q(1)
if |?|(my process, no) = false then return Q(0)
reveal my anchor
if |?|(my checkbox, yes) = false then return Q(0)
click my checkbox
Q(1)
on |?|(x, |ξ| as boolean)
local x, |ξ|
repeat 20 times
if |ξ| = (x exists) then return true
delay 0.2
end repeat
false
end |?|
on Q(e as boolean)
quit Prefs
e
end Q
The virtue of a UI that remains hidden but accessible means this script will execute quite happily in the background, and you’ll not find any jarring disturbances taking place on your screen. The menu extra volume icon will simply appear, if previously it wasn’t available; or disappear, if it was; that is, the script currently acts to toggle the visibility of the volume icon.
The other benefit of the hidden UI is that the script ought to be very stable, and not be susceptible to the weaknesses inherent in other UI scripts where a user or another application causing disturbance to the interface during a script’s execution would generate an error in the script. This won’t happen here.
To warn other uses, this script, however, will only be likely to work on High Sierra. If it does work on Mojave, that’s fortunate. If not, it could undoubtedly be tweaked very simply to operate on Mojave. As it currently stands, it is also limited to English-language systems.
AppleScript: 2.7
Operating System: macOS 10.13