Created
December 18, 2010 17:34
-
-
Save siyo/746685 to your computer and use it in GitHub Desktop.
my itunes cli
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 'kconv' | |
require 'rubygems' | |
require 'rbosa' | |
class ItunesCLI | |
COMMANDS = { | |
'next' => :next_track, | |
'n' => :next_track, | |
'prev' => :back_track, | |
'back' => :back_track, | |
'b' => :back_track, | |
'pause' => :pause, | |
'play' => :play, | |
'playpause' => :playpause, | |
'p' => :playpause, | |
'stop' => :stop, | |
'track' => :current_track, | |
't' => :current_track, | |
} | |
def initialize | |
@app = OSA.app('iTunes') | |
end | |
def run(argv) | |
key = argv.empty? ? nil : argv.shift | |
unless COMMANDS.keys.include? key | |
puts usage | |
exit | |
end | |
cmd = COMMANDS[key] | |
@app.send(cmd) if cmd != :current_track | |
puts track_info | |
end | |
def track_info | |
track = @app.current_track | |
str = "%s - %s / %s" % [track.name, track.album, track.artist] | |
str.toutf8 | |
end | |
def usage | |
keys = COMMANDS.keys.sort_by{|e| e.to_s} | |
str = "Usage: itunes [command]\n" | |
str << "commands:\n" | |
keys.each{|k| | |
str << " %-10s ... %s\n" % [ k, COMMANDS[k] ] | |
} | |
str | |
end | |
end | |
ItunesCLI.new.run(ARGV) if __FILE__ == $0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment