Thursday, January 31, 2008

Workout Generator Applescript: How I Managed to Nerd-ify Even the Most Simple of Sports

As some of you might have noticed from my "Project Fatass" post below, I'm trying to run a 5k in May. In order to do that, I probably need to be able to run a 1k first. I'm working on that.

Of course, since I use a computer---and AppleScript---for just about everything else, I figured I'd apply it to running as well. But unlike a lot of the other scripts I write, I think other people will find this useful.

In order to get to the point where I can run a 5k without dying, I'm following the fantastic Couch to 5k Running Plan, or to keep the branding consistent with my other efforts, the "Fatass to 5k plan." It's an interval plan that starts out with alternating between 60 seconds of running and 90 seconds of walking for the first week. By the ninth week, you're running 3 miles without stopping.

Of course, to do an interval plan like that, you need a way to reliably time the segments. I didn't want to have to be staring at a watch the whole time, so I got the idea of having iTunes do it for me. And since everything on a Mac is insanely scriptable, it turned out to be relatively easy.

This script makes use of the "say" capability of OS X, and with the new voice in Leopard, it's almost creepy how good the voice sounds. The script generates a playlist that can be loaded on any iPod (I'm using Katie's shuffle). The playlist announces when to start running and start walking, and it inserts appropriately-timed songs for each leg of the workout. So right now, it tells me to walk for 5 minutes, plays a song or two, tells me to run for 60 seconds, plays one minute of a randomly-selected song, tells me to walk to 90 seconds, plays a minute and a half of a different song, and so on.

I've attached the script below. It's VERY MUCH in beta right now. I plan on adding a nice UI once I learn Xcode, but in the meantime, here's how to use it:

1. Go into iTunes and create playlists that you want to use for workout music. I have 2: "Running Music" and "Walking Music," each with the appropriate energy level for the activity. You can have as many as you'd like, or use your entire library, or whatever.

2. Open the script and edit theWorkout array to include the pattern of your workout. Mine mainly alternates between walking and running, but your workout might include sprinting, running backwards, spinning in circles, or any number of other things. (Mine should probably include stretching after the warmup walk.)

2. Edit theWorkoutTimes to include the time of each leg of the workout in minutes. The first time in this array corresponds to the first leg in theWorkout.

3. Edit theWorkoutPlaylists with the name of the playlist you want to use for each section. Don't screw this up, because I haven't included error handling yet.

4. Edit the value of theNewPlaylistName to specify the name of the new playlist to be generated. Set theTempFile to a location where the script can store a temporary audio file it generates for the speech bits.

5. Set AnnounceLeg to indicate whether or not you want the speech to say "leg x of y" before each leg. Set it to false, and it won't announce that.

6. Set announceTime to true if you want the voice to announce how long each leg is.

7. Click "Run" and let the magic happen. It usually takes 2-3 minutes, depending on how many files the script needs to generate. Here's what it does:

For each announcement, the script generates a temporary .aiff file, then imports it into the library and adds it into the playlist. Every time a new leg starts, it grabs a random song from the specified playlist, imports a new copy of the file, sets the end time of the file so the song is an appropriate length, and adds it to the new playlist. All of the "artist" fields of the new songs are set to the same as the new playlist name, so after you're done, you can just go delete everything with that Artist name.

Download version 0.1 here and let me know what you think!

Workout Generator.zip - version 0.1

1 comment:

  1. This sounds like exactly what I have been looking for... only one problem.. I only have PC's available to me... any chance there is something like this for us non-apple folk?

    thanks

    ReplyDelete