Skip to content

Instantly share code, notes, and snippets.

@dtinth
Created July 10, 2014 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtinth/7eba28a573ffb1554410 to your computer and use it in GitHub Desktop.
Save dtinth/7eba28a573ffb1554410 to your computer and use it in GitHub Desktop.
require 'osaka'
require 'growl'
def get_player_position
Osaka::ScriptRunner.execute(%q(tell application "iTunes" to get the player position)).to_f
end
def get_track_description
Osaka::ScriptRunner.execute(%q(tell application "iTunes" to get the current track's long description))
end
def get_tracklist(description)
description.lines.map(&:strip)
.map { |line| [line, line.match(/\[(\d+):(\d+):(\d+)\]/)] }
.reject { |text, match| match.nil? }
.map { |text, match| { text: text, time: match_to_time(match) } }
end
def match_to_time(m)
m[1].to_f * 3600 + m[2].to_f * 60 + m[3].to_f
end
current_position = -Float::INFINITY
loop do
last_position, current_position = [current_position, get_player_position]
tracklist = get_tracklist(get_track_description)
new_song = tracklist.reverse.find { |track|
last_position < track[:time] && track[:time] <= current_position }
if new_song
Growl.notify new_song[:text], name: 'cuegrowler', title: 'New Track'
puts "\n[#{Time.now}]"
puts new_song[:text]
end
sleep 2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment