Class: ItunesCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/itunes_command.rb

Constant Summary collapse

VERSION =
'1.6.4'
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
  "                   pause/play
  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', '"' => :playpause }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeItunesCommand

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_modeObject

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



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/itunes_command.rb', line 406

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

#artistsObject



360
361
362
363
364
365
366
# File 'lib/itunes_command.rb', line 360

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_queueObject



338
339
340
341
# File 'lib/itunes_command.rb', line 338

def clear_queue
  @i.clear_queue
  puts "Cleared queue"
end

#pager(rows) ⇒ Object



368
369
370
371
372
373
374
# File 'lib/itunes_command.rb', line 368

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

#playlistsObject



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

#playpauseObject



303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/itunes_command.rb', line 303

def playpause
  puts %x{tell application "iTunes"
    playpause
  end tell}
  state = `osascript -e 'tell application "iTunes" to player state as string'`.strip
  puts state
  return
  if state == 'stopped'
    @i.currentTrack.playOnce(1)
  else
    puts @i.currentTrack.name
    @i.pause
  end
end


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_queueObject



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/itunes_command.rb', line 318

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

#skipObject



350
351
352
353
354
# File 'lib/itunes_command.rb', line 350

def skip
  @i.nextTrack
  track = @i.currentTrack
  puts "Playing '#{track.name}' by #{track.artist} from #{track.album}"
end

#start_queueObject



343
344
345
346
347
348
# File 'lib/itunes_command.rb', line 343

def start_queue
  @i.stop
  @i.queue.playOnce(1)
  track = @i.currentTrack
  puts "Playing '#{track.name}' by #{track.artist} from #{track.album}"
end

#stopObject



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