/dev/trouble
Eric Roller's Development Blog

iTunes Script for Circuit Training

- Posted in General by

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.

Note: There exists a newer version of this script!

(* AppleScript for circuit training with iTunes.
The default circuits are 40 seconds each, with pauses of 15 seconds.
At the beginning, the script asks for different times.

©2009, Eric Roller
*)

-- begin by asking for the workout and delay times
set tWork to the text returned of ¬
    (display dialog "Set the Workout Time [s]" default answer "40")
set tPause to the text returned of ¬
    (display dialog "Set the Delay Time [s]" default answer "15")

-- whether to skip to the next song, but only every other time
set skip to false

-- enter the forever loop
repeat
    -- start playing the music
    set volume without output muted
    tell application "iTunes" to play

    -- delay for 40 seconds by means of a dialog
    set answer to display dialog tWork & " Seconds Workout" ¬
        buttons "Stop" with title "Circuits Loop" ¬
        giving up after tWork
    -- stop if requested
    if the button returned of the answer is "Stop" then exit repeat

    -- stop the music, actually just mute it
    set volume with output muted
    if skip is true then
        -- every other time, change the track
        tell application "iTunes" to next track
    end if
    set skip to not skip

    -- wait 15 seconds (playing the song muted)
    set answer to display dialog tPause & " Seconds Pause" ¬
        buttons "Stop" with title "Circuits Loop" ¬
        giving up after tPause
    if the button returned of the answer is "Stop" then exit repeat
end repeat

-- finish by unmuting the volume and pausing iTunes
tell application "iTunes" to pause
set volume without output muted

Note that we go to the next track after every other workout. Also, since the volume is muted, we skip the first 15 seconds of every song. This is deliberate!