using cron to schedule iTunes?
http://www.malcolmadams.com/itunes/index.shtml
includes instructions for scripting iTunes with iCal.
Being a luddite, I have resisted the apps such as iCal that are designed to drive users toward .Mac; I'd rather schedule via cron.
Finally, in looking over the script library I did not find many scripts that were oriented to schediling in general.
Here's what I'd like to accomplish:
Each day, cron would fire off at specified times and access online radio streams via a custom playlist or playlists. The radio streams are of course just track numbers, so I imagine this should be easlit accomplished.
Any words of wisdom? In using cron, it's possible that the best way to do it is to set up a single script for each new play event, so that the applecript executes, establishes the play, and is done.
Thanks, hope to hear more soon!
[917 byte] By [
mwhybark] at [2007-11-9 13:37:25]

# 1 Re: using cron to schedule iTunes?
I'm not the cron guy--deeg is--but have a look at:
"CLI, Applescript, and cron"
http://www.oreillynet.com/cs/user/view/cs_msg/7550
and
"Alarm Clock with iTunes & Cron Solution"
http://www.oreillynet.com/cs/user/view/cs_msg/12839
Then its just a matter of writing the applet in Script Editor. You'll need the address of each stream:
tell application "iTunes" to open location "http://pri.kts-af.net/redir/index.pls?esid=858120ba998171abe313ee76a94b1349&url_no=4&client_id=7&uid=68efed4d03ec7e45fd3978262c107180&clicksrc=xml"
to get the address, run this script on a single selection:
tell application "iTunes"
get address of selection
end tell
OR, you could put each stream in its own playlist (named "stream1", "stream2", etc) by itself and write this script:
tell application "iTunes" to play playlist "stream1"
Save as an application, uncheck show startup screen.
Load 'em up in cron. Enjoy
# 2 Re: using cron to schedule iTunes?
Thanks, Doug.
I built a separte script for each station, like this:
filename: KUOW
tell application "iTunes"
set the view of the front browser window to playlist "a radio selection"
copy (a reference to (get view of front window)) to thePlaylist
play track "KUOW Live 96 kbps" of thePlaylist
end tell
and it more or less works within the app, at least.
Oddly, saving as applications is unreliable. Also, long tracknames or tracknames with special characters - ie, ":-)" - appear to fail.
more debugging later. I sure will hit the oreilly ref.
# 3 Re: using cron to schedule iTunes?
saved as application from within Script Editor, added to cron with Cronnix, and it worked just fine.
default-format saved scripts (after 'check syntax' and the variable and term highlighting is added) did not run from the command line even after chmod 777; console reported 'cannot execute binary file'
Now I need to add some error checking based on 'am i burning a CD?' sort of things.
Thanks!
# 4 Re: using cron to schedule iTunes?
Glad to help.
Here's a variation of your script, for laffs:
tell application "iTunes"
set thePlaylist to playlist "a radio selection"
tell thePlaylist to play track "KUOW Live 96 kbps"
end tell
Also, you may need to escape some characters with a "\" if they are presenting problems. Usually double quotes inside a string require this, eg:
set x to "My dog's name is \"Ralph\""
Whatever.
# 5 Re: using cron to schedule iTunes?
Hm, I thought I'd tried that simpler version, but test flight sez you are right.
Here's where I've gotten to. I should note that I'm in OS X.
There are two things not yet done:
1. I'd love to break the script up into named functions to fix the end-if confusion that will happen if it ever gets bigger (plus, the string variables could be entered as declared variables, hey?)
2. I need to understand how to get the disc-inserted status of iTunes to test - I think that may be all I need to test for to meet my needs.
Finally, I punched the "burn" button in Finder to see if I could get some status from it regarding that process and the whole script halted with a "Finder busy" error, which is actually fine with me if it doesn't override a burn.
Thanks for your help and feedback!
-- This is a script intended to permit cron-based scheduling of internet radio streams in iTunes.
-- In order not to interrupt disc burning operations, I test for apps that need a burner.
-- then I test iTunes to make sure iTunes is not ripping or burning.
-- this last problem is still under research.
-- ideally the script should be broken out of the if - then nesting and called by subroutine names
tell application "Finder"
set _apps_ to the name of every process as string
-- this works to present an array of all processes, coercing to string so i can check on the app name
repeat with i from 1 to 1
-- invoking 'repeat' to get breakpoints in if-then tests
if _apps_ contains "Toast" then
say "Toast is active."
-- beep
-- or you could just have it beep, how boring
exit repeat
else
if _apps_ contains "iDVD" then
say "i-D-V-D is active"
exit repeat
else
-- if iTunes is doing something
-- say iTunes is busy
-- else
tell application "iTunes"
set thePlaylist to playlist "a radio selection"
tell thePlaylist to play track "KUOW Live 96 kbps"
end tell
-- end if
end if
end if
end repeat
end tell
I couldn't find a "code" button so i just manually enered the tags I've seen elsewhere - sorry if it comes out kooky
# 6 Re: using cron to schedule iTunes?
A simple solution to a couple of problems is to wrap the Tunes stuff in a try block. If there is an error, anything in the try block will not be carried out.
try
tell app "iTunes"
--do something
end tell
on error
-- do nothing
end try
Also, when checking processes it is best to target "System Events" rather than Finder. This will give you a list:
tell app "System Events"
set running_apps to (get name of processes)
end tell
if running_apps contains "Toast" then
-- etc
Lookin' good!
# 7 Re: using cron to schedule iTunes?
Try blocks, eh?
cool! my docs are a bit aged and don't seem to include it ("the Tao of Applescript"), and the apple-hosted stuff is a bit hard to wade through. That's quite helpful.
Do you have a reference to disc-state testing, by any chance?
# 8 Re: using cron to schedule iTunes?
Tao of page 338 :)
Disc state may have to be done through the Finder...not certain, but maybe check for a class of disc??
# 9 Re: using cron to schedule iTunes?
Getting more ambitious. Haven't figured out how to get the dialog and the speech to happen simultaneously.
Ideally, I'd like to declare the tested apps at the top and do some form of looping, but I'm not conversant enough with how to do that.
the declaration is easy:
set theTestList to ("app1", "app2", . . .)
but over that I'm unsure.
Also see my sloppy repeated call to iTunes.
The other problem is that there's no obvious way to get "burn" status. Fortunately it doesn't seem to cause problems with iTunes. the other small problem is that modals seem to create difficulties - the script just waits, no biggie; but it makes me uneasy - wil the script just die on a timeout?
-- This is a script intended to permit cron-based scheduling of internet radio streams in iTunes.
-- In order not to interrupt disc burning operations, I test for apps that need a burner.
-- the iTunes Dictionary does not appear to contain specific ebets or properties that reflect ripping or burning
set theStation to "KUOW Live 96 kbps"
set myPlaylist to "a radio selection"
-- I have a subset of internet radio in a custom playlist; you might have one too, with a different name
-- We find the list item for the sation by name
tell application "System Events"
if ejectable of disks contains true then
-- assumption being that the CD is DOING something, and you don't want iTunes jumping to a radio station all willy-nilly
-- inserted blank CDs will not be caught: they are not mounted, see?
set CDMounted to 1
-- a CD is mounted
end if
end tell
tell application "System Events"
set running_apps to (get name of processes)
-- what's running?
end tell
-- now we'll test for CD and DVD-oriented replication apps
if running_apps contains "Toast" then
set theDiscApps to 1
-- Toast is running
else if running_apps contains "iDVD" then
set theDiscApps to 1
-- iDVD is running
-- add your own tests if you'd like
end if
if CDMounted = 1 or theDiscApps = 1 then
copy (display dialog "A CD or DVD-related process is active. Switch iTunes to " & theStation & " anyway?" buttons {"OK", "Cancel"} default button "Cancel") to dialogResults
if the button returned of dialogResults is "OK" then
try
tell application "iTunes"
set thePlaylist to playlist myPlaylist
tell thePlaylist to play track theStation
end tell
on error
say "iTunes is busy at the moment."
-- if iTunes is presenting a modal dialog, the script hangs and has to be escaped from.
-- running the script after "burn disc" is engaged in iTunes does not interrupt the burn.
end try
end if
else
try
tell application "iTunes"
set thePlaylist to playlist "a radio selection"
tell thePlaylist to play track theStation
end tell
on error
say "iTunes is busy at the moment."
-- if iTunes is presenting a modal dialog, the script hangs and has to be escaped from.
-- running the script after "burn disc" is engaged in iTunes does not interrupt the burn.
end try
end if
# 10 Re: using cron to schedule iTunes?
oops, need to declare my testing variables at the top:
set CDMounted to 0
set theDiscApps to 0
# 11 Re: using cron to schedule iTunes?
In AppleScript a "function" is a subroutine or handler. Use this in replace of the coded block:
to play_stream()
try
tell application "iTunes"
set thePlaylist to playlist myPlaylist
tell thePlaylist to play track theStation
end tell
on error
say "iTunes is busy at the moment."
-- if iTunes is presenting a modal dialog, the script hangs and has to be escaped from.
-- running the script after "burn disc" is engaged in iTunes does not interrupt the burn.
end try
end play_stream
-- call like this
tell me to play_stream
Or something. See Tao in subroutines.
# 12 Re: using cron to schedule iTunes?
hm, that's puzzling.
with "to play_stream()" defined, the post-test if/else blocks are like this:
if -- some stuff
if the button returned of dialogResults is "OK" then
tell me to play_stream
end if
else
tell me to play_stream
end if
outputs <<handler play_stream>> to the result but no action takes place
hm. gotta do some hardware monkeying, then I'll hit the books, then I'll be back.
# 13 Re: using cron to schedule iTunes?
aha!
should it be
tell me to play_stream()
including the trailing parens?
# 14 Re: using cron to schedule iTunes?
shoot, the globals aren't populating the handler. books ho!
# 15 Re: using cron to schedule iTunes?
Yes, declare globals at the top, especially the playlist stuff...otherwise, stick your variables in the calling statements (), like
tell me to play_stream(myPlaylist)
then later, declaring the subroutine, start like:
to play_stream(myPlaylist)
Although, I usually use globals and properties...hate keeping track of what was sent where when.
Keep on keepin' on!
# 16 Re: using cron to schedule iTunes?
As final as it will be for a spell:
-- by mike whybark, http://mike.whybark.com/
-- thanks to Doug Adams of Doug's Applescripts for iTunes, http://www.malcolmadams.com/itunes/
-- This is a script intended to permit cron-based scheduling of internet radio streams in iTunes.
-- In order not to interrupt disc burning operations, I test for apps that need a burner and ask if the user wnats to continue
-- the iTunes Dictionary does not appear to contain specific events or properties that reflect ripping or burning. Buring appears unaffected by the station switching anyway.
-- copy this code, paste into Script Editor, edit your values for "myPlaylist" and "the Station" to reflect your needs, and save as "application." I name each script with the station's call letters, so this one would be "KUOW"
-- Save the applets into ~/Library/iTunes/Scripts
-- set up a crontab to switch according to your desired schedule. I use the excellent GUI edtor 'Cronnix' - http://www.koch-schmidt.de/cronnix/
global theStation, myPlaylist, CDMounted, theDiscApps
set theStation to "KUOW Live 96 kbps"
set myPlaylist to "a radio selection"
set CDMounted to 0
set theDiscApps to 0
-- I have a subset of internet radio in a custom playlist; you might have one too, with a different name
-- We find the list item for the sation by name
tell application "System Events"
if ejectable of disks contains true then
-- assumption being that the CD is DOING something, and you don't want iTunes jumping to a radio station all willy-nilly
-- inserted blank CDs will not be caught: they are not mounted, see?
set CDMounted to 1
-- a CD is mounted
end if
end tell
tell application "System Events"
set running_apps to (get name of processes)
-- what's running?
end tell
-- now we'll test for CD and DVD-oriented replication apps
if running_apps contains "Toast" then
set theDiscApps to 1
-- Toast is running
else if running_apps contains "iDVD" then
set theDiscApps to 1
-- iDVD is running
-- add your own tests if you'd like
end if
if CDMounted = 1 or theDiscApps = 1 then
copy (display dialog "A CD or DVD-related process is active. Switch iTunes to " & theStation & " anyway?" buttons {"OK", "Cancel"} default button "Cancel") to dialogResults
if the button returned of dialogResults is "OK" then
tell me to play_stream()
end if
else
tell me to play_stream()
end if
to play_stream()
try
tell application "iTunes"
set thePlaylist to playlist myPlaylist
tell thePlaylist to play track theStation
end tell
on error
say "iTunes is busy at the moment."
-- if iTunes is presenting a modal dialog, the script hangs and has to be escaped from.
-- running the script after "burn disc" is engaged in iTunes does not interrupt the burn.
end try
end play_stream
# 17 Re: using cron to schedule iTunes?
Looks good!
To avoid the modal problem, wrap the error handler in a try too!
try
tell application "iTunes"
set thePlaylist to playlist myPlaylist
tell thePlaylist to play track theStation
end tell
on error
try
say "iTunes is busy at the moment."
-- if iTunes is presenting a modal dialog, the script hangs and has to be escaped from.
-- running the script after "burn disc" is engaged in iTunes does not interrupt the burn.
end try
end try