Tmux Now Playing
With a little AppleScript we can get our tmux status bar to display the currently playing song in Spotify or iTunes (or any other media player).
AppleScript
AppleScript appeared on Mac OS back in 1993 with the purpose of allowing end-users to easily, programatically control applications and modify data in documents. It works off of Apple Events, standardized data that the OS sends to applications when fired. The syntax is easy enough to read so I won't spend time explaining.
set spotify_state to false
set itunes_state to false
if is_app_running("Spotify") then
tell application "Spotify" to set spotify_state to (player state as text)
end if
if is_app_running("iTunes") then
tell application "iTunes" to set itunes_state to (player state as text)
end if
(* Whatever other music applications you use *)
if spotify_state is equal to "playing" then
tell application "Spotify"
set track_name to name of current track
set artist_name to artist of current track
return track_name & " - #[bold]" & artist_name & "#[nobold]"
end tell
else if itunes_state is equal to "playing" then
tell application "iTunes"
set track_name to name of current track
set artist_name to artist of current track
return track_name & " - #[bold]" & artist_name & "#[nobold]"
end tell
else
return "Nothing playing :("
end if
on is_app_running(app_name)
tell application "System Events" to (name of processes) contains app_name
end is_app_running
As you can see the basic idea is:
• Check for open media players
• Check state of players
• Get song information
• Return formatted stringsp
Bash Heredocs
What's also nice about AppleScript is that you can execute it from command line with the osascript
command. Further, instead of saving our AppleScript in a second file we can take advantage of bash heredocs. So instead of running our AppleScript we can encapsulate this in a bash script for extra manipulation if we want:
#!/bin/bash
NOW_PLAYING=$(osascript <<EOF
set spotify_state to false
set itunes_state to false
...
on is_app_running(app_name)
tell application "System Events" to (name of processes) contains app_name
end is_app_running
EOF)
echo $NOW_PLAYING
tmux Status Bar
Directly from the tmux man pages for setting status bar text:
status-left string
Display string (by default the session name) to the left of the
status bar. string will be passed through strftime(3) and for-
mats (see FORMATS) will be expanded. It may also contain any of
the following special character sequences:
Character pair Replaced with
#(shell-command) First line of the command's output
#[attributes] Colour or attribute change
## A literal `#'
The #(shell-command) form executes `shell-command' and inserts
the first line of its output. Note that shell commands are only
executed once at the interval specified by the status-interval
option: if the status line is redrawn in the meantime, the pre-
vious result is used. Shell commands are executed with the tmux
global environment set (see the ENVIRONMENT section).
I used the attributes bold
for the artist's name and then turned it back off with nobold
. Similarly there is a command status-right
which is what I preferred.
Putting it all together
So we have our AppleScript which generates the text to display, the bash script file to boostrap the AppleScript which actually gets executed, and finally our tmux.conf which updates the status bar with our song information.
Now to wire it all up we just need we just need to add this line to our .tmux.conf:
set -g status-right "#(~/.tmux/current_track.sh) #[fg=red]::#[fg=brightblue] #[bold]%d#[nobold] %b #[fg=red]::#[fg=brightblue]%l.%M %p "
Note
I made a few other modifications like lowering the status-interval
of the time in between refreshing the bar. I also increased the size of the right status bar since some song names are long.