applescript yosemite sound window selection

Discussion in 'Mac Programming' started by tbme, Aug 31, 2014.

  1. tbme macrumors newbie

    Joined:
    Aug 31, 2014
    #1
    Hi
    I have a problem with following script to change the output device:
    Code:
    set asrc to (choose from list {"Internal Speakers", "Soundflower (2ch)", "Soundflower (64ch)"} with title "Sound Picker" default items {"Internal Speakers"}) as text
    if result is "false" then return
    
    tell application "System Preferences"
    	reveal anchor "output" of pane id "com.apple.preference.sound"
    	activate
    	
    	tell application "System Events"
    		tell process "System Preferences"
    			select (row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" whose value of text field 1 is asrc)
    		end tell
    	end tell
    	
    	quit
    end tell
    
    It gives following error:
    Code:
    error "System Events got an error: Can’t get window \"Sound\" of process \"System Preferences\"." number -1728 from window "Sound" of process "System Preferences"
    
    This code seemed to work in Mountain Lion, but now I'm testing this on the Yosemite public beta 2

    Any help is appreciated!
     
  2. Partron22 macrumors 68020

    Partron22

    Joined:
    Apr 13, 2011
    Location:
    Yes
    #2
    Code:
     whose value of text field 1 is asrc
    Looks a little sketchy to me.

    I see objects like this in Mavericks:
    Code:
     table 1 of scroll area 1 of tab group 1 of window "Sound" of application process "System Preferences" of application "System Events"
     row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" of application process "System Preferences" of application "System Events"
     text field 1 of row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" of application process "System Preferences" of application "System Events"
     text field 2 of row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" of application process "System Preferences" of application "System Events"
     column 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" of application process "System Preferences" of application "System Events"
     column 2 of table 1 of scroll area 1 of tab group 1 of window "Sound" of application process "System Preferences" of application "System Events"
    
     
  3. tbme thread starter macrumors newbie

    Joined:
    Aug 31, 2014
    #3
    I forgot to mention that I simply found that somewhere. It seemed to work for the person (original post)

    Nevertheless, I don't really understand what you try to say with your second part of code. I'm looking for the solution for applescript not finding window "Sound".
     
  4. Partron22 macrumors 68020

    Partron22

    Joined:
    Apr 13, 2011
    Location:
    Yes
    #4
    That second bit isn't code, it's a list of objects in the "Sound" window.
    I think what you need to do is check the value of:
    Code:
    text field 1 of row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" of application process "System Preferences" of application "System Events"
    Perhaps the value of text field 1 of row 2 as well.

    From what I see, your code is likely finding the sound window OK, it's having trouble finding a textfield, within the Sound window, whose value is asrc.

    You might try having your script do something like
    Code:
    set myval to text field 1 of row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" of application process "System Preferences" of application "System Events"
    return myval
    -Then check result in AppleScript Editor.
    That should let you ferret out the Mavericks value of the textfield you want to work with.
     
  5. tbme, Sep 1, 2014
    Last edited: Sep 1, 2014

    tbme thread starter macrumors newbie

    Joined:
    Aug 31, 2014
    #5
    I tried that and it gave the same error (can't find window "Sound") on that line that I added.

    How did you find those objects? Maybe it's different for the Yosemite Beta?

    EDIT: I have tried some things:
    Code:
    get name of front window
    when the Sound window is on the foreground gives:
    Code:
    --> missing value
    If I use "window 1" to do the commands, it does seem to work (it can find the names of the output devices)
     
  6. Partron22 macrumors 68020

    Partron22

    Joined:
    Apr 13, 2011
    Location:
    Yes
    #6
  7. kryten2, Sep 1, 2014
    Last edited: Sep 1, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #7
    Works on Yosemite DP4-DP5. Not working on Yosemite DP6 with the aforementioned error.

    Code:
    set asrc to (choose from list {"Internal Speakers", "Soundflower (2ch)", "Digital Out"} with title "Sound Picker" default items {"Internal Speakers"}) as text
    if result is "false" then return
    
    tell application "System Preferences"
    	activate
    	reveal anchor "output" of pane id "com.apple.preference.sound"
    end tell
    
    tell application "System Events"
    	tell process "System Preferences"
    		select (row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" whose value of text field 1 is asrc)
    	end tell
    end tell
    
    tell application "System Preferences" to quit
    
     

    Attached Files:

  8. tbme thread starter macrumors newbie

    Joined:
    Aug 31, 2014
    #8
    I have found that using window 1 instead of window "Sound" does the thing.
    Maybe I'll change it to "Sound" again when the official release is out.

    Thanks for the help anyway!
     
  9. the Western zoo macrumors 6502

    the Western zoo

    Joined:
    May 16, 2006
    Location:
    Aarhus C, Denmark
    #9
    I have made a script to choose my cinema display as my audio output. However, sometimes - seamly at random - I get an error message from the script.

    Does anybody have any suggestions as to why I get an error?

    My script:
    Code:
    	# Set Audio Out to Cinema Display	
    	tell application "System Preferences"
    		activate
    		reveal anchor "output" of pane id "com.apple.preference.sound"
    	end tell
    	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 whose value of text field 1 is "LED Cinema Display")
    		end tell
    	end tell
    	quit application "System Preferences"
     

Share This Page