The Terminal, bindkey, and iTunes

Dec 13, '04 09:19:00AM

Contributed by: benholt

I stopped using Butler and/or LaunchBar just to be more geeky by using the Terminal (don't tell me no one's ever done this before). I set up aliases fine, but encountered a problem setting up my 'Function keys (fkeys) to iTunes' functions. I knew to use bindkey, but couldn't find the special code for the fkeys anywhere -- you can't just put "F1."

After almost giving up, I stumbled upon a great Unix feature: Control-V. Press that in the Terminal and then hit any fkey, and it will give you the magic code to use, even pasting it if you're using vi. Then, put these lines (or something similar) in your ~/.cshrc (if using tcsh):

#Key bindings for iTunes
bindkey -c "^[OP" "sh /Users/your_user/scripts/previous"
bindkey -c "^[OQ" "sh /Users/your_user/scripts/playpause"
bindkey -c "^[OR" "sh /Users/your_user/scripts/next"
bindkey -c "^[OS" "sh /Users/your_user/scripts/tuneinfo"
You can use a different path to the scripts if you want. After that, you need to make those scripts. A sample one is:
#!/bin/sh
echo "Play/Pause"
osascript <<ENDSCRIPT
  tell application "iTunes"
  playpause
  set theCurrentTrack to the current track
  set theBand to the artist of theCurrentTrack as string
  set theSong to the name of theCurrentTrack as string
  return (theSong & " - " & theBand)
  end tell
ENDSCRIPT
For next and previous functions, just copy the first script and change playpause to next track and previous track, and the script must be named to match what you put in ~/.cshrc. Then log out, open a new terminal window, and voila! I am using these fkeys: These only work in the Terminal but this is OK, since other apps I use (namely World of Warcraft) use the fkeys.

[robg adds: I tested this one and it works as described (remember to make the scripts executable with chmod 755 script_name), but I'll stick with Butler -- I like having access to the keys from apps other than the Terminal.]

Comments (5)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20041210120032130