Skip to content

Instantly share code, notes, and snippets.

@codiez
Created December 20, 2009 20:14
Show Gist options
  • Save codiez/260617 to your computer and use it in GitHub Desktop.
Save codiez/260617 to your computer and use it in GitHub Desktop.
Listen for distributed notifications from iTunes of the current song
'''
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()
@y4rr
Copy link

y4rr commented Feb 17, 2021

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()

@jim-cooley
Copy link

jim-cooley commented Oct 19, 2021

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