Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 613
ismail Avatar asked Dec 19 '09 14:12

ismail


People also ask

How do you view notifications on Mac?

in the upper-right corner of your screen, or swipe left with two fingers from the right edge of your trackpad. To view notifications that you missed, such as calendar alerts or FaceTime calls, click Notifications at the top of Notification Center. To open a notification in the app that sent it, click the notification.

How do I get notifications on my Mac from my iPhone?

To get SMS, MMS, and Message alerts on your Mac, again sign into iCloud with the same Apple ID on both your phone and your Mac. Next, open Settings on your iPhone, choose Messages, and hit Send & Receive. Make sure you're using the right Apple ID, then tick both your phone number and email address.

Why am I not getting notifications on my Mac?

Give Notification Permission Step 1: Click on the little Apple icon in the upper left corner. Step 2: Open System Preferences menu. Step 3: Go to the Notifications & Focus menu. Step 4: Select an app that's giving you trouble with notifications.


2 Answers

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()
like image 105
ismail Avatar answered Oct 26 '22 23:10

ismail


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

like image 45
RyanWilcox Avatar answered Oct 26 '22 22:10

RyanWilcox