Skip to content

Instantly share code, notes, and snippets.

@gp42
Created October 15, 2019 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gp42/ec32c80bbfb1b4bfed7ad51bd9a29bd2 to your computer and use it in GitHub Desktop.
Save gp42/ec32c80bbfb1b4bfed7ad51bd9a29bd2 to your computer and use it in GitHub Desktop.
Get Spotify info to tmux
#!/usr/bin/env ruby
# Return a short string with spotify status
require 'json'
sym = {
:play => '►',
:pause => '■'
}
truncate_length = 20
%x(pgrep -x 'Spotify')
exit 0 if $? != 0
def truncate(string, max)
string.length > max ? "#{string[0...max]}…" : string
end
def osascript(script)
%x(osascript -e '#{script}')
end
def spotify_info()
script=<<EOS
tell application "Spotify"
set dM to round ((duration of current track / 1000) / 60) rounding down
set dS to round ((duration of current track / 1000) mod 60) rounding down
set pM to round (player position / 60) rounding down
set pS to round (player position mod 60) rounding down
set info to "{"
set info to info & "\\"artist\\": \\"" & artist of current track as string & "\\","
set info to info & "\\"album\\": \\"" & album of current track as string & "\\","
set info to info & "\\"track\\": \\"" & name of current track as string & "\\","
set info to info & "\\"uri\\": \\"" & spotify url of current track as string & "\\","
set info to info & "\\"duration_min\\": \\"" & dM as string & "\\","
set info to info & "\\"duration_sec\\": \\"" & dS as string & "\\","
set info to info & "\\"state\\": \\"" & player state & "\\","
set info to info & "\\"position_min\\": \\"" & pM as string & "\\","
set info to info & "\\"position_sec\\": \\"" & pS as string & "\\""
set info to info & "}"
end tell
return info
EOS
JSON.parse(osascript(script))
end
# playing, paused
info = spotify_info
# do not show anything if not playing
exit 0 if info['state'] != 'playing'
info['state_icon'] = info['state'] == 'playing' ? sym[:play] : sym[:pause]
printf("[%s %s - %s]",
info['state_icon'],
truncate(info['artist'], truncate_length),
truncate(info['track'], truncate_length)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment