Getting the Current Sound Output with AppleScript

I've seen scripts to toggle the sound output, but I'm having trouble customizing it.


Below is what I'm trying, and it's not working. Clearly I'm having trouble getting the name current sound source into a variable so the script can decide accordingly. What am I missing?


Thank you!


tell application "System Preferences"

reveal anchor "output" of pane id "com.apple.preference.sound"

activate

tell application "System Events"

tell process "System Preferences"

set currentSource to (text field 1 of the selected row of table 1 of scroll area 1 of tab group 1 of window "sound")

if currentSource is "FastTrack Pro"

select (row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" whose value of text field 1 is "SAMSUNG")

else if currentSource is "SAMSUNG" then

select (row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" whose value of text field 1 is "FastTrack Pro")

end if

end tell

end tell


#quit

end tell

iMac (27-inch, Late 2012), OS X Mountain Lion

Posted on Jul 30, 2013 5:18 AM

Reply
5 replies

Jul 30, 2013 12:11 PM in response to HaPsantran

Hi,



tell application "System Preferences"

reveal anchor "output" of pane id "com.apple.preference.sound"


activate

tell application "System Events"

tell process "System Preferences"

tell window "Sound"

tell tab group 1

tell scroll area 1

tell table 1

set selected_row to (first UI element whose selected is true)

end tell

end tell

end tell

end tell

end tell

end tell

end tell


Jul 30, 2013 11:49 AM in response to kelhak1

This helped me in terms of getting the value rather than the object of the field itself. However, I'm still having trouble getting the row of the currently selected sound source.


Additionally, it would be great to know if there's an attribute I can just name (such as selectedSoundSource) rather than have to use GUI scripting to accomplish this.


Thanks!

Jul 30, 2013 12:45 PM in response to kelhak1

BTW, you should separate the tell block:



kelhak1 wrote:


Hi,



tell application "System Preferences"

reveal anchor "output" of pane id "com.apple.preference.sound"


activate

end tell

tell application "System Events"

tell process "System Preferences"

tell window "Sound"

tell tab group 1

tell scroll area 1

tell table 1

set selected_row to (first UI element whose selected is true)


properties of selected_row

end tell

end tell

end tell

end tell

end tell

end tell

Jul 30, 2013 12:53 PM in response to kelhak1

Edited:



tell application "System Preferences"

reveal anchor "output" of pane id "com.apple.preference.sound"


activate

end tell

tell application "System Events"

tell process "System Preferences"

tell window "Sound"

tell tab group 1

tell scroll area 1

tell table 1

set selected_row to (first UI element whose selected is true)

end tell

end tell

end tell

end tell

end tell

set device_name to (value of text field 1 of selected_row)

end tell


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Getting the Current Sound Output with AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.