Created
December 20, 2009 20:14
-
-
Save codiez/260617 to your computer and use it in GitHub Desktop.
Listen for distributed notifications from iTunes of the current song
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
''' | |
This python script listens for distributed notifications from iTunes of new songs playing, | |
works alot better then constantly polling. | |
''' | |
import Foundation | |
from AppKit import * | |
from PyObjCTools import AppHelper | |
class GetSongs(NSObject): | |
def getMySongs_(self, song): | |
song_details = {} | |
ui = song.userInfo() | |
song_details = dict(zip(ui.keys(), ui.values())) | |
print song_details | |
nc = Foundation.NSDistributedNotificationCenter.defaultCenter() | |
GetSongs = GetSongs.new() | |
nc.addObserver_selector_name_object_(GetSongs, 'getMySongs:', 'com.apple.iTunes.playerInfo',None) | |
NSLog("Listening for new tunes....") | |
AppHelper.runConsoleEventLoop() |
This script works only with system python2. Also, the f*ggot company has renamed iTunes. So here's a couple of needed changes:
#!/usr/bin/python
'''
This python script listens for distributed notifications from iTunes of new songs playing,
works alot better then constantly polling.
'''
import Foundation
from AppKit import *
from PyObjCTools import AppHelper
class GetSongs(NSObject):
def getMySongs_(self, song):
song_details = {}
ui = song.userInfo()
song_details = dict(zip(ui.keys(), ui.values()))
print song_details
nc = Foundation.NSDistributedNotificationCenter.defaultCenter()
GetSongs = GetSongs.new()
nc.addObserver_selector_name_object_(GetSongs, 'getMySongs:', 'com.apple.Music.playerInfo',None)
NSLog("Listening for new tunes....")
AppHelper.runConsoleEventLoop()
Runs with Python3, just be sure to install the right PyObjC wrapper, and modify line 1 to point to your python3 installation if its not in the default location.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there a way to return the variable song_details to a python script to be queried later? similar to a basic class in python?