Tuesday, June 18, 2019

#1 2019-04-15 06:12:31 pm

emendelson
Member
Registered: 2008-10-31
Posts: 376

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?

Offline

 

#2 2019-04-16 11:36:42 pm

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 80

Re: Show sound volume in menu bar via AppleScript or command?

From the commandline, I found the domain for the user defaults preferences that governs menu extra items:

defaults read com.apple.systemuiserver

This contains about 15 or so keys, one of which is:

"NSStatusItem Visible com.apple.menuextra.volume" = 1;

Naturally, I tried:

defaults write com.apple.systemuiserver "NSStatusItem Visible com.apple.menuextra.volume" -bool false

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):

"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"
    );

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:

Applescript:


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

Offline

 

#3 2019-04-17 08:28:29 am

emendelson
Member
Registered: 2008-10-31
Posts: 376

Re: Show sound volume in menu bar via AppleScript or command?

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!

Offline

 

#4 2019-04-22 04:02:38 pm

emendelson
Member
Registered: 2008-10-31
Posts: 376

Re: Show sound volume in menu bar via AppleScript or command?

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!

Offline

 

#5 2019-04-29 10:39:38 am

ldicroce
Member
Registered: 2017-11-25
Posts: 107

Re: Show sound volume in menu bar via AppleScript or command?

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

Applescript:

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 .....

Last edited by ldicroce (2019-04-29 11:10:06 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)