Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'GeekTool - Useful and fun info on the desktop' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
GeekTool - Useful and fun info on the desktop
Authored by: adhesiv on Jul 09, '04 09:47:06PM
I modified my script a little...ok a lot...the output of this will look like this:

[Staind]  :||:  Price to Play  :||:  3:11 / 3:41


the timer will adjust depending on the position of the track and updates in real time going from 0:00 to 3:41 (in this case)

It also checks to see if iTunes is playing, paused, stopped, rewinding, or fast forwarding and will output the track title regardless of the state the player is in. This was mentioned to me by Frederico (thanx!).


--iTunesTracker: Copyright 2000 - Jayson Cote

property go1 : false
-- check to see if iTunes is running
tell application "System Events"
	set the process_flag to (exists process "iTunes")
end tell
if the process_flag then
	tell application "iTunes"
		set d to duration of current track
		set e to player position
		set rT to my doTime(e)
	end tell
	-- check to see if iTunes is playing
	tell application "iTunes"
		if player state contains playing then set go1 to true
	end tell
	if go1 contains false then
		-- check to see if iTunes is paused
		tell application "iTunes"
			if player state contains paused then set go1 to true
		end tell
	end if
	if go1 contains false then
		-- check to see if iTunes is stopped
		tell application "iTunes"
			if player state contains stopped then set go1 to true
		end tell
	end if
	if go1 contains false then
		-- check to see if iTunes is rewinding
		tell application "iTunes"
			if player state contains rewinding then set go1 to true
		end tell
	end if
	if go1 contains false then
		-- check to see if iTunes is fast forwarding
		tell application "iTunes"
			if player state contains fast forwarding then set go1 to true
		end tell
	end if
	if go1 then
		-- do what you need to do
		tell application "iTunes"
			set foo1 to artist of current track
			set foo2 to name of current track
			set foo3 to time of current track
			set foo4 to rT & " / " & foo3
			set foo5 to "[" & foo1 & "]" & "  :||:  " & foo2 & "  :||:  " & foo4
		end tell
	end if
end if
on doTime(x)
	set m to x div 60
	set s to (x - m * 60)
	if s < 10 then set s to "0" & s
	return m & ":" & s
end doTime


[ Reply to This | # ]
GeekTool - Useful and fun info on the desktop
Authored by: Frederico on Jul 11, '04 04:35:45AM
I like that you've taken an extra step or three to look for additional Player states; and testing your script exposes some weaknesses in mine that I just wrapped entirely in one big error handler; something your script lacks and really needs. E.G., try this: open your script in Script Editor; observe the 'Event Log History'; open up the iTunes Music Store; find and play any sample; run your script:

tell application "System Events"
	exists process "iTunes"
		true
end tell
tell application "iTunes"
	get player state
		playing
	get duration of current track
	-->	"iTunes got an error: Can't get duration of current track."

Part of the problem is that you're asking iTunes to return information about song data before you even know the Player State; but, as the above test proves, even a player state check first, with streamlined code to reduce the excessive requests in your original check for Player State (i.e., if your Player is Fast Forwarding, your script makes five attempts to figure that out; too slow; taxes osascript and CPU cycles) followed by an info request can fail in certain states when the player is still Playing. Observe:


property go1 : false
-- check to see if iTunes is running
tell application "System Events"
	set the process_flag to (exists process "iTunes")
end tell
if the process_flag then
	tell application "iTunes"
		-- check to see if iTunes is playing
		set go1check to player state
		if go1check contains playing or go1check contains paused or go1check ¬
			contains stopped or go1check contains rewinding or go1check contains fast forwarding ¬
			then set go1 to true
	end tell
	if go1 then
		-- do what you need to do
		tell application "iTunes"
			set d to duration of current track
			set e to player position
			set rT to my doTime(e)
			set foo1 to artist of current track
			set foo2 to name of current track
			set foo3 to time of current track
			set foo4 to rT & " / " & foo3
			set foo5 to "[" & foo1 & "]" & "  :||:  " & foo2 & "  :||:  " & foo4
		end tell
	end if
end if
on doTime(x)
	set m to x div 60
	set s to (x - m * 60)
	if s < 10 then set s to "0" & s
	return m & ":" & s
end doTime
Result:

tell application "System Events"
	exists process "iTunes"
		true
end tell
tell application "iTunes"
	get duration of current track
		-->"iTunes got an error: Can't get duration of current track."
So, while we have a Player State that is Playing, and we could even meet any one of the other five states while in the Music Store, or other similar condition, we choke on duration. Try reordering the info calls, and you get some interesting results:

    -->"iTunes got an error: Can't get time of current track."
    -->"iTunes got an error: Can't get artist of current track."
    -->"iTunes got an error: Can't get duration of current track."
    -->"iTunes got an error: Can't get name of current track."
get player position
	-->	0
Interesting. So, with some logical deductions and some error handlers, we can probably, through testing various Player States, return more useful data to GeekTool. Now, I'm still using iTunes 4.1 and iTunes 4.5, and I think iTunes 4.6 actually deals with this better by allowing for more Shared Library and Music Store calls, plus you could probably get the current view of the browser to return more useful info, so I don't know how much effort its worth. That said, assuming they didn't, here's one iteration that not only streamlines your code more, but also handles at least the Music Store failure a bit more gracefully:

property go1 : false
-- check to see if iTunes is running
tell application "System Events"
	set the process_flag to (exists process "iTunes")
end tell
if the process_flag then
	tell application "iTunes"
		-- check to see if iTunes is playing
		set go1check to player state
		if go1check contains playing or go1check contains paused or go1check ¬
			contains stopped or go1check contains rewinding or go1check contains fast forwarding ¬
			then
			set go1 to true
		else
			return "iTunes Player State unknown"
		end if
		if go1 then
			-- do what you need to do
			set e to player position
			set rT to my doTime(e)
			try
				set {foo1, foo2, foo} to {artist, name, time} of current track
			on error
				set {foo1, foo2, foo3} to {"iTunes Music Store", "(unknown sample track)", ""}
			end try
			set foo4 to rT & " / " & foo3
			set foo5 to "[" & foo1 & "]" & "  :||:  " & foo2 & "  :||:  " & foo4
		end if
	end tell
	return foo5
end if
on doTime(x)
	set m to x div 60
	set s to (x - m * 60)
	if s < 10 then set s to "0" & s
	return m & ":" & s
end doTime

--> "[iTunes Music Store]  :||:  (unknown sample track)  :||:  0:02 / "
Anyway, it's all fun playing around with new ways to skin cats. Cheers

[ Reply to This | # ]
GeekTool - Useful and fun info on the desktop
Authored by: Frederico on Jul 11, '04 04:43:50AM
Oops! There's an error in the last line of that error handler; it reads:

try
set {d, foo1, foo2, foo} to {duration, artist, name, time} of current track
on error
Should read:

try
set {d, foo1, foo2, foo3} to {duration, artist, name, time} of current track
on error
Sorry!

[ Reply to This | # ]
GeekTool - Useful and fun info on the desktop
Authored by: Frederico on Jul 11, '04 06:47:45AM

Hmmmm... never mind that I neglected to account for the Radio playlist being active... Good thing I rarely ever listen to it.... (;



[ Reply to This | # ]
GeekTool - Useful and fun info on the desktop
Authored by: Frederico on Jul 12, '04 08:45:00PM
This version accounts for both Radio and Music Store:

--iTunesTracker: Copyright 2000 - Jayson Cote -- modified by Frederico
tell application "System Events" to set the process_flag to (exists process "iTunes")
if the process_flag then
	tell application "iTunes"
		set ps to player state
		if ps contains playing or ps contains paused or ps ¬
			contains stopped or ps contains rewinding or ps contains fast forwarding ¬
			then
			set e to player position
			set rT to my doTime(e)
			try
				set {foo1, foo2, foo3} to {artist, name, time} of current track
				if foo3 is missing value then set {foo1, foo2, foo3} to {"Internet Radio: " & foo2, current stream title, "(stream)"}
			on error
				set {foo1, foo2, foo3} to {"iTunes Music Store", "(unknown sample track)", "0:30"}
			end try
			set foo4 to rT & " / " & foo3
			set foo5 to "[" & foo1 & "]" & "  :||:  " & foo2 & "  :||:  " & foo4
		else
			return "iTunes Player State unknown"
		end if
	end tell
	return foo5
end if
on doTime(x)
	set m to x div 60
	set s to (x - m * 60)
	if s < 10 then set s to "0" & s
	return m & ":" & s
end doTime


[ Reply to This | # ]