Fedxa
Fedxa

Reputation: 131

How to select speakers for iTunes from applescript

I would like to be able to select output sound device for iTunes from a script (any programming language would be ok in fact).

For the moment I was able to use UI element scripting to get up to clicking on the button which gives the menu to select the speakers:

tell application "System Events"
     tell window "iTunes" of process "iTunes"
        set chbtn to first UI element whose help is "Choose which speakers to use."
        tell chbtn
            click
            -- tell menu 1 to get every menu item
        end tell
     end tell
end tell

This works, and menu with possible choices appears. However, the applescript seems to stop after the click command, and further actions (in the place where the comment is in the code) happen only after I click somewhere on the screen myself. How can I prevent this and continue to select the menu item from this menu?

Any solution without reverting to UI scripting is also very welcome!

Upvotes: 2

Views: 915

Answers (2)

Sojourner
Sojourner

Reputation: 66

I've had that delay problem before when using UI scripting. You might be able to eliminate it by telling the script which elements to click. I don't have external speakers, so the elements' names and properties aren't on my computer. An easy way to get more info on the elements available is to use (not free, but excellent) UI Browser http://pfiddlesoft.com/uibrowser/. A less easy, but free, way to get more info on the elements is to use:

tell application "System Events"
    tell window "iTunes" of process "iTunes"
        set chbtn to first UI element whose help is "Show or hide item artwork and video viewer."
        tell chbtn
            entire contents
        end tell
end tell

end tell

Upvotes: 0

Fedxa
Fedxa

Reputation: 131

The solution code is

tell application "iTunes" to activate
tell application "System Events"
    tell window "iTunes" of process "iTunes"
        click (first UI element whose help is "Choose which speakers to use.")
        keystroke "DENON" & return -- Select "DENON" airplay entry
        -- keystroke "Computer" & return -- Select standard output
     end tell
end tell

However, there is an annoying 4 second delay between the click and the keystroke

Upvotes: 1

Related Questions