How do you listen to notifications from iTunes on a Mac (Using the NSDistributedNotificationCenter)
Asked Answered
A

2

5

Looking for help/tutorials/sample code of using python to listen to distributed notifications from applications on a mac. I know the py-objc lib is the bridge between python and mac/cocoa classes, and the Foundation library can be used to add observers, but looking for examples or tutorials on how to use this to monitor iTunes.

Abbeyabbi answered 19/12, 2009 at 14:59 Comment(2)
Does iTunes actually post distributed notifications these days?Overarm
Yes it does.... under com.apple.iTunes.player, using notification watch i can see them.Abbeyabbi
A
12

If anyone comes by to this question, i figured out how to listen, the code below works. However accessing attributes do not seem to work like standard python attribute access.

Update: you do not access attributes as you would in python i.e (.x), the code has been updated below, it now generates a dict called song_details.

Update3: Update to the code, now subclassing NSObject, removed adding the addObserver from the class. Will keep the code updated on github, no more updates here.

import Foundation
from AppKit import *
from PyObjCTools import AppHelper

class GetSongs(NSObject):
    def getMySongs_(self, song):
        song_details = {}
        ui = song.userInfo()
        for x in ui:
            song_details[x] = ui.objectForKey_(x)
        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()
Abbeyabbi answered 20/12, 2009 at 19:16 Comment(0)
K
4

The source code for GrowlTunes might give you some clues here. You'd have to translate from Objective-C to PyObjC, but eh, whatever. :)

GrowlTurnesController.m (Or grab the whole growl source tree and navigate to GrowlTunes so you can see it all in action.: here's a link to the directions on how to get the source

Kelda answered 20/12, 2009 at 15:7 Comment(1)
Thanks for the help, managed to figure it out using pyobjc website & apple web site.Abbeyabbi

© 2022 - 2024 — McMap. All rights reserved.