How to toggle "reduce motion" preference with AppleScript? - 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

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

Enable Voice Control. AppleScript

I try to create an Apple Script to Enable Voice Control in a Mac.
This is in: System Preferences > Accessibility > Voice Control > Enable Voice Control
I think I get close. But I do not know how to call the left menu "Voice control"
This is what I have tried and do not work:
tell application "System Events"
click checkbox "Enable Voice Control" of window "Voice Control" of window "Accessibility" of application process "System Preferences" of application "System Events"
end tell
You asked in the comments if there was another way to get the solution. Here's a way to revise your code. First, I will explain the solution, step by step.
Update: This if statement, as pointed out in user3439894's very helpful comments below, is actually necessary, for several reasons. It should always be included.
if running of application "System Preferences" then
quit application "System Preferences"
delay 1
end if
This is done so the script will not fail if it is running. Note that the user also pointed out that killall should be used to ensure that the process completely terminates.
It allows for predictability in the behavior of the application.
user3439894 also pointed out that I should add this code block, in case there is a timing issue, resulting in the application trying to reopen while it is in the process of closing.
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
Next, in this snippet, you click the Accessibility button in System Preferences
tell application "System Events"
click button "Accessibility" of scroll area 1 of window 1 of process "System Preferences" -- click the Accessibility button
delay 2 -- while loads
Next, you select the Voice Control row in Settings.
select row 13 of table 1 of scroll area 1 of window 1 of process "System Preferences"
delay 0.2 -- delay while page loads
And finally, click the checkbox. As I explained in the comments, groups are used to organize elements.
click checkbox "Enable Voice Control" of group 1 of window 1 of process "System Preferences"
end tell
Full (updated) code:
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
delay 0.1
end if
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
activate application "System Preferences"
tell application "System Events"
click button "Accessibility" of scroll area 1 of window 1 of process "System Preferences" -- click the Accessibility button
delay 2.0 -- delay while page loads
select row 13 of table 1 of scroll area 1 of window 1 of process "System Preferences"
delay 0.2 -- delay while page loads
click checkbox "Enable Voice Control" of group 1 of window 1 of process "System Preferences"
end tell
Corrections I made to your code:
You cannot reference elements from windows that are not already opened in an app (like you did with the checkbox "Enable Voice Control" of window "Voice Control" of window "Accessibility"), so first I opened the Accessibility window (click a button) -> Voice Control Window (select the row), and then found/clicked the checkbox.
Then, I looked at the hierarchy of the elements to figure out where the checkbox is in that Voice Control Window. I found out that it is inside group 1 of window 1 (Voice Control Window), so I clicked it.
I also added delays (the app needs time to load after every click/select).
Also, here is how I figured out where the elements are.
If you don't already use it, there is this built-in app called Accessibility Inspector to help you locate elements.
You can also use get in applescript. For example,
tell application "System Events"
get buttons of scroll area 1 of window 1 of process "System Preferences"
end tell
would return:
... button "Accessibility" of scroll area 1 of window "System Preferences" of application process "System Preferences" of application "System Events", button "Screen Time" of scroll area 1 of window "System Preferences" of application process "System Preferences" of application "System Events",...
Another example:
tell application "System Events"
get UI elements of window 1 of process "System Preferences"
end tell
and here's some of the output
...scroll area 1 of window "System Preferences" of application process "System Preferences" of application "System Events", toolbar 1 of window "System Preferences" of application process "System Preferences" of application "System Events..."
A very helpful tool.

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

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

Resources