Re: New iTunes Track (AS DJ Script)
Re: New iTunes Track (AS DJ Script)
- Subject: Re: New iTunes Track (AS DJ Script)
- From: Mr Tea <email@hidden>
- Date: Wed, 25 Feb 2004 16:30:47 +0000
This from Greg Townsend - dated 24/2/04 11.55 pm:
>
Is there a way to use AS to determine when the track playing in iTunes
>
changes? I know you can use an idle handler, but I'd rather not have to
>
poll iTunes any more than is necessary
...and this from Matthew Smith - dated 25/2/04 2.10 am:
>
There's no other way, other than polling.
Which begs the question, is there anything inherently bad/inefficient/gauche
about using a stay-open applet with an idle handler for this sort of task?
I ask because I've never been entirely comfortable about leaving applets
ticking away around the place. I can't explain why. I just get a sort of
'you don't want to do that' feeling about it. (This happened, for example,
when I migrated to OS X, where 'folder actions' were at first unavailable,
then poorly implemented. I could easily have converted my folder action
scripts into stay-open applets to watch the folders for me, but preferred to
find other ways of doing what I needed.)
Is my dislike of stay-open applets irrational? (and would that dislike have
been irrational in OS 9 and earlier?) There are clearly contexts, like
monitoring track changes in iTunes, where nothing else will work, so should
I feel entirely comfortable about using a script like the 'AS DJ' reproduced
below, which checks iTunes every 3 seconds, or are there caveats and
potential pitfalls to consider... ?
A few words about the applet script. It's been knocked-up as an oblique
response to Greg's original question, and is built around a much simpler
compiled script that I run using an iKey-powered hot key to tell me what's
playing at the moment when something catches my attention. I've thrown in
various bits and bobs that illustrate aspects of iTunes scripting and the
broader capabilities of AppleScript. (I might also have slipped in some
demonstrative examples of sloppy scripting and redundant code, just to keep
y'all on your toes.)
While this applet has been running on my system (a G4 Quicksilver 867 with
1.5GB RAM), I checked in the Activity Viewer to see what it was up to, and
found that it was using 0-1.4% of CPU, around 16MB of Real Memory, and
122.43MB of Virtual Memory. If an applet had been taking up that kind of
memory on my old Performa 5200, I would have instantly stomped it out of
existence, but perhaps this is normal, acceptable behaviour in these more
profligate times. Here's the AS DJ...
--===========================================================
global newTrack
global oldTrack
global dLogs
set dLogs to "You are currently listening to
This track is
Let's get down with some radical sounds! This is
It's time to groove your arse off to
Yo, homiez! This is
Here's a kickin' track by one cool [[inpt PHON]] mUXDAX fUXkAX. [[inpt
TEXT]] This is "
set oldTrack to anything
on idle
tell application "iTunes"
if player state is playing then
set newTrack to current track
if newTrack is not oldTrack then
set n to name of newTrack
if class of newTrack is not URL track then
tell current track
set {n, A, R} to {name, artist, rating}
if R is 0 then
set RLog to ". Unrated."
else
set RLog to ". " & (R / 20) & " stars."
end if
set dlog to some paragraph of dLogs & n & ", by " & A & RLog
set VA to volume adjustment
copy (-60) to volume adjustment
delay 0.4
say dlog
copy VA to volume adjustment
end tell
else
say "You are listening to " & n & ". Track information not
available."
end if
set oldTrack to newTrack
end if
end if
end tell
return 3
end idle
on quit
continue quit
end quit
--===========================================================
(Any lines that abut the left margin of the message window have been wrapped
and should be re-connected to the end of the previous line.)
The three second return before each run of the idle handler is generously
brief. A larger return value would make the script less busy, but less
responsive, too. To use it, you need to paste it into a script editor
document window, and save the file as an application with the 'stay open'
option checked. Good luck. Adapt it to your needs. Apologies for the fact
that there's no annotations, no error checking, and no comeback if it
fritzes your iTunes library.
Regards,
Nick
pp Mr Tea
--
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.