Class: ItunesCommand
- Inherits:
-
Object
- Object
- ItunesCommand
- Defined in:
- lib/itunes_command.rb
Constant Summary collapse
- VERSION =
'1.3'
- HELP =
<<END itunes-command Commands: q quit h show commands s <string> searches for tracks matching <string> <track number> plays a track (assumes you've done a search and got results) a lists all artists in the library v show the current volume level v <level> sets the volume level (1-100) + <increment> increases the volume by <increment>; default is 10 steps - <increment> decreases the volume by <increment>; default is 10 steps x stop p shows all playlists <playlist number> shows all the tracks in a playlist l list all tracks in the queue (which will play tracks in succession) n <track number> put a track in the queue; can be a range, e.g. 3-5 c clear the queue g start playing tracks in the queue k skip to next track in queue END
- COMMANDS =
{ 's' => :search, 'x' => :stop , 'play' => :play, 'v' => :volume, 'a' => :artists, '+' => :+, '-' => '-', 'p' => :playlists, 'select_playlist' => :select_playlist, 'l' => :show_queue, 'n' => :queue, 'c' => :clear_queue, 'g' => 'start_queue', 'k' => 'skip'}
Instance Attribute Summary collapse
-
#playlist_mode ⇒ Object
Returns the value of attribute playlist_mode.
Class Method Summary collapse
Instance Method Summary collapse
- #+(steps = 10) ⇒ Object
- #-(steps = 10) ⇒ Object
- #artists ⇒ Object
- #clear_queue ⇒ Object
-
#initialize ⇒ ItunesCommand
constructor
A new instance of ItunesCommand.
- #pager(rows) ⇒ Object
- #parse(method, *args) ⇒ Object
- #play(index) ⇒ Object
- #playlist(index) ⇒ Object
- #playlists ⇒ Object
- #print_names(items) ⇒ Object
- #queue(index) ⇒ Object
- #search(string = nil) ⇒ Object
- #search_artist(string = nil) ⇒ Object
- #select_playlist(index) ⇒ Object
- #show_queue ⇒ Object
- #skip ⇒ Object
- #start_queue ⇒ Object
- #stop ⇒ Object
- #volume(level = nil) ⇒ Object
Constructor Details
#initialize ⇒ ItunesCommand
Returns a new instance of ItunesCommand.
193 194 195 196 197 198 |
# File 'lib/itunes_command.rb', line 193 def initialize @i = ITunes.new @playlists = [] @tracks = [] @playlist_mode = false end |
Instance Attribute Details
#playlist_mode ⇒ Object
Returns the value of attribute playlist_mode.
192 193 194 |
# File 'lib/itunes_command.rb', line 192 def playlist_mode @playlist_mode end |
Class Method Details
.run(argv = ARGV) ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/itunes_command.rb', line 386 def self.run(argv=ARGV) i = ItunesCommand.new puts HELP loop do command = Readline.readline(">> ").chomp if command =~ /^q/ exit elsif command =~ /^h/ puts HELP next end args = command.split(' ') if args.first =~ /\d+/ if i.playlist_mode args.unshift 'select_playlist' else args.unshift 'play' end end method = COMMANDS[args.shift] if method.nil? puts "Sorry, I don't recognize that command." next end begin args.empty? ? i.send(method) : i.send(method, args.join(' ')) rescue ArgumentError puts "Invalid command." end end end |
Instance Method Details
#+(steps = 10) ⇒ Object
257 258 259 260 |
# File 'lib/itunes_command.rb', line 257 def +(steps=10) @i.soundVolume = @i.soundVolume + steps.to_i volume end |
#-(steps = 10) ⇒ Object
262 263 264 265 |
# File 'lib/itunes_command.rb', line 262 def -(steps=10) @i.soundVolume = @i.soundVolume - steps.to_i volume end |
#artists ⇒ Object
341 342 343 344 345 346 347 |
# File 'lib/itunes_command.rb', line 341 def artists rows = [] (@artists=@i.artists).keys.sort_by {|x| x.downcase}.each_with_index do |k, i| rows << ("%s (%s tracks)" % [k, @artists[k]]) end pager(rows) end |
#clear_queue ⇒ Object
323 324 325 326 |
# File 'lib/itunes_command.rb', line 323 def clear_queue @i.clear_queue puts "Cleared queue" end |
#pager(rows) ⇒ Object
349 350 351 352 353 354 355 |
# File 'lib/itunes_command.rb', line 349 def pager(rows) out = rows.join("\n") IO.popen("less", "w") do |less| less.puts out less.close_write end end |
#parse(method, *args) ⇒ Object
200 201 202 |
# File 'lib/itunes_command.rb', line 200 def parse(method, *args) self.send method, args end |
#play(index) ⇒ Object
234 235 236 237 238 239 240 241 242 |
# File 'lib/itunes_command.rb', line 234 def play(index) if @tracks.empty? puts "No tracks in buffer yet. Try searching." return end track = @tracks[index.to_i] puts "Playing '#{track.name}' by #{track.artist} from #{track.album}" track.playOnce(1) end |
#playlist(index) ⇒ Object
284 285 286 287 |
# File 'lib/itunes_command.rb', line 284 def playlist(index) @playlists ||= playlists puts @playlists[index].name end |
#playlists ⇒ Object
267 268 269 270 271 272 273 274 |
# File 'lib/itunes_command.rb', line 267 def playlists @playlist_mode = true puts "Showing playlists" playlists = @i.playlists playlists.each_with_index do |p,i| puts("%2d %s" % [i, p.name]) end end |
#print_names(items) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/itunes_command.rb', line 222 def print_names(items) items = Array(items) rows = [] items.each_with_index do |t, i| begin puts("%2d %s : %s" % [i, t.artist, t.name]) rescue end end items end |
#queue(index) ⇒ Object
289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/itunes_command.rb', line 289 def queue(index) if index =~ /\d+-\d+/ start = index.split('-').first.to_i last = index.split('-').last.to_i Array(start..last).each do |i| @i.queue_track(track=@tracks[i]) puts "Added #{track.name} to the queue" end else @i.queue_track(track=@tracks[index.to_i]) puts "Added #{track.name} to the queue" end end |
#search(string = nil) ⇒ Object
204 205 206 207 208 209 210 211 |
# File 'lib/itunes_command.rb', line 204 def search(string=nil) unless string puts "Please enter a search string" return end @tracks = @i.find_track(string) print_names(@tracks) end |
#search_artist(string = nil) ⇒ Object
213 214 215 216 217 218 219 220 |
# File 'lib/itunes_command.rb', line 213 def search_artist(string=nil) unless string puts "Please enter a search string" return end @tracks = @i.find_track(string) print_names(@tracks) end |
#select_playlist(index) ⇒ Object
276 277 278 279 280 281 282 |
# File 'lib/itunes_command.rb', line 276 def select_playlist(index) @current_playlist = @i.playlists[index.to_i] # show tracks @tracks = @current_playlist.tracks print_names(@tracks) @playlist_mode = false end |
#show_queue ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/itunes_command.rb', line 303 def show_queue if @i.queue.tracks.empty? puts "The queue is empty" return end state = `osascript -e 'tell application "iTunes" to player state as string'`.strip if state != 'stopped' current_track_index = `osascript -e 'tell application "iTunes" to index of current track as string'`.to_i else current_track_index = nil end @i.queue.tracks.each_with_index do |t, i| if current_track_index && current_track_index - 1 == i puts "%s : %s <--- currently playing" % [t.artist, t.name] else puts "%s : %s" % [t.artist, t.name] end end end |
#skip ⇒ Object
335 336 337 338 339 |
# File 'lib/itunes_command.rb', line 335 def skip @i.nextTrack track = @i.currentTrack puts "Playing '#{track.name}' by #{track.artist} from #{track.album}" end |
#start_queue ⇒ Object
328 329 330 331 332 333 |
# File 'lib/itunes_command.rb', line 328 def start_queue @i.stop @i.queue.playOnce(1) track = @i.currentTrack puts "Playing '#{track.name}' by #{track.artist} from #{track.album}" end |
#stop ⇒ Object
244 245 246 |
# File 'lib/itunes_command.rb', line 244 def stop @i.stop end |
#volume(level = nil) ⇒ Object
248 249 250 251 252 253 254 255 |
# File 'lib/itunes_command.rb', line 248 def volume(level=nil) unless level puts "The volume is #{@i.soundVolume} out of 100" return end @i.soundVolume = level puts "The volume set to #{@i.soundVolume} out of 100" end |