Your browser (Internet Explorer 7 or lower) is out of date. It has known security flaws and may not display all features of this and other websites. Learn how to update your browser.

X

Navigate / search

How do I make my repayments Payday loan Why is a payday loan right for you

Control Airplay/Airtunes feature of iTunes 10.0 with Applescript

I’ve been using iTunes with several Airport express units utilizing Apple’s Airplay (previously called Airtunes) scattered throughout my house as a basic but very powerful “Whole House Audio” system for several months now. In addition to my computer speakers in the home office, I have an Airport Express connected via standard audio, or optical audio cable to a stereo or dedicate speaker system in each of our primary living spaces (e.g. Kitchen, Living Room, Basement, Master Bedroom). This setup has worked very well for distributing high quality music throughout the house and can easily be controlled via the iTunes console on the computer hosting the music, or with the excellent and free Apple Remote application for iOS devices (such as my iPhone).

Where I started to run into trouble however was when I attempted to integrate my fledgling MCV Vera Home Automation system. In short, I wanted to be able to trigger a series of events in iTunes from an existing home automation scene so that pressing one button on the wall would do such things as:

  • Turn off living room lights
  • Bring dining room overhead light up to 60%
  • Enable Kitchen and Sunroom speakers within iTunes
  • Disable all other home speakers within iTunes
  • Play a playlist called “Dinner Time” on random shuffle.

The lighting events are very straightforward with my z-wave switches and the Vera, and with my previous experience automating actions on the Mac I knew that the iTunes control should be able to be done with Applescript. Indeed, Apple provides a fairly complete set of built in applescript actions for iTunes making things like selecting playlists and playing tunes extremely easy. What they do not provide however is any native way of controlling the Airtunes functionality for whole house audio distribution. Many attempts at searching for an existing solution to control the Airplay (previously called Airtunes) functionality of iTunes provided conflicting information on it’s feasibility. I did however run across a third party piece of software that claims to use applescript to provide this level of control to their users, so I knew it was possible.

After some experimentation, I was able to produce the below applescript which reliably adjusts the Airplay/Airtunes speaker settings on your mac. I am providing it here with the hope that it can be found by those of you who, like me, had been googling like crazy to try to find a solution like this, but coming up empty. This can be called via a local command, a scheduled action, or (as is the case for me) a remotely executed action from my home automation system. This script will only run successfully if you have one or more Airplay speakers connected to your mac (this is obvious). It also requires that “Enable access for assistive devices” to be enabled from the Accessibility preferences in your system settings. The first bit of code in the script checks this setting and will prompt you to enable it if not already enabled. I’ve left the example deliberately simple. The code below will simply turn on three speakers, found by name if they are presently off. Building more logic into the script will be straightforward if your solution requires it.

If you would like more information on the other components required to make this work with my home automation system, or would like to see my recommendations for Speakers/amplifiers to be used with your Airport Express units, please see my original forum post on the Mi Casa Verde user forums here.

Please feel free to leave your comments, suggestions, and code improvements (I do not claim to be an applescript expert) in the comments.

This version of the script is for iTunes 10.0 only.

You can find a newer version of this script for use with iTunes 10.1 here.

I have also provided an earlier version of this script for use with iTunes 9.x here.


--

-- Remember to turn on access for assistive devices.

-- Working as of September 2010 with iTunes 10.0.x only. (this is now Broken in 10.1)

-- See http://fall-line.com or http://forum.micasaverde.com for iTunes 9 version.

-- First, check to make sure assitive devices is enabled. If not, allow the user to turn it on so the rest of the script can execute.

tell application "System Events" to set isUIScriptingEnabled to UI elements enabled

if isUIScriptingEnabled = false then

tell application "System Preferences"

activate

set current pane to pane "com.apple.preference.universalaccess"

display dialog "Your system is not properly configured to run this script.

         Please select the \"Enable access for assistive devices\"

         checkbox and trigger the script again to proceed."

return

end tell

end if

activateapplication "iTunes"

tell application "System Events"

tell application process "iTunes"

clickbutton 8 of window "iTunes" --Press the iTunes 10 Speaker Button

key code 125 using {command down} -- this is essentiall hitting page down (command + down arrow)

delay 0.1

keystrokereturn

tell window "Multiple Speakers" of application process "iTunes" of application "System Events"

activate-- Make sure we are on the right window

tell table 1 of scroll area 1

activate-- Drill into the Table that contains our speakers

-- "Computer" should always be row #1 since it is immune to the alphabetizing iTunes does to the rest. If it is off, turn it on first, so that we can modify the status of others in any way we please without getting an error.

if (description of checkbox 1 of group 1 of row 1) = "Computer" and value of checkbox 1 of group 1 of row 1 as integer = 0 then

clickcheckbox 1 of group 1 of row 1

end if

repeat with i from 1 to (count of every row) -- Itterate through each row, looking for the criteria below

set rowcount to count of rows

if rowcount > 0 then

tell group 1 of row i

activate-- Drill into the object group of each row, so we don't have to later.

-- If "Speaker in Room A" is turned off (value of checkbox 1 = 0), turn it on. Otherwise do nothing

if (description of checkbox 1) = "Speaker in Room A" and value of checkbox 1 as integer = 0 then

clickcheckbox 1

end if

if (description of checkbox 1) = "Speaker in Room B " and value of checkbox 1 as integer = 0 then

clickcheckbox 1

end if

end tell

end if

end repeat

end tell

end tell

end tell

end tell

-- Finally, play the Dinner Time playlist. All you need to do here is create a playlist called "Dinner Time", etc.

tell application "iTunes"

set shuffle of playlist "Dinner Time" to 0

set shuffle of playlist "Dinner Time" to 1

play playlist "Dinner Time"

end tell--

Wenn Sie Deutsch sprechen, besuchen Sie diese Seite

Comments

iLounge Forums

Applescript to control Airtunes device selection?…

Solution here for iTunes 10. Also link to iTunes 9.x version:

Control Airplay/Airtunes feature of iTunes with Applescript | Fall-Line……

iLounge Forums

Automating or scripting airtunes?…

I too was long frustrated by this, but was eventually able to get this work in the latest versions of iTunes (9 and 10). Here is my guide how to do it:

Control Airplay/Airtunes feature of iTunes w……

Tys von Gaza

Very cool.

Do you have any idea if there is a way to trigger an applescript event when the selected speakers change? Ideally I would like to turn off my speakers when they are not in use, but switch them on when a set of speakers is selected in iTunes.

n8henrie

I’ve incorporated this into a script that runs with my alarm clock (to ensure that it doesn’t end up blasting my alarm throughout the house), but I’ve recently been getting an error intermittently.

Frustratingly, it’s not every time, but most mornings I get an error. When I run the script through Script Debugger, I get “System Events got an error: Can’t get window “iTunes” of application process “iTunes”.” Anyone else getting this? I wonder what’s going wrong.

n8henrie

NVM — I think it may be a problem with Spaces. Seems to work when I run the script from the same “Space” as iTunes, and give me the error when I run it from the default Space. Odd.

admin

Interesting note, thanks. I don’t personally use spaces so I’ve never run into this issue, but it’s good to know for others that may encounter the same thing. There may be a solution that will make the script more flexible when it comes to multiple spaces.

Pete

Hi, I had a few issues with this script and iTunes 10.1. Firstly, button 8 of window “iTunes” is now “Shuffle”, so the script has to be modified to click button 11 now.
Secondly, I seem to get a long pause (~4 seconds) after the script activates the speaker pop-up menu, and before it presses command-down. I’m not sure why.
Thirdly, the script doesn’t seem to actually activate my chosen speakers for some reason. I’ve had a look at the Events/Replies in AppleScript Editor, and the “if” statements recognising my speakers register fine. Apparently the checkbox is being clicked, but there is no result.

Leave a comment

name

email (not published)

website