Terminal/Osascript: Automatically hide and show menu bar - shell

Is there a way to enable "Automatically hide and show menu bar" checkbox via applscript/osascript or just terminal?
Current OS: macOS Mojave 10.14
I read things like LSUIPresentationMode and tried different things with osascript already

This AppleScript code should work for you
if application "System Preferences" is running then quit application "System Preferences"
repeat until application "System Preferences" is not running
delay 0.1
end repeat
tell application "System Preferences" to reveal pane id "com.apple.preference.general"
tell application "System Events" to tell process "System Preferences" to tell window "General"
repeat while not (exists of checkbox "Automatically hide and show the menu bar")
delay 0.1
end repeat
click checkbox "Automatically hide and show the menu bar"
end tell
quit application "System Preferences"
As per the request from a comment to my answer for the original post, this following code is a conversion from AppleScript to shell script… and can be run in the Terminal.app and should produce the same results
printf 'aWYgYXBwbGljYXRpb24gIlN5c3RlbSBQcmVmZXJlbmNlc
yIgaXMgcnVubmluZyB0aGVuIHF1aXQgYXBwbGljYXRpb24gIlN5c3RlbSBQcmVmZ
XJlbmNlcyIKcmVwZWF0IHVudGlsIGFwcGxpY2F0aW9uICJTeXN0ZW0gUHJlZmVyZ
W5jZXMiIGlzIG5vdCBydW5uaW5nCiAgICBkZWxheSAwLjEKZW5kIHJlcGVhdAp0Z
WxsIGFwcGxpY2F0aW9uICJTeXN0ZW0gUHJlZmVyZW5jZXMiIHRvIHJldmVhbCBw
YW5lIGlkICJjb20uYXBwbGUucHJlZmVyZW5jZS5nZW5lcmFsIgoKdGVsbCBhcHBsa
WNhdGlvbiAiU3lzdGVtIEV2ZW50cyIgdG8gdGVsbCBwcm9jZXNzICJTeXN0ZW0gUH
JlZmVyZW5jZXMiIHRvIHRlbGwgd2luZG93ICJHZW5lcmFsIgogICAgcmVwZWF0IH
doaWxlIG5vdCAoZXhpc3RzIG9mIGNoZWNrYm94ICJBdXRvbWF0aWNhbGx5IGhp
ZGUgYW5kIHNob3cgdGhlIG1lbnUgYmFyIikKICAgICAgICBkZWxheSAwLjEKICAgI
GVuZCByZXBlYXQKICAgIGNsaWNrIGNoZWNrYm94ICJBdXRvbWF0aWNhbGx5IG
hpZGUgYW5kIHNob3cgdGhlIG1lbnUgYmFyIgplbmQgdGVsbAoKcXVpdCBhcHBsa
WNhdGlvbiAiU3lzdGVtIFByZWZlcmVuY2VzIiA='|base64 -D|osascript

Code can be shorter on "MacOS Catalina" #2019-12 with "AppleScript Editor"
tell application "System Preferences" to reveal pane id "com.apple.preference.general"
tell application "System Events" to tell process "System Preferences" to tell window "General"
click checkbox "Automatically hide and show the menu bar"
end tell
quit application "System Preferences"
and you can run it from command line direct
printf 'tell application "System Preferences" to reveal pane id "com.apple.preference.general"\n tell application "System Events" to tell process "System Preferences" to tell window "General"\n click checkbox "Automatically hide and show the menu bar"\n end tell\n quit application "System Preferences"' | osascript
or from file
osascript {{script_name}}.scpt
thx #wch1zpink
https://stackoverflow.com/a/53437866/1347601

If anyone is trying to do this in Monterey, I was able to modify the terminal command from #wch1zpink, and it works great.
printf 'tell application "System Preferences" to reveal pane id "com.apple.preference.dock"\n tell application "System Events" to tell process "System Preferences" to tell window "Dock & Menu Bar"\n click checkbox "Automatically hide and show the menu bar in full screen"\n end tell\n quit application "System Preferences"' | osascript

Related

Toggle HDR mode in macOS 12

tell application "System Preferences"
set the current pane to pane id "com.apple.preference.displays"
tell application "System Events"
tell window 1 of application process "System Preferences"
click checkbox "High Dynamic Range" of window "Displays" of application process "System Preferences" of application "System Events"
end tell
end tell
I'm using macOS 12.
It's not working. Help me please.
macos12
Thank you #Zade.
I solved it to use the UI Browser.
on run
activate application "System Preferences"
tell application "System Events"
tell process "System Preferences"
click button "Displays" of scroll area 1 of window "System Preferences"
delay 2
click checkbox "High Dynamic Range, Automatically adjust the display to show high dynamic range content." of group 1 of window "Displays"
end tell
delay 1
quit application "System Preferences"
end tell
end run

Applescript to turn mirroring on and select iPad

I recently updated my mac to Monterey and my old applescript is not working anymore and I get this error: 'System Events got an error: Can’t get pop up button 1 of window 1 of process "System Preferences". Invalid index.'
I honestly have no idea which part should I change. Thank you in advance.
tell application "System Preferences"
set current pane to pane "com.apple.preference.displays"
activate
end tell
tell application "System Events"
tell process "System Preferences"
click pop up button 1 of window 1
click menu item 1 of menu 1 of pop up button 1 of window 1
end tell
end tell
tell application "System Preferences"
delay 10
quit
end tell
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
activate
end tell
tell application "System Events"
tell application process "System Preferences"
click pop up button "Add Display" of window "Displays"
click menu item 2 of menu 1 of pop up button "Add Display" of window "Displays"
end tell
end tell
tell application "System Preferences"
delay 5
quit
end tell

Show Bluetooth / Volume in menu bar Apple Script

I have a working script that I would like to see if there is a way to hide or simply have the script run in the background. The current working script physically activates the pane thus the user is seeing it happen in front of them.
tell application "System Preferences"
activate
set the current pane to pane "Bluetooth"
delay 1
end tell
tell application "System Events"
tell process "System Preferences"
set toggleBluetooth to the checkbox "Show Bluetooth in menu bar" of the window "Bluetooth"
click toggleBluetooth
end tell
end tell
I've also found a way to turn on certain things instantly and was wondering if there was a way to take this and make it work with the bluetooth and volume.
tell application "Finder"
tell Finder preferences
set desktop shows hard disks to true
end tell
end tell
This following AppleScript code can be used to toggle the Bluetooth menu bar item on and off, without bringing System Preferences to the front.
if application "System Preferences" is running then
do shell script "killall 'System Preferences'"
repeat until application "System Preferences" is not running
delay 0.1
end repeat
end if
tell application "System Preferences"
reveal anchor "Main" of pane id "com.apple.preferences.Bluetooth"
end tell
tell application "System Events"
repeat until checkbox "Show Bluetooth in menu bar" of window "Bluetooth" of application process "System Preferences" exists
delay 0.1
end repeat
click checkbox "Show Bluetooth in menu bar" of window "Bluetooth" of application process "System Preferences"
end tell
tell application "System Preferences" to quit
And this will do the exact same thing for the volume icon in the menu bar
if application "System Preferences" is running then
do shell script "killall 'System Preferences'"
repeat until application "System Preferences" is not running
delay 0.1
end repeat
end if
tell application "System Preferences"
reveal anchor "output" of pane id "com.apple.preference.sound"
end tell
tell application "System Events"
repeat until checkbox "Show volume in menu bar" of window "Sound" of application process "System Preferences" exists
delay 0.1
end repeat
click checkbox "Show volume in menu bar" of window "Sound" of application process "System Preferences"
end tell
tell application "System Preferences" to quit

How to toggle "reduce motion" preference with AppleScript?

I'd like to be able to toggle the macOS "reduce motion" preference (system preferences > accessibility > display > display > reduce motion) via the touchbar analogous to this article about toggling dark mode Does anyone have an idea on how to toggle it with AppleScript? I googled around a bit, but came up empty handed.
This AppleScript code works for me using the latest version of macOS Catalina
if application "System Preferences" is running then ¬
do shell script "killall 'System Preferences'"
repeat until application "System Preferences" is not running
delay 0.1
end repeat
tell application "System Preferences"
reveal anchor "Seeing_Display" of ¬
pane id "com.apple.preference.universalaccess"
end tell
tell application "System Events"
repeat until exists of checkbox "Reduce motion" of tab group 1 of ¬
group 1 of window "Accessibility" of application process "System Preferences"
delay 0.1
end repeat
click checkbox "Reduce motion" of tab group 1 of ¬
group 1 of window "Accessibility" of application process "System Preferences"
end tell
tell application "System Preferences" to quit

Osascript/Applescript: Uncheck "Displays have separate Spaces"

Standard Configuration of Mojave 10.14 in Mission Control is, that "Displays use separate spaces" is checked.
I want it unchecked...
Is there a way to do this with applescript/osascript?
This is what i tried but its not clicking on the checkbox..
if application "System Preferences" is running then quit application
"System Preferences"
repeat until application "System Preferences" is not running
delay 0.1
end repeat
tell application "System Preferences" to reveal pane id "com.apple.preference.expose"
tell application "System Events" to tell process "System Preferences" to
tell window "Mission Control"
repeat while not (exists of checkbox "Displays have separate Spaces")
delay 0.1
end repeat
click checkbox "Displays have separate Spaces"
end tell
quit application "System Preferences"
When running your code, it stays in the repeat while not ... loop because the target checkbox is not directly under the window but a part of a group.
Adding to tell group 2 to to tell window "Mission Control" fixes it.
Change:
tell application "System Events" to tell process "System Preferences" to tell window "Mission Control"
To:
tell application "System Events" to tell process "System Preferences" to tell window "Mission Control" to tell group 2
Note: When checking/unchecking the Displays have separate Spaces checkbox, it requires a logout for the change to take effect.

Resources