iLounge iPhone + iPod Buyers' Guide
Become a member of the iLounge Forums. Register Now!
To start viewing messages, select the forum that you want to visit from the selection below.
If this is your first visit, be sure to check out the Forum FAQ and Forum Policy.

Topic: using cron to schedule iTunes?

Reply Thread Tools Topic Search
Old 08-27-2003, 06:35 PM
#1
 
Freshman Lounger
 
Join Date: Aug 2003
Location: --
Posts: 15
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!
__________________
--
mwhybark is offline  
Share on TwitterShare on FacebookDigg this Post!Google Bookmark this Post!
Reply With Quote

Join the iLounge Community and the ad above will disappear.

Old 08-27-2003, 07:25 PM
#2
 

Mod
 
Join Date: Jun 2003
Location: Providence, RI, USA
Posts: 705

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:

Code:
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:

Code:
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:

Code:
tell application "iTunes" to play playlist "stream1"


Save as an application, uncheck show startup screen.

Load 'em up in cron. Enjoy
__________________
Visit Doug's AppleScripts for iTunes
Doug Adams is offline  
Share on TwitterShare on FacebookDigg this Post!Google Bookmark this Post!
Reply With Quote
Old 08-27-2003, 08:00 PM
#3
 
Freshman Lounger
 
Join Date: Aug 2003
Location: --
Posts: 15
thanks

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.
__________________
--
mwhybark is offline  
Share on TwitterShare on FacebookDigg this Post!Google Bookmark this Post!
Reply With Quote
Old 08-27-2003, 08:22 PM
#4
 
Freshman Lounger
 
Join Date: Aug 2003
Location: --
Posts: 15
proof of concept success

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!
__________________
--
mwhybark is offline  
Share on TwitterShare on FacebookDigg this Post!Google Bookmark this Post!
Reply With Quote
Old 08-27-2003, 08:22 PM
#5
 

Mod
 
Join Date: Jun 2003
Location: Providence, RI, USA
Posts: 705

Glad to help.

Here's a variation of your script, for laffs:

Code:
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.
__________________
Visit Doug's AppleScripts for iTunes

Last edited by Doug Adams; 08-27-2003 at 08:25 PM.
Doug Adams is offline  
Share on TwitterShare on FacebookDigg this Post!Google Bookmark this Post!
Reply With Quote
Old 08-28-2003, 02:08 AM
#6
 
Freshman Lounger
 
Join Date: Aug 2003
Location: --
Posts: 15
more plus some conditional catching

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!

Code:
-- 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
__________________
--
mwhybark is offline  
Share on TwitterShare on FacebookDigg this Post!Google Bookmark this Post!
Reply With Quote
Old 08-28-2003, 06:30 AM
#7
 

Mod
 
Join Date: Jun 2003
Location: Providence, RI, USA
Posts: 705

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.

Code:
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:

Code:
tell app "System Events"
     set running_apps to (get name of processes)
end tell
if running_apps contains "Toast" then
-- etc
Lookin' good!
__________________
Visit Doug's AppleScripts for iTunes
Doug Adams is offline  
Share on TwitterShare on FacebookDigg this Post!Google Bookmark this Post!
Reply With Quote
Old 08-28-2003, 10:40 AM
#8
 
Freshman Lounger
 
Join Date: Aug 2003
Location: --
Posts: 15
ahh! sounds useful!

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?
__________________
--
mwhybark is offline  
Share on TwitterShare on FacebookDigg this Post!Google Bookmark this Post!
Reply With Quote
Old 08-28-2003, 11:21 AM
#9
 

Mod
 
Join Date: Jun 2003
Location: Providence, RI, USA
Posts: 705

Tao of page 338

Disc state may have to be done through the Finder...not certain, but maybe check for a class of disc????
__________________
Visit Doug's AppleScripts for iTunes
Doug Adams is offline  
Share on TwitterShare on FacebookDigg this Post!Google Bookmark this Post!
Reply With Quote
Old 08-28-2003, 01:11 PM
#10
 
Freshman Lounger
 
Join Date: Aug 2003
Location: --
Posts: 15
more

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:

Code:
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?

Code:
-- 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
__________________
--
mwhybark is offline  
Share on TwitterShare on FacebookDigg this Post!Google Bookmark this Post!
Reply With Quote
Old 08-28-2003, 01:24 PM
#11
 
Freshman Lounger
 
Join Date: Aug 2003
Location: --
Posts: 15

oops, need to declare my testing variables at the top:

Code:
set CDMounted to 0
set theDiscApps to 0
__________________
--
mwhybark is offline  
Share on TwitterShare on FacebookDigg this Post!Google Bookmark this Post!
Reply With Quote
Old 08-28-2003, 02:38 PM
#12
 

Mod
 
Join Date: Jun 2003
Location: Providence, RI, USA
Posts: 705

In AppleScript a "function" is a subroutine or handler. Use this in replace of the coded block:

Code:
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.
__________________
Visit Doug's AppleScripts for iTunes
Doug Adams is offline  
Share on TwitterShare on FacebookDigg this Post!Google Bookmark this Post!
Reply With Quote
Old 08-28-2003, 04:41 PM
#13
 
Freshman Lounger
 
Join Date: Aug 2003
Location: --
Posts: 15

hm, that's puzzling.

with "to play_stream()" defined, the post-test if/else blocks are like this:

Code:
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.
__________________
--
mwhybark is offline  
Share on TwitterShare on FacebookDigg this Post!Google Bookmark this Post!
Reply With Quote
Old 08-28-2003, 04:43 PM
#14
 
Freshman Lounger
 
Join Date: Aug 2003
Location: --
Posts: 15

aha!

should it be

Code:
tell me to play_stream()
including the trailing parens?
__________________
--
mwhybark is offline  
Share on TwitterShare on FacebookDigg this Post!Google Bookmark this Post!
Reply With Quote
Old 08-28-2003, 04:49 PM
#15
 
Freshman Lounger
 
Join Date: Aug 2003
Location: --
Posts: 15

shoot, the globals aren't populating the handler. books ho!
__________________
--
mwhybark is offline  
Share on TwitterShare on FacebookDigg this Post!Google Bookmark this Post!
Reply With Quote

Topic: using cron to schedule iTunes?

Reply Thread Tools Topic Search

Become a member of the iLounge Forums. Register Now!
To start viewing messages, select the forum that you want to visit from the selection below.
If this is your first visit, be sure to check out the Forum FAQ and Forum Policy.
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





View iLounge History. Read our old Forums Archive (2001-2003)
All times are GMT -4. The time now is 05:26 PM.


Shop for Accessories: Cases, speakers, chargers, etc.