How to AppleScript the launching of sound.profPane? - macos

I have this script
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.sound"
reveal anchor "Output" of pane id "com.apple.preference.sound"
end tell
The problem is that it first goes to the general configuration, with all those icons, only then, it focuses on sound. This create a quick and annoying flicker. I am only interested in the sound pane.
Is there nay way I can write a script that'll show JUST the sound pane without that flickering?
thanks!

You can just use the reveal command directly:
tell application "System Preferences"
activate
reveal anchor "output" of pane id "com.apple.preference.sound"
end tell
If you are using UI scripting, the activate command can also be left out:
tell application "System Preferences"
reveal anchor "output" of pane id "com.apple.preference.sound"
end tell
tell application "System Events" to tell process "System Preferences"
tell table 1 of scroll area 1 of tab group 1 of window 1
if selected of row 1 then
set selected of row 2 to true
else
set selected of row 1 to true
end if
end tell
end tell

Related

Using AppleScript to uncheck HDR in system preferences

I have a 4K HDR Monitor, and sometimes Macbook causes the colors to be washed, the fix is to Disable and then Re-Enable HDR.
I am trying to create an AppleScript to then incorporate that in Automator to do so.
I was able to get some traction but not able to identify how to do the actual uncheck and identify the group.
Here is what i had so far:
tell application "System Preferences"
activate
delay 2
set the current pane to pane id "com.apple.preference.displays"
delay 2
tell application "System Events"
click checkbox "High Dynamic Range" of group 2 of window "LG HDR 4K" of application process "System Preferences"
end tell
quit end tell
Here is the error I get:
error "System Events got an error: Can’t get group 2 of window \"LG HDR 4K\" of application process \"System Preferences\". Invalid index." number -1719 from group 2 of the window "LG HDR 4K" of application process "System Preferences"
here is a screenshot of the page I am trying to uncheck and re-check HDR:
Any advice would be appreciated, thank you.
I don't have a high-def display, so I don't see this particular option, but if I run the following code, I get a full list of all the subelements of the window:
tell application "System Preferences"
activate
delay 2
set the current pane to pane id "com.apple.preference.displays"
delay 2
tell application "System Events"
tell window 1 of application process "System Preferences"
entire contents
end tell
end tell
end tell
Subelements you are interested in seem to have the following form:
radio button "Scaled" of tab group 1 of window "Built-in Retina Display" of application process "System Preferences" of application "System Events"
Note that it includes a tab group 1 entry (referring to the fact that you are on the 'Display' tab of the four tabs available which is missing from your chain.
The Display pane UI layout has changed a bit in macOS Monterey, and now looks like this:
Display Prefs UI Layout
In my case I wanted to enable HDR on my second monitor, so using the following code I was able to get Script Editor to test whether the High Dynamic Range checkbox was true or false, and if false to enable it:
tell application "System Preferences"
activate
delay 1
set the current pane to pane id "com.apple.preference.displays"
delay 1
tell application "System Events"
click button "Display Settings…" of window "Displays" of application process "System Preferences" of application "System Events"
delay 1
tell sheet 1 of window 1 of application process "System Preferences"
select row 2 of outline 1 of scroll area 1
set theCheckbox to checkbox "High Dynamic Range, Automatically adjust the display to show high dynamic range content."
tell theCheckbox
set checkboxStatus to value of theCheckbox as boolean
if checkboxStatus is false then click theCheckbox
end tell
end tell
end tell
end tell
If I wanted to select a different monitor, I would change the index of "row 2" in this line:
select row 2 of outline 1 of scroll area 1 of sheet 1 of window "Displays" of application process "System Preferences" of application "System Events"

Applescript - Idein

I'm newbie to Appplescript. I need to automate certain actions on my computer related with my Bluetooth keyboards.
I want to be able to click on the remove or connect button of a keyboard in the following dialog window of the System Preferences Panel.
Dialog window
My code until this moment is as follows:
try
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell process "System Preferences"
click button "Set Up Bluetooth Keyboard…" of window "Keyboard"
end tell
end tell
tell application "System Events"
tell group 1 of window 1 of application process "System Preferences"
click button "remove" of "Home Keyboard"
end tell
end tell
end try
My problem is related with the remove button since is unidentified cell of an unidentified table. With unidentified, I mean without description. Maybe there is an easy solution, but I'm not able to find it. Furthermore, It could happen that more than one keyboard exists, so I need to identify the cell from the Keyboard name.
Do you know any hint related with this issue?
Thanks in advance
Here is a sample script I used to reconnect a specific mouse via Bluetooth :
tell application "System Events"
tell application "System Preferences"
activate
reveal anchor "MouseTab" of pane id "com.apple.preference.mouse"
end tell
tell application process "System Preferences"
click button "Configuration of Bluetooth mouse…" of window 1 -- see note 1
delay 1
select (first row of table 1 of scroll area 1 of sheet 1 of front window whose value of item 1 of static text of UI element 1 contains "Mouse") -- see note 1
get value of item 1 of static text of UI element 1 of row 2 of table 1 of scroll area 1 of sheet 1 of front window
click button "Done" of sheet 1 of front window -- see note 1
end tell
tell application "System Preferences" to quit
end tell
Note 1 : Be careful about the 3 lines with comment 'see note 1' : the value of the string may be different for your local language. Please adjust these 3 values.
I think for keyboard, concept should be very similar. Because it is using GUI scripting, if Apple changes the layout of Bluetooth screen preferences, it must be adjusted. This script works from Yosemite to ElCaptain.I can't test it for next systems.

change screen resolution with AppleScript

I am trying to click at radio buttons in Displays panel of System Prefernces, namely to change Screen resolution. This is the code I use to identify radio buttons:
tell application "System Preferences"
activate
reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
end tell
tell application "System Events"
tell application process "System Preferences"
set frontmost to true
get every radio button of window 0
--click button 1 of window 0 of application process "System Preferences" of application "System Events"
--click radio button "Scaled" of radio group of window "com.apple.preference.displays"
end tell
end tell
The radio buttons returned are none. Based on what I see, window has zero radio buttons. This leads to a conclusion that radio buttons are part of sub window, namely the Displays subwindow and not the main window. How can I navigate to this "subwindow" and click radiobuttons?
The radio buttons are part of the radio group. The radio group is part of the tab group.
Here's the script:
tell application "System Preferences"
activate
reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
end tell
tell application "System Events"
tell application process "System Preferences"
set frontmost to true
tell tab group 1 of window 1
click radio button 2 of radio group 1 -- "Scaled"
select row 2 of table 1 of scroll area 1 -- select the second row in the table to change the resolution of the monitor
end tell
end tell
end tell
For Mac OS 10.15, you'll need this instead.
Set 'q' to the display button preference you want (1-4)
set tabNum to q as number
tell application "System Preferences" to reveal pane "com.apple.preference.displays"
tell application "System Events" to tell process "System Preferences"
set activeWindow to window 1
repeat until exists activeWindow
end repeat
set tabGroup to tab group 1 of activeWindow
tell tabGroup to click radio button "Scaled"
set subGroup to group 1 of tabGroup
set radioGroup to radio group 1 of subGroup
tell radioGroup to click radio button tabNum
--log activeWindow
--delay 0.5
tell application "System Preferences"
quit
end tell
end tell

Applescript audio input/output set if device is present

I have this script which currently will set audio inputs/outputs to a plantronics headset, but if the device is not plugged in the script will error. I need it to return text saying
"Plug the device in"
if it isn't detected within the audio settings.
Here is the script:
tell application "System Preferences" to activate
tell application "System Preferences"
reveal anchor "input" of pane id "com.apple.preference.sound"
end tell
tell application "System Events" to tell process "System Preferences"
tell table 1 of scroll area 1 of tab group 1 of window 1
select (row 1 where value of text field 1 is "Plantronics C725")
end tell
end tell
tell application "System Preferences" to activate
tell application "System Preferences"
reveal anchor "output" of pane id "com.apple.preference.sound"
end tell
tell application "System Events" to tell process "System Preferences"
tell table 1 of scroll area 1 of tab group 1 of window 1
select (row 1 where value of text field 1 is "Plantronics C725")
end tell
end tell
You must include your instruction "select..." into a try/end try block. I assume that you just have to do it for the "input" tab, because if "input" is OK, then the headset is connected and output tab should also be OK.
Replace the instruction :
select (row 1 where value of text field 1 is "Plantronics C725")
by :
try
select (row 1 where value of text field 1 is "Plantronics C725")
on error
display alert "headset not connected !"
return
end try
You can also change your code to repeat in case of error, but if you have an issue with your headset, the script may repeat forever !

Syntax errors in AppleScript for Mavericks

I'm tring to select "Soundflower (2ch)" as my audio output using this script:
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.sound"
get the name of every anchor of pane id "com.apple.preference.sound"
reveal anchor "output" of current pane
select (row 1 of table 1 of scroll area 1 of tab group 2 current pane)
end tell
It gives the error :
Syntax Error: Expected "," but found number
for "row 1"
What is the correct format?
I also tried:
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.sound"
get the name of every anchor of pane id "com.apple.preference.sound"
reveal anchor "output" of current pane
select (row, 1 of table, 1 of "scroll area", 1 of "tab group", 2 of current pane)
end tell
but got
Syntax Error: Can’t get 1 of table. Access not allowed.
How do I do this?
Try:
tell application "System Preferences"
activate
set myPane to pane id "com.apple.preference.sound"
set the current pane to myPane
tell myPane
name of every anchor -- {"output", "input", "effects"}
reveal anchor "output"
end tell
end tell
-- Use this script as a wrapper for GUI Scripting statements when you are confident that GUI Scripting is available and enabled or that the user knows how to enable it if necessary
activate application "System Preferences"
tell application "System Events"
tell process "System Preferences"
select row 1 of table 1 of scroll area 1 of tab group 1 of window 1
end tell
end tell

Resources