Home Writing

til / AppleScript: Spotify current position and track duration

Previously, we got the artist, track, and album name for what’s currently playing in Spotify. But, we are missing one piece that makes this information useful, how far along we’ve listened. So, let’s get the current position and the total duration of the track.

tell application "Spotify"
	-- Spotify data
	set currentPosition to player position
	set currentPosition to round currentPosition rounding down
end tell

We’ll start by getting the current player position. If we run the script with only the first line, we see that the number is a real, a number with decimals. Note that AppleScript returns the last value automatically. We’ll get an error if we try adding rounding to the first line, so we reassign the variable on the second line and tell AppleScript to round the value down. Note that comments are marked by --.

tell application "Spotify"
	-- Spotify data
	set currentPosition to player position
	set currentPosition to round currentPosition rounding down
	set totalDuration to duration of current track
end tell

If we run the code above, we’ll see that totalDuration is a large value. This is because duration is in milliseconds and currentPosition is in seconds. We’ll convert totalDuration to seconds by dividing by 1000.

tell application "Spotify"
	-- Spotify data
	set currentPosition to player position
	set currentPosition to round currentPosition rounding down
	set totalDuration to (duration of current track) / 1000
	
	-- Player position
	set current to my calculateTime(currentPosition)
	
	-- Total duration
	set total to my calculateTime(totalDuration)
	
	return "(" & current & " / " & total & ")"
end tell

We want to display both the current position and total duration as minutes:seconds. For this, we’ll create a function, calculateTime, that takes seconds and returns a string. Note that we need to add my before the function name. This tells AppleScript that it should look for a custom function. This keyword is only necessary inside a tell block.

on calculateTime(totalSeconds)
	-- Calculate minutes and seconds
	set min to (totalSeconds / 60)
	set s to round min mod 1 * 60
	set min to round min rounding down
	
	-- Handle if seconds is ever exactly 60
	if s is equal to 60 then
		set s to 0
		set min to min + 1
	end if
	
	-- Add leading zeros
	if s is less than 10 then
		set s to "0" & s
	end if
	
	return min & ":" & s
end calculateTime

We start by getting the number of minutes by dividing the provided number of seconds by 60. To get seconds, we use a modulo operation on the calculated minutes. This gives us the remainder, which is the number of seconds in decimal form. If we multiply this value by 60 we get the number of seconds. After that, we can round the minutes down to remove the remainder.

We also add two if sections, one that handles if seconds would ever be exactly 60 for whatever reason and one that adds leading zeros to seconds.

As mentioned earlier, AppleScript will return the last value automatically, so we don’t need to add the return keyword, but I think it makes it clearer.

The full code is provided below. If we copy this to “Script Editor” and run it, we’ll get something like "(1:12 / 2:07)" in the result section.

tell application "Spotify"
	-- Spotify data
	set currentPosition to player position
	set currentPosition to round currentPosition rounding down
	set totalDuration to (duration of current track) / 1000
	
	-- Player position
	set current to my calculateTime(currentPosition)
	
	-- Total duration
	set total to my calculateTime(totalDuration)
	
	return "(" & current & " / " & total & ")"
end tell

on calculateTime(totalSeconds)
	-- Calculate minutes and seconds
	set min to (totalSeconds / 60)
	set s to round min mod 1 * 60
	set min to round min rounding down
	
	-- Handle if seconds is ever exactly 60
	if s is equal to 60 then
		set s to 0
		set min to min + 1
	end if
	
	-- Add leading zeros
	if s is less than 10 then
		set s to "0" & s
	end if
	
	return min & ":" & s
end calculateTime

  • Loading next post...
  • Loading previous post...