Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Shake 'n' Bake

macrumors 68020
Original poster
Mar 2, 2009
2,186
2
Albany
I'm trying to write an AppleScript that gives me the time remaining and total time of the currently playing iTunes track. I'd like it to be formatted as minutes:seconds / minutes:seconds. If that is impossible, seconds / seconds would work as well.

Here's what I've got so far:

on run
set info to ""
set info2 to ""
tell application "System Events"
set num to count (every process whose name is "iTunes")
end tell
if num > 0 then
tell application "iTunes"
if player state is playing then
set when to player position
set long to time of current track
time of long as string
set info to when & " / " & long as string
end if
end tell
end if
return info
end run

Now the problem is it can't get long as a string because it is formatted as 3:31 (the total time of the track). If I try to divide long by 60 to get the seconds, it won't work because it can't make 3:31 into a real number. Converting when to a minutes:seconds format would work just as well for me.

Thanks for your help.
 

Shake 'n' Bake

macrumors 68020
Original poster
Mar 2, 2009
2,186
2
Albany
Sorry for the third post in a row, but I've made some progress.

I now have this:

on run
set info to ""
tell application "System Events"
set num to count (every process whose name is "iTunes")
end tell
if num > 0 then
tell application "iTunes"
if player state is playing then
set when to player position
set whenStr to (when div 60) & ":" & (when mod 60) as text
set long to time of current track
set info to whenStr & " / " & long as string
end if
end tell
end if
return info
end run

And it works well, except for one thing. When the number of seconds is less than 10, there is no zero in front of it. It displays 3:5, rather than 3:05. Any ideas?
 

Hal Itosis

macrumors 6502a
Feb 20, 2010
900
4
Kinda rushed/busy right now to study your script. What i can offer at the moment is a handler i use in my iTunes-related script. Feed it total seconds and it formats a time string...

Code:
on TrueDuration(totalSecs)
	set theHours to totalSecs div 3600
	set theMins to (totalSecs mod 3600) div 60
	set theSecs to (totalSecs mod 60) as integer
	set hText to ""
	set mText to theMins as text
	set sText to theSecs as text
	if theHours > 0 then
		set hText to (theHours & ":") as text
		if theMins < 10 then set mText to "0" & mText
	end if
	if theSecs < 10 then set sText to "0" & sText
	return hText & mText & ":" & sText
end TrueDuration


set tStart to start of current track
set tStop to finish of current track
set x to my TrueDuration(tStop - tStart)

...but that may not be what you're asking for. (i use that function to calculate the actual length of songs... because iTunes ignores the offsets we might add to the start/stop times in the Options window).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.