Show sound volume in menu bar via AppleScript or command?

The Mac’s system preferences Sound panel includes an option to “Show volume in menu bar.” Is there a way to turn this on from a terminal or via AppleScript?

I ask because High Sierra always forgets this setting when restarting the machine (possibly other versions) and it would be convenient to set it as a startup item.

I see that the command

osascript -e “set Volume 5”

can set the sound volume. But is there a command that turns on the speaker icon in the menu bar?

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

This script is superb. Thank you for it! I’ve only tested it on High Sierra, but will test on Mojave soon and will report.

I wasn’t able to do anything more with com.apple.systemuiserver - but I’ll continue to experiment.

Again, a thousand thanks!

It seems not to work under Mojave, though this may be intermittent. I thought it worked the first time I tried it, but I couldn’t get it to work again. Will continue to search. Meanwhile, thank you for this!

Can this “trick” be applied to Messages?
I would like to send to myself a message regularly without interrupt my flow using this script:

tell application "Messages"
	send "This is an iMessage" to buddy "ldicroce@mac.com" of (service 1 whose service type is iMessage)
end tell

or to send an email using “Mail”…

Thanks !

EDITED:
I just realised that it actually stays in the background …