Created
January 18, 2009 10:15
-
-
Save jptix/48590 to your computer and use it in GitHub Desktop.
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 -wKU | |
require "osx/cocoa" | |
include OSX | |
require_framework "ScriptingBridge" | |
class NextPreviewPlayer < NSObject | |
def initialize | |
@itunes = SBApplication.applicationWithBundleIdentifier 'com.apple.iTunes' | |
@default_center = NSDistributedNotificationCenter.defaultCenter | |
@played = 0 | |
add_observer | |
end | |
def player_info(notification) | |
return unless (info = notification.userInfo).has_key?('Store URL') | |
state = info['Player State'] | |
track = info['Track Number'] | |
case state | |
when 'Stopped' | |
play_next | |
when 'Playing' | |
track == @played ? stop : @played = track | |
end | |
end | |
private | |
def play_next | |
dont_observe { @itunes.nextTrack } | |
@itunes.playpause | |
end | |
def stop | |
dont_observe { @itunes.stop } | |
@played = 0 | |
end | |
def add_observer | |
@default_center.objc_send(:addObserver, self, | |
:selector, :player_info, | |
:name, 'com.apple.iTunes.playerInfo', | |
:object, nil) | |
end | |
def remove_observer | |
@default_center.removeObserver(self) | |
end | |
def dont_observe | |
remove_observer | |
yield | |
add_observer | |
end | |
end | |
NextPreviewPlayer.new | |
NSApplication.sharedApplication.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment