🕷 zenspider.com

by ryan davis

Looking for the Ruby Quickref?

Updated iTunes Bedtime script

Published 2014-12-19 @ 00:00

Tagged applescript

Minor changes this time. This works in iTunes v12, which is the latest release for Yosemite. The only real fixes centered around verbage changes to menu items. I wish it had applescript support that didn’t require UI scripting to do basic things like listen to a smart playlist or a particular band.

This script starts up itunes and airfoil, sets the volumes of both, picks a specified playlist, and plays it. I fire this one up just before going to bed. I have another copy that sets up my programming playlist and another for cooking.

They’re always available via the script menu by putting them in "~/Library/Scripts/".

I hope this helps.


property myPlaylist : "Mellow"
property myVolume : 50
property airfoilVolume : 0.5

tell application "iTunes"
    stop application
    my setShuffle()
    set sound volume to myVolume
    my setSource()
    my minimizeITunes()
end tell

my setSpeakers()

on setShuffle()
    tell application "System Events"
        tell process "iTunes"
            tell menu 1 of menu bar item "Controls" of menu bar 1
                tell menu 1 of menu item "Shuffle"
                    if "Turn On Shuffle" is in (title of menu items) then
                        click menu item "Turn On Shuffle"
                    end if
                    click menu item "Songs"
                end tell
                tell menu 1 of menu item "Repeat"
                    click menu item "All"
                end tell
            end tell
        end tell
    end tell
end setShuffle

on minimizeITunes()
    tell application "iTunes" to close every window
    tell application "System Events" to tell process "iTunes"
            click menu item "Switch to MiniPlayer" of menu 1 of menu bar item "Window" of menu bar 1
        end try
    end tell
end minimizeITunes

on setSource()
    tell application "iTunes"
        tell playlist myPlaylist
        end tell
        next track
    end tell
end setSource

on setSpeakers()
    set volume output volume 100
    set volume input volume 100
    set volume alert volume 100
    set volume without output muted
    tell application "Airfoil"
        close every window
        set current audio source to application source "iTunes"
        set linked volume to false
        disconnect from every speaker
        set (volume of every speaker) to airfoilVolume
        connect to speaker "airport2"
        connect to speaker "Computer"
    end tell
end setSpeakers