11

I am brand new here and I have an additional clarification or need additional help for an issue from this thread - Applescript: "can't get tab group 1 of window" (El Capitan)

In my preference pane, in addition to Headphones and Digital out, I also have 2 apple monitors and would like the option of selecting one of them as out put (namely the first one in the list).

Basically I need help with adjust his solution to be able to select the third row or first monitor as sound output. Or a way to accomplish this without knowing how to edit AppleScript for a different setup.

2
  • Welcome to Ask Different. I've linked this to the other question. Could you edit your post to show what you have? Perhaps adding a screen image of the preference pane. I'll add an answer that doesn't require any sort of AppleScript learning if that's the issue.
    – bmike
    Nov 25, 2015 at 21:43

5 Answers 5

11

With a little experimentation on Arthur Hammer's script I discovered that the lines set deviceselected to "xyz" appear to be just visual feedback whilst in Script Editor & don't seem to be part of the actual functionality.

The actual switching appears to be just between 2 chosen lines in the Control Panel; so, if you just need to switch between any 2 outputs, then you can just use the row numbers.

In this example I've lifted them out to be variables, so you don't need to delve in the script to change the required lines…

As you have more than 2 outputs, the first time you switch it will not necessarily go from/to the correct device, but will after that.
For example, if you wanted to switch between outputs 3 & 4, yet 2 was currently selected, the first change would be from "not 3" as opposed to "is 3" [hope you understand that convoluted explanation]

(*
Applescript to toggle between two sound outputs by Line number, ¬
as they appear in the Sound Control Panel. Based on code by ¬
Arthur Hammer http://apple.stackexchange.com/a/209434/85275
*)

set outputA to 3 --change this to the actual 'line number' of your first desired output
set outputB to 4 --change this to the actual 'line number' of your second desired output
--the rest of the script will use these vales as a switch

tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.sound"
end tell


tell application "System Events"
    tell application process "System Preferences"
        repeat until exists tab group 1 of window "Sound"
        end repeat
        tell tab group 1 of window "Sound"
            click radio button "Output"
            if (selected of row outputA of table 1 of scroll area 1) then
                set selected of row outputB of table 1 of scroll area 1 to true
            else
                set selected of row outputA of table 1 of scroll area 1 to true
            end if
        end tell
    end tell
end tell
--tell application "System Preferences" to quit
--remove the comment '--' tag above to make the control panel quit afterwards, leave for testing.

Maybe a little pictorial clarification - using the above script with outputA = 3 & outputB = 4, I can now switch between Digital Out & TonePort UX2.

One more note - the line numbers do not have to be consecutive, I could just as easily switch between Internal Speakers & TonePort by using lines 1 & 4.

enter image description here

2
  • 1
    Note that if you want to select AirPlay devices, the separation line count as one item.
    – fharper
    Jan 3, 2019 at 22:14
  • Worked beautifully. I used Alfred workflow with a shortcut key of Shift + Ctrl + Cmd + S. Thanks so much!
    – ssppjj
    May 12, 2022 at 2:24
4

@Tetsujin - thanks for this solution. Works perfectly. I'm using it for switching between my built in audio (for when I'm coding for work) and my Universal Audio Apollo card (for when I'm doing music production). Since the UAudio device also provides a superior input mic option, I added the obvious input switch as well after your "Output" section:

    click radio button "Input"
    if (selected of row outputA of table 1 of scroll area 1) then
       set selected of row outputB of table 1 of scroll area 1 to true
    else
       set selected of row outputA of table 1 of scroll area 1 to true
    end if
2

In Mojave, I usually manually switch the output device from the Volume Control in the menu, so I automate the same thing using the AppleScript below which does the toggling based on what is checked/unchecked.

If you use this w/ some launchers, then this approach doesn't need permissions to the Preferences.

ignoring application responses
    tell application "System Events" to tell process "SystemUIServer"
        click menu bar item 5 of menu bar 1 -- May need to change the index as needed
    end tell
end ignoring

-- Optional, but just to avoid the 5-6sec delay
delay 0.25
do shell script "killall System\\ Events"

tell application "System Events" to tell process "SystemUIServer"
    tell menu bar item 5 of menu bar 1
        set device1 to menu item "Name of Device 1" of menu 1
        set device2 to menu item "Name of Device 2" of menu 1

    if (value of attribute "AXMenuItemMarkChar" of device1) as string is "✓" then
        click device2
    else
        click device1
    end if

    end tell
end tell
1

I'm not able to guess as to a general AppleScript, but I do like and use Automator to record the steps needed.

Open that tool and make a new workflow. Then press the record button. You can then record the sequence used to open System Preferences, select the pane you wish, select the audio you want and then end recording.

You can usually look over the actions and trim them or add delays if needed using Automator and you won't need to know how to program the rows and items in Applescript.

3
  • I have tried that multiple times without success. Doesn't seem to work under El Capitan. The AppleScript solution from the other thread works very well but seems focused for just the 2 options of Headphones or Digital output. I feel sure there is a relatively simple revision to the script to make it work.
    – JRTemp
    Nov 27, 2015 at 1:07
  • I still need a resolve and have no way to contact Arthur Hammer who had a wonderful solution in the referenced thread. Mr. Arthur Hammer, I hope you stumble across this and can help!
    – JRTemp
    Nov 29, 2015 at 23:21
  • @bmike - I tweaked Arthur's script to give a workable solution for n outputs. It's a bit manual but pretty simple to tweak.
    – Tetsujin
    Dec 6, 2015 at 12:50
1

guys! That script below was prepared by me and it does not care about how many devices you actually have. It will work in any case:

tell application "System Preferences" to ¬
    reveal anchor "output" of pane id "com.apple.preference.sound"

tell application "System Events"
    tell application process "System Preferences"
        repeat until exists tab group 1 of window "Sound"
            delay 0.1
        end repeat
        
        tell tab group 1 of window "Sound"
            set DevicesCount to count rows of table 1 of scroll area 1
            repeat with n from 0 to DevicesCount
                if (selected of row n of table 1 of scroll area 1) then
                    set SelectedDevice to n
                end if
            end repeat
            if (SelectedDevice < DevicesCount) then
                set NewDevice to SelectedDevice + 1
                set selected of row NewDevice of table 1 of scroll area 1 to true
            else
                set selected of row 1 of table 1 of scroll area 1 to true
            end if
            
        end tell
    end tell
end tell

quit application "System Preferences"
4
  • Uh... perhaps a little more description on what "it will work" means. What exactly does this script take and input? what action does it perform? thanks! May 3, 2022 at 16:44
  • Thanks for your question. It is just opens Sound options in System Preferences, then count how many output devices you have. After it it make selected next device. If selected device was last - first one will be chosen. So this is how we can just iterate over any amount of devices, and not care about how many of them we have
    – Sloneg
    May 5, 2022 at 7:03
  • In other words it will give you an option to select next output device by iteration. All examples that i found just selects device with specific id. In case if number of devices will change (just connect Bluetooth headset for example) my script will still work, others no
    – Sloneg
    May 5, 2022 at 7:22
  • Exactly! I have a script that will select the n-th output device as listed, but it fails if the list changes. I want a script that select the device name "AirPod Max" no matter where this item occurs in the list. But I cannot figure out where to input "AirPods Max" into your script. Indeed I cannot tell how your script knows which row to stop on! Do you keep calling your script and checking the clipboard? I just don't get it! May 7, 2022 at 17:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .