if its too loud, turn it down

Monday, June 29, 2009

Use applescript and keyboard shortcuts to skip forward or back in iTunes

When I'm listening to long audio files, like podcasts or time-shifted programs, often I want to skip ahead an exact duration of time. For example, if the program has commercials, I want to skip ahead exactly 3 minutes. If I overshoot, then I want to skip back 10 seconds until I hit the program's return from break. I set up a set of applescripts that I trigger with Quicksilver keyboard shortcuts (see short how-to on adding hotkeys with quicksilver). This prevents me from having to actually go to iTunes, and manually move the time slider forward and back until I get the right position.

Here's the script for moving forward 3 minutes:
tell application "iTunes"
 if player state is playing then
  -- length of current track:
  set trackTime to duration of current track
  --get the current position time:
  set currTime to get player position
  set currSkip to currTime + 180
  
  -- checks if new position is greater than 
  -- length of track, and corrects it if it is:
  if currSkip > trackTime then
   set currSkip to trackTime
  end if
  
  set player position to currSkip --skip to new position
 end if
end tell
...and here's one for skipping back 10 seconds:
tell application "iTunes"
 if player state is playing then
  --get the current track time:
  set currTime to get player position
  if currTime < 10 then
   --go to start of the track:
   set currSkip to 0
  else
   --otherwise, skip backwards 
   set currSkip to currTime - 10
  end if
  
  set player position to currSkip --skip to new position
 end if
 
end tell
Note that the time is kept in seconds (not minutes). So to create scripts for varying amounts of time just edit the seconds and save as a separate script. Save in your user's "scripts" folder, ~/Library/Scripts/ as something like "skip3min.scpt".

3 comments:

  1. Hello, I'm a newbie in applescripting (and when I say "newbie" I mean I started reading this post... :-)
    I'm using the widget Harmonic which auto-embeds lyrics in the mp3s (or at least it tries...)
    It works quite simply, when dashboard is opened and a song is playing it automatically looks for the lyrics online and embeds them if it finds them, in a couple of seconds.
    What I would like my mewbie-beginner-lazy script to do for me is to go to the next track after every 5 or 10 seconds from the beginning of every track so to have them all filled with lyrics.
    Now the questions: how can I tell iTunes to do this after 5 seconds? Do I have to loop it? How? Can I then tell him to make a playlist with just the tracks with no lyrics? (maybe this is already a step forward...)

    Thanks for the help, I hope I won't have to ask such stupid questions in the future... :-P

    ReplyDelete
  2. Not a stupid question at all. It's a good idea. I'm unfamiliar with this widget "Harmonic", I think it will depend on how much applescript support it has. If you open Script Editor then go File->Open Dictionary and then find the Harmonic program (and open it) you can see what is supported.

    ReplyDelete
  3. Wow. I'd been looking for a rewind script for a long time. This works perfectly.

    ReplyDelete

Note: Only a member of this blog may post a comment.