Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

vbs script for iTunes converted for use on Mac?

This guy wrote an awesome vbs script that will create an intervaled playlist for runners, where you can set your intervals — for example, run for five minutes, walk for 2 — and it will automatically truncate the songs in your playlist for you. It even puts a "ding" in between each interval so you know when it ends and you never have to consult your stopwatch again.

The problem? It only works on Windows! (boo!) I have never written an AppleScript before — would it be possible for someone to translate this file so it works on my Mac?

IBook, Mac OS X (10.4.5)

Posted on Mar 10, 2006 3:57 PM

Reply
Question marked as Best reply

Posted on Mar 10, 2006 4:19 PM

What's there to translate?

with timeout of 10000 seconds
tell application "iTunes"
play playlist "run" -- change to actual playlist name
delay (5 * minutes)
play playlist "walk" -- change to actual playlist name
delay (2 * minutes)
end tell
end timeout

[edit] :o) it's can get very complicated: see this http://hac.95mb.com/Techs.html, those two apps were written in AppleScript.
18 replies
Question marked as Best reply

Mar 10, 2006 4:19 PM in response to Brittanie Hoofard

What's there to translate?

with timeout of 10000 seconds
tell application "iTunes"
play playlist "run" -- change to actual playlist name
delay (5 * minutes)
play playlist "walk" -- change to actual playlist name
delay (2 * minutes)
end tell
end timeout

[edit] :o) it's can get very complicated: see this http://hac.95mb.com/Techs.html, those two apps were written in AppleScript.

Mar 10, 2006 8:03 PM in response to Brittanie Hoofard

my bad, you mean stopping at the end of 2 minutes of playlist "walk"?

with timeout of 10000 seconds
tell application "iTunes"
play playlist "run" -- change to actual playlist name
delay (5 * minutes)
beep
play playlist "walk" -- change to actual playlist name
delay (2 * minutes)
beep
stop
end tell
end timeout

or for a real interval work out:

with timeout of 10000 seconds
repeat 10 times -- this is ten interval, that 70 min 🙂
tell application "iTunes"
play playlist "run" -- change to actual playlist name
delay (5 * minutes)
beep
play playlist "walk" -- change to actual playlist name
delay (2 * minutes)
beep
end tell
end repeat
end timeout

IMHO: do it outside... Luke, don't let the machines rule you. :o)

Mar 11, 2006 2:04 AM in response to Michael Henley

You are correct — the idea is for the script to work on my iPod, not really for iTunes.

I know you can set end times for each song manually, but with 100+ songs in a playlist I didn't really want to do that. Also, if I'm running 5-2 intervals, maybe some songs are less than 5 minutes long, so I would want the playlist to go on to the next song, or keep playing until the five minutes is up. (That's too much adding of song lengths to do for 45 minutes of running, anyway).

The above script works for iTunes (haven't tried it on my iPod yet) but there are two problems. First, I'm still not hearing the beep at all. Also, the songs are truncated, but for each of the two playlists, it keeps starting from the beginning, so really I'm just hearing the same two songs over again. I'd rather it pick tracks from each playlist randomly as each interval starts.

The Windows script download includes a track called "ding" which I've already added to my playlist. If I were to add a command to play track ding at the end of each interval I think that would be great, but the question remains: how to have random playlists and how to get this script to work on my iPod.

I've been researching on this guy's site, he has some cool scripts but I'm still a freshman at writing this stuff on my own.

Thanks for all your help, everyone. Keep it comin'.

IBook Mac OS X (10.4.5)

Mar 11, 2006 3:17 AM in response to Brittanie Hoofard

Assuming you haven't replaced your iPod's firmware with iPodLinux, you can consider the iPod software sealed. In other words, it's not going to run an AppleScript. To get around this all we need to do is manipulate two basic features of the iPod:

The iPod...
A. respects arbitrary end points set by individual tracks.
B. will play a static playlist in order.

So as you can imagine this requires a somewhat fragile arrangement be set up ahead of time in iTunes. The steps might be something like this:

You...
1. create a playlist with all the tracks you want to listen to on your run.
2. select the playlist in iTunes and run the AppleScript.

The Script...
1. gets a list of tracks in the original playlist.
2. creates a new blank static playlist.
3. gets the length of the first track in the track list.
4. if the track is less than five minutes, it adds the track to the new playlist, and gets the length of the next track from the track list.
5. if the length of the next track plus the length of all previously added tracks is greater than five minutes, the script sets an arbitrary end point in the next track at the five minute total-elapsed-time mark.
6. the modified track is added to the new playlist.
7. the 'ding' track is added to the new playlist.
8. steps 4 thru 7 repeat, alternating between a five and two minute limit, until the script reaches the end of the track list.

You...
3. add the new playlist to your iPod. (by syncing, etc.)
4. play the new playlist on your run.

Now, there are a couple problems with this set up. For one, there is no mechanism for resuming a track that has been interrupted for a ding. You could re-play the track from the beginning, but it will still end prematurely. The only way around that would be to have the script actually duplicate the physical mp3 file and set the start time of the copy to match the end time of the original.

Another problem arises when you want to listen to a truncated track normally (either on your mac or the iPod). In this case you'll want a script that can undo all the changes the first script made. It would delete the generated playlist (it's useless without a strict arrangement of tracks) and set all of the track end points to the actual length of each track.

Mar 11, 2006 3:21 AM in response to Michael Henley

Resuming a truncated track is not that big of a deal to me. In fact, the track ending abruptly is probably better because when I'm in the running zone I need something more jarring to make me realize it's time to stop.

As far as the "undo" script, the Windows version also offers this. But does running this script on a certain playlist mean that the songs in that playlist will also play truncated in other playlists as well?

IBook

IBook Mac OS X (10.4.5)

Mar 11, 2006 3:34 AM in response to Brittanie Hoofard

But does running this script on a certain playlist mean that the songs in that playlist will also play truncated in other playlists as well?

Yes. A playlist is merely a collection of references to individual files and their database entries. Changing a property of a track from a playlist is the same as changing it from the Library or anywhere else, because every reference points to the same data.

Mar 11, 2006 7:37 AM in response to Brittanie Hoofard

Okay, try this.

click here to open this script in your editor <pre style="font-family: 'Monaco', 'Courier New', Courier, monospace; overflow:auto; color: #222; background: #DDD; padding: 0.2em; font-size: 10px; width:400px">property run_min : 5
property walk_min : 2

tell application "iTunes"
set source_pl to the view of the front browser window
try
set ding_track to the first track of the first playlist whose name is "Ding"
on error
display dialog "Please make sure a track with the title \"Ding\" is in your iTunes library before running this script." buttons {"Cancel"} default button 1
end try
if the name of source_pl is "Library" then if the button returned of (display dialog "It appears that you have selected your entire iTunes Library (" & (count tracks of the source_pl) & " tracks), are you sure you want to continue?" buttons {"why not?", "NO!!!!!!!!"} default button 2) is not "why not?" then
error number -128
else if the text returned of (display dialog "...because the script will take awhile, and you'll end up with annoying end points inserted randomly throughout your entire library, that's why. If you're still certain this is what you want, type \"yes\" below and click Nuke It, or you can Chicken Out instead." default answer "" buttons {"Nuke It", "Chicken Out"} default button 2) is not "yes" then
error number -128
end if
set {error_msg, error_str} to {"", "Syntax Error!" & return & return & "To enter fractions of a minute use:" & return & "1.5 (for 1 min 30 sec)" & return & "5.25 (for 5 min 15 sec)" & return & "etc." & return & return}
repeat
try
set run_min to text returned of (display dialog error_msg & "Set the running interval: (in minutes)" default answer run_min) as number
if result ≤ 0 then error
set error_msg to ""
repeat
try
set walk_min to text returned of (display dialog error_msg & "Set the walking interval: (in minutes)" default answer walk_min) as number
if result ≤ 0 then error
exit repeat
on error number x
if x is -128 then error number -128
end try
set error_msg to error_str
end repeat
set {run_sec, walk_sec} to {run_min * minutes, walk_min * minutes}
exit repeat
on error number x
if x is -128 then error number -128
end try
set error_msg to error_str
end repeat
set new_pl to "*" & source_pl's name
repeat
if not (playlist new_pl exists) then
set new_pl to make new playlist with properties {name:new_pl}
exit repeat
else if button returned of (display dialog "A playlist with the name \"" & new_pl & "\" already exists. What would you like to do?" buttons {"Cancel", "Use Different Name", "Replace Playlist"} default button 3) is "Use Different Name" then
set new_pl to text returned of (display dialog "Enter a different name for the new playlist:" default answer new_pl & space & (do shell script "date '+%y-%b-%d %H.%M.%S'"))
else
set new_pl to playlist new_pl
exit repeat
end if
end repeat
set the view of the front browser window to new_pl
delete tracks of new_pl
set {trip_tally, trip_time} to {0, run_sec}
repeat with a_track in source_pl's tracks
set a_length to the duration of a_track
if a_length + trip_tally > trip_time then
set finish of a_track to trip_time - trip_tally
duplicate a_track to new_pl
duplicate ding_track to new_pl
set trip_tally to 0
if trip_time is run_sec then
set trip_time to walk_sec
else
set trip_time to run_sec
end if
else
set trip_tally to trip_tally + a_length
duplicate a_track to new_pl
end if
end repeat
end tell
</pre>Note that since the default values are persistent properties, you won't need to edit the script to change them. Any new value will be automatically remembered between runs.

Here is another script to undo changes made by the first script. Note here that it doesn't really clear the arbitrary end, it merely matches it to the length of the track. I'm not sure there is a way to accomplish the former, but this may be good enough:

click here to open this script in your editor <pre style="font-family: 'Monaco', 'Courier New', Courier, monospace; overflow:auto; color: #222; background: #DDD; padding: 0.2em; font-size: 10px; width:400px">tell application "iTunes" to repeat with a_track in tracks of the front browser window's view
tell a_track
get duration -- this needs to be on its own line for some reason
set finish to result
end tell
end repeat</pre>

Mar 11, 2006 4:37 PM in response to Michael Henley

Michael,

Those scripts worked great! Thank you so much. The creator of the original script said several peple had requested a Mac version, but he didn't know how to create one. I'd like to send him a link to this forum so those who want to use this script can get it, if you don't mind.

I was wondering — if I've already created a playlist for running, then could I replace the statement 'source_pl is "Library"' with something like "is playlist "Playlist name" so it doesn't pull every song in my Library?

Thanks again for working this out for me.

IBook Mac OS X (10.4.5)

IBook Mac OS X (10.4.5)

Mar 11, 2006 6:07 PM in response to Michael Henley

Nevermind, nevermind. You are correct. Do you mind if I send this link out for others, since it does exactly what I wanted it to do?

Also, just a note for other users — the "ding" seems to stand out more if you increase the playback options for a longer crossfade playback.

Michael, you are the best!

IBook Mac OS X (10.4.5)

vbs script for iTunes converted for use on Mac?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.