DEV Community

Cover image for Let's put the current iTunes track into the Tmux status line
Michael Welford
Michael Welford

Posted on • Originally published at michaelwelford.blog

Let's put the current iTunes track into the Tmux status line

When you go full terminal it can be nice to know what music is playing for when it really sucks/rocks. Wouldn't it be nice to get something like this into your Tmux status bar:

tmux status

(the answer is yes)

Let's do this

First you need to get the following into a script file, let's say we put this into ~/.config/itunes_status.sh.

#!/usr/bin/env bash

ITUNES_TRACK=$(osascript <<EOF
if appIsRunning("iTunes") then
  tell app "iTunes" to get the name of the current track
end if

on appIsRunning(appName)
    tell app "System Events" to (name of processes) contains appName
end appIsRunning
EOF

if [[ ! -z "$ITUNES_TRACK" ]]; then
  ITUNES_ARTIST=$(osascript <<EOF
  if appIsRunning("iTunes") then
      tell app "iTunes" to get the artist of the current track
  end if

  on appIsRunning(appName)
      tell app "System Events" to (name of processes) contains appName
  end appIsRunning
EOF

  TRACK_LEN=${#ITUNES_TRACK}
  if [[ "$TRACK_LEN" -gt 30 ]]; then
    ITUNES_TRACK=`echo "$ITUNES_TRACK" | cut -c -30`
    ITUNES_TRACK+=...
  fi

  ARTIST_LEN=${#ITUNES_ARTIST}
  if [[ "$ARTIST_LEN" -gt 20 ]]; then
    ITUNES_ARTIST=`echo "$ITUNES_ARTIST" | cut -c -20`
    ITUNES_ARTIST+=...
  fi

  echo '#[fg=#99a4bc]♫#[fg=#b4b4b9]' "$ITUNES_TRACK" '#[fg=#787882]-#[fg=#b4b4b9]' "$ITUNES_ARTIST"
  exit
else
  echo "#[fg=#787882]No music playing"
fi
Enter fullscreen mode Exit fullscreen mode

Essentially this does the following:

  • Check if iTunes is running, and if so grab the name of the track
  • If we have a track name, also grab the artist
  • If the track name is over 30 characters in length, truncate and insert an ellipsis
  • If the artist name is over 20 characters in length, do the same
  • Output this with some colours and space it out
  • Otherwise write "No music playing"

Insert into the Tmux status bar

Ok, so that's nice an' all but we need to get that into the Tmux status. That can be accomplished by inserting something like the following into your .tmux.conf:

set -g status-right "#(~/.config/itunes_status.sh) #[fg=#57575e]│ #[fg=white]%d/%m/%Y %H:%M "
Enter fullscreen mode Exit fullscreen mode

Note as a "bonus" I have also put in the time after the iTunes track (yes, yes, such a generous bonus). If you are wondering what those colour hex values are, they come from the falcon theme (made by moi). Don't forget that you will need to make this script executable too.

Top comments (0)