Search code examples
applescript

what means "tab group 1" in AppleScript, why not "tab group 2"?


I'm trying to understand code belong, and can't find anything explain what means "tab group 1".. And I don't know how to debug to find the value with "tab group 1" Btw, I test "tab group 0",its ok, but "tab group 2" its error..

set devices to {}

tell application "System Preferences"
    reveal pane "声音"
end tell

tell application "System Events"
    tell application process "System Preferences"
        repeat until exists window "声音"
        end repeat
        
        tell tab group 1 of window "声音"
            get properties
            click radio button "输出"
            tell table 1 of scroll area 1
                set selected_row to (first UI element whose selected is true)
                set currentOutput to value of text field 1 of selected_row as text
                
                repeat with r in rows
                    try
                        set deviceName to value of text field 1 of r as text
                        set end of devices to deviceName
                    end try
                end repeat
            end tell
        end tell
    end tell
end tell

if application "System Preferences" is running then
    tell application "System Preferences" to quit
end if

set text item delimiters to "‡"

set devicesStr to devices as text

set comm to "bash ./main.sh" & " \"" & devicesStr & "\"" & " \"" & currentOutput & "\"" & " output"


log comm

#do shell script comm

Solution

  • AppleScript GUI scripting involves working its way down through the view hierarchy of the application's windows. In this case, 'Tab Group [X]' means that there are at least [X] tab groups within the container at that level of the hierarchy, and you need to determine which one contains the lower-level element you're trying to access. Unfortunately, the elements of the view hierarchy aren't always immediately visible (there may be 'hidden' containers and such), and the hierarchy may change significantly from one app update to the next. That can be headache inducing.

    You can debug this manually (with a little patience) by working your way down the hierarchy yourself until you find the elements you need, using a series of every UI element of.. commands. I.e., begin with:

    tell application "System Events"
        tell application process "System Preferences"
            tell window "声音"
                get every UI element
            end tell
        end tell
    end tell
    

    Then choose a likely UI element from the list it produces and add a new tell block. However, it's easier to use the Accessibility Inspector app, which gives you a look into the details of any applications view hierarchy. Accessibility Inspector is included with Xcode downloads (which is free, and worth having around); I don't know if there's a place to download it separately.

    See Apple's Guide of Testing Accessibility.