🕷 zenspider.com

by ryan davis



sitemap
Looking for the Ruby Quickref?

Applescript PITA: Set iTunes DJ playlist and multiple speakers

Published 2011-08-23 @ 02:52

Tagged toys, osx, applescript, productivity

This is in my script menu titled “Play iTunes DJ - Bedtime”. I have another for Coding that just changes the two properties at the top.

property myPlaylist : "Mellow"
property myVolume : 25

tell application "iTunes"
  activate
  
  set dj to playlist "iTunes DJ"
  stop application
  reveal dj
  
  my setSource()
  my setSpeakers()
  
  set sound volume to myVolume
  play dj
  
  set minimized of front browser window to true
end tell

on setSource()
  tell application "System Events"
    tell application process "iTunes"
      tell window "iTunes"
        if value of pop up button 1 is not myPlaylist then
          -- this is so stupid... apple fucked up the UI and detaches the menu so we can't access it
          -- so we must set the value and then... UGH... _type_ to select the value we just set
          set focused of pop up button 1 to true
          set value of pop up button 1 to myPlaylist
          delay 0.5
          keystroke " "
          delay 0.5
          keystroke " "
          delay 0.5
          click button "Refresh"
        end if
      end tell
    end tell
  end tell
end setSource

on setSpeakers()
  tell application "System Events"
    tell application process "iTunes"
      -- stable:
      -- set airplayButton to the first button of window "iTunes" whose description contains "AirPlay"
      -- fast(er):
      set airplayButton to button 12 of window "iTunes" -- changes on updates :(
      -- lame... when it is an icon, it doesn't have a title attribute... at all... so you must do this:
      set thetitle to get value of attributes of airplayButton whose name is "AXTitle"
      
      if thetitle is not {"2 Speakers"} then
        try
          window "Multiple Speakers" -- error if not open
        on error
          tell airplayButton
            delay 0.1
            click
            delay 0.1
            keystroke (ASCII character 31) -- down arrow key
            delay 0.1
            keystroke (ASCII character 31) -- down arrow key
            delay 0.1
            keystroke " "
          end tell
        end try
        delay 0.5
        set value of every checkbox of group 1 of every row of table 1 of scroll area 1 of window "Multiple Speakers" whose value is 0 to 1
      end if
    end tell
  end tell
end setSpeakers

As an aside:

Dear apple… fuck you… This was a serious pain in the ass and it shouldn’t be.

Would it have been so hard to add an application attribute for active speakers or make iTunes DJ its own class so it could have *gasp* a source attribute?

Lemme help you out there… Answer: No.