Skip to content

Instantly share code, notes, and snippets.

@siyo
Created December 18, 2010 17:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siyo/746685 to your computer and use it in GitHub Desktop.
Save siyo/746685 to your computer and use it in GitHub Desktop.
my itunes cli
#!/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