We can use iTunes to play music while we do circuit training, i.e. 40 seconds music while we work out, and 15 seconds pause while we rest or go to the next station:
- save the following AppleScript in ~/Library/iTunes/Scripts; you will probably have to create the Scripts directory;
- open iTunes with a suitable playlist; we recommend a good song and a Genius playlist;
- select the first song;
- launch the script from Scripts menu.
NB. This is an updated version of a previous script.
(* AppleScript for circuit training with iTunes.
The default circuits are 40 seconds each, with pauses of 15 seconds.
At the beginning, the script allows you to set different times.
After every second set, iTunes is asked to go to the middle section
of the next song.
version 2.1, ©2010, Eric Roller
*)
property wCount : 40
property tWork : 40
property tRest : 20
property tFudge : 1.0 -- use this to adjust the rest times
on run
-- begin by asking for the workout and delay times
set wCount to askUser("Number of Workouts", wCount)
set tWork to askUser("Workout Time in Seconds", tWork)
set tRest to askUser("Rest Time in Seconds", tRest)
-- whether to skip to the next song, but only every other time
set skip to false
set remaining to wCount
try
with timeout of 6000 seconds
-- enter the workout loop
repeat while (remaining > 0)
announceCount(remaining, wCount)
-- wait, play the music, wait and then stop it again
sleepFor(tRest, "Rest")
tell application "iTunes" to play
sleepFor(tWork, "Workout")
tell application "iTunes" to pause
if skip is true then
-- every other time, change the track
tell application "iTunes" to next track
-- skip to the middle section of the next track
set tForward to trackSeconds() / 2 - tWork
tell application "iTunes" to ¬
set the player position to tForward
end if
set skip to not skip
set remaining to remaining - 1
end repeat
end timeout
on error errText number errNum
if errNum is not -128 then ¬
display dialog "Error: " & errNum & return & errText
end try
-- finish by pausing iTunes
tell application "iTunes" to stop
-- work out how many iterations we did
set remaining to wCount - remaining
if remaining is greater than 0 then ¬
say "Well done! You completed " & (remaining as text) ¬
& " workouts."
end run
-- small helper to ask the user for an integer
on askUser(aText, aDefault)
set anInt to the text returned of ¬
(display dialog "Set the " & aText default answer aDefault)
return (anInt as integer)
end askUser
-- helper to announce how many more workouts there are
on announceCount(aCount, maxCount)
if aCount = maxCount then
set message to "Starting " & (aCount as text) & " workouts."
else if aCount = 1 then
set message to "Last one."
else if ((2 * aCount) as integer = maxCount) then
set message to "Half time."
else if (random number from -1.0 to 1.0) > 0.0 then
set message to (aCount as text) & " more"
else
set message to (aCount as text) & " to go"
end if
delay (tFudge / 2)
say message without waiting until completion
end announceCount
-- helper to wait for a given amount of seconds
on sleepFor(tDelay, workType)
set tOut to tDelay
-- apply a fudge factor for the rest times
if workType is "Rest" then set tOut to tDelay - tFudge
-- we use a dialog message with a delay
set answer to display dialog (tDelay as text) & " Seconds " ¬
& workType buttons "Stop" with title "Circuits Loop" ¬
giving up after tOut
-- trigger an error if we want to stop
if the button returned of the answer is "Stop" then ¬
error "User canceled." number -128
end sleepFor
-- helper to determing the length of the current track in seconds
on trackSeconds()
-- ask iTunes for the track length (MM:SS)
tell application "iTunes" to get the time of the current track
set the tLength to the result
-- split the length into minutes and seconds
set saveDeli to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set tMinutes to the first text item of the tLength
set tSeconds to the last text item of the tLength
set AppleScript's text item delimiters to saveDeli
-- add up the total length in seconds
return (tSeconds + 60 * tMinutes)
end trackSeconds