Show Bluetooth / Volume in menu bar Apple Script - bash

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

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

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.

Terminal/Osascript: Automatically hide and show menu bar

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

Applescript Mojave Toggle Accessibility Grayscale On/Off

I have a script I run periodically to toggle grayscale on/off with Applescript. It runs fine on High Sierra but throw an exception when I use it was Mojave.
tell application "System Preferences"
reveal anchor "Seeing_Display" of pane id "com.apple.preference.universalaccess"
end tell
tell application "System Events" to tell process "System Preferences"
delay 0.5 # Needed or else script fails
set theCheckbox to checkbox "Use grayscale" of window "Accessibility"
tell theCheckbox
# If the checkbox is not checked, check it to turn grayscale on
if not (its value as boolean) then
set checked to true
click theCheckbox
else # else turn grayscale off
set checked to false
click theCheckbox
end if
end tell
end tell
tell application "System Preferences"
quit
end tell
The exception is:
System Events got an error: Can’t get checkbox "Use grayscale" of window "Accessibility" of process "System Preferences". (-1728)
Does Mojave still support Applescript/Would anyone know how to fix this?
As of Catalina, updated to:
# quit system preferences if running
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
end if
# wait for system preferences to quit
with timeout of 10 seconds
repeat while running of application "System Preferences" is true
delay 0.01
end repeat
end timeout
# switch to correct anchor
tell application "System Preferences" to reveal anchor "Seeing_ColorFilters" of pane "Accessibility"
#tell application "System Preferences" to activate
#get the name of every anchor of pane "Accessibility" of application "System Preferences"
tell application "System Events" to tell process "System Preferences" to tell window "Accessibility"
#return UI elements of tab group 1 of group 1
with timeout of 10 seconds
repeat until exists checkbox "Enable Color Filters" of tab group 1 of group 1
delay 0.01
end repeat
end timeout
click the checkbox "Enable Color Filters" of tab group 1 of group 1
end tell
tell application "System Preferences" to quit
This works for me Using OS Mojave
tell application "System Preferences"
reveal anchor "Seeing_Display" of pane id "com.apple.preference.universalaccess"
end tell
tell application "System Events" to tell process "System Preferences"
repeat while not (exists of checkbox "Use grayscale" of group 1 of window "Accessibility")
delay 0.1
end repeat
set theCheckbox to checkbox "Use grayscale" of group 1 of window "Accessibility"
tell theCheckbox
# If the checkbox is not checked, check it to turn grayscale on
if not (its value as boolean) then
set checked to true
click theCheckbox
else # else turn grayscale off
set checked to false
click theCheckbox
end if
end tell
end tell
tell application "System Preferences"
quit
end tell

Resources