Skip to content

Instantly share code, notes, and snippets.

@aalin
Last active November 20, 2016 13:28
Show Gist options
  • Save aalin/a19f54b80cb480c49b2440cc8a04c33d to your computer and use it in GitHub Desktop.
Save aalin/a19f54b80cb480c49b2440cc8a04c33d to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'shellwords'
FIELDS = %w(artist album name duration spotify\ url).map { |x| "#{x} of current track" }.join(' & "\\n" & ')
SCRIPT =<<EOF
tell application "Spotify"
#{FIELDS}
end tell
EOF
COMMAND = Shellwords.shelljoin(["osascript", "-e", SCRIPT])
artist, album, track, duration, uri = `#{COMMAND}`.strip.split("\n")
duration = (duration.to_i / 1000).divmod(60)
formatted = format(
"%s - %s (%d:%02d) %s",
artist,
track,
duration[0],
duration[1],
uri.sub(/^spotify:track:/, "https://open.spotify.com/track/")
)
puts formatted
IO.popen('pbcopy', 'w') do |io|
io.write formatted
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment