Last active
November 20, 2016 13:28
-
-
Save aalin/a19f54b80cb480c49b2440cc8a04c33d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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