How do I convert time into a string with AppleScript?

Discussion in 'macOS' started by Shake 'n' Bake, Jan 28, 2011.

  1. Shake 'n' Bake macrumors 68020

    Shake 'n' Bake

    Joined:
    Mar 2, 2009
    Location:
    Albany
    #1
    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.
     
  2. Hal Itosis macrumors 6502a

    Hal Itosis

    Joined:
    Feb 20, 2010
    #2
    maybe instead of "time" use:
    set long to duration of current track

    ?
     
  3. Shake 'n' Bake thread starter macrumors 68020

    Shake 'n' Bake

    Joined:
    Mar 2, 2009
    Location:
    Albany
    #3
    Excellent! That worked. After rounding to the nearest integer it's perfect. Thank you.
     
  4. Shake 'n' Bake thread starter macrumors 68020

    Shake 'n' Bake

    Joined:
    Mar 2, 2009
    Location:
    Albany
    #4
    Is there a way to change the seconds into the minutes:seconds format?
     
  5. Shake 'n' Bake thread starter macrumors 68020

    Shake 'n' Bake

    Joined:
    Mar 2, 2009
    Location:
    Albany
    #5
    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?
     
  6. Hal Itosis macrumors 6502a

    Hal Itosis

    Joined:
    Feb 20, 2010
    #6
    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).
     

Share This Page