Created
October 13, 2008 22:40
-
-
Save kjell/16618 to your computer and use it in GitHub Desktop.
A Ruby script to drive iTunes in an alarm–clock–inspired fashion.
This file contains 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 | |
%w(rubygems chronic rbosa).each {|lib| require lib} | |
itunes = OSA.app('iTunes') | |
playlist = itunes.sources[0].playlists.select{|p| p.name == 'radio'}.pop | |
($sleep = true and ARGV.pop if ARGV.last.match(/!/)) | |
directives = eval("{#{ARGV.last.gsub('-', '=>')}}") rescue [] | |
chronic = lambda{|time| time.respond_to?(:integer?) ? Time.now + time*60 : (t = Chronic.parse(time, :context => :future); t < Time.now ? t += 24*60*60 : t )} | |
display_time = lambda {|t| t ||= Time.now; t.strftime('%H:%M %p')} | |
presets = {'morning' => {:_1 => ['current', '5am']}, 'bluegrass' => {:_1 => ['jazz', '7am']}, 'routes' => {:_1 => ['current', '5am']}} | |
directives.merge!(presets[ARGV.first]) if ARGV.size > 1 || directives.nil? # this needs work, I can't just do itunes.rb bluegrass | |
jobs = directives.map do |(cmd, args)| # [":on - ['current', '8:18 am'], :off - 23"] | |
case cmd.to_s | |
when *%w(_1 on + start play go) | |
start = chronic[args.last] | |
puts "gonna start @ #{display_time[start]}" | |
[start, lambda{station = playlist.search(args.first).pop; itunes.play(station); station.reveal; puts "playing — #{display_time[nil]}"}] | |
when *%w(_0 _ | off stop) | |
stop = chronic[args] | |
puts "gonna stop @ #{display_time[stop]}" | |
[stop, lambda{itunes.pause; puts "paused — #{display_time[nil]}"}] | |
end | |
end.sort_by {|(time, lambda)| time} | |
sleep_then_wake_at = lambda do |time| | |
OSA.app('System Events').sleep if system("sudo pmset schedule wake '#{time.strftime("%D %H:%M:%S")}'") | |
end | |
_do = lambda do |job| | |
time, action = job; sleep(time - Time.now) rescue nil; action[] | |
(sleep_then_wake_at[jobs.first.first]; sleep 21) if $sleep && jobs.first | |
_do[jobs.shift] unless jobs.empty? | |
end | |
_do[jobs.shift] | |
=begin | |
Here's how to use this shit: | |
itunes.rb ":on - ['kbem', '8:18 am'], :off - 23" will | |
— turn itunes off in 23 minutes (you can use a more specific time here if you want) | |
— play the radio station with 'kbem' in it's name in the iTunes playlist radio at 8:18am | |
— exit successfully? | |
Other bits? | |
— You need to make a playlist in iTunes (mine's called radio) with the tracks you want to play. | |
I use internet radio stations. (see line 5). | |
— Calling the script with an exclamation mark will schedule your system to wake from sleep | |
before it starts playing and subsequently put it to sleep. | |
— Aliases for :on are :+, :start, :play, :go | |
— Likewise for :off, :_, :|, :stop | |
— Line 11 allows you to give it preset directives: if you listen to a particular radio show, set | |
it up here and call it as follows: | |
itunes.rb bluegrass ":| - 27" ! | |
— Just as a recap, what will that last command do? | |
Match the preset bluegrass, which translates into ['jazz', '7am'] (line 11); then turn off iTunes | |
(only if it's playing) 27 minutes hence. The ! means to sleep your computer. It will then wake up | |
at about 7 and turn on the given station. | |
http://gist.github.com/16618 | |
–Kjell Olsen | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment