Applescript 选择声音输出在打开声音首选项屏幕时停止

问题描述 投票:0回答:1

目标:使用Applescript选择“Office”扬声器。

操作系统:Mac Sonoma 14.3.1

代码:

tell application "System Settings"
    activate
    reveal pane id "com.apple.Sound-Settings.extension"
end tell

tell application "System Events"
    tell process "System Preferences"
        repeat until exists window "Sound"
            delay 0.5
        end repeat
        
        tell window "Sound"
            repeat until exists tab group 1
                delay 0.5
            end repeat
            
            tell tab group 1
                click radio button "Output"
                
                repeat until exists table 1 of scroll area 1
                    delay 0.5
                end repeat
                
                tell table 1 of scroll area 1
                    -- Get the position of the target row
                    set targetRowPosition to position of row 1 where value of static text 1 is "Office"
                    
                    -- Click on the target row
                    click targetRowPosition
                end tell
                
            end tell
        end tell
    end tell
end tell

相当令人沮丧的结果:打开首选项声音窗口然后继续运行......就是这样。不选择输入或输出,不从音频源列表中选择“Office”

automation applescript macos-sonoma
1个回答
0
投票

由于我使用的是没有“系统设置”的旧Mac,并且没有任何外部扬声器,因此我将提供适用于我的系统的功能,以从声音的音效中选择警报声音。您应该能够将其映射到您的设置和输出。

tell application "System Preferences"
    activate
    set ankor to anchor "effects" of pane id "com.apple.preference.sound" of application "System Preferences"
    reveal ankor
end tell

tell application "System Events"
    tell process "System Preferences"
        
        set spk to "Glass" -- desired alert sound
        
        -- collect the higher level ui elements in a single variable
        set t1 to table 1 of scroll area 1 of tab group 1 of window "Sound"
        
        -- list of table elements to select sound from
        set rowRef to rows of t1
        
        -- cycle through list of rows and compare each row's `value of text field 1`
        repeat with ear in rowRef
            if value of text field 1 of ear is equal to spk then
                set matchingItem to contents of ear -- get row number of matching value
                exit repeat
            end if
        end repeat
        
        select matchingItem
        --> row 6 of table 1 of scroll area 1 of tab group 1 of window "Sound" of application process "System Preferences" of application "System Events"
    end tell
end tell

现在您已经获得了创建更有针对性的脚本所需的结果:

tell application "System Preferences"
    activate
    set ankor to anchor "effects" of pane id "com.apple.preference.sound" of application "System Preferences"
    reveal ankor
end tell

tell application "System Events"
    tell process "System Preferences"
        set glassAlert to row 6 of table 1 of scroll area 1 of tab group 1 of window "Sound" of application process "System Preferences" of application "System Events"
        
        select glassAlert
    end tell
end tell

如果您发现语法与更现代的操作系统版本不同,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.