todbot / Blink1Control2

Blink1Control GUI to control blink(1) USB RGB LED devices.
http://blink1.thingm.com/
Apache License 2.0
154 stars 24 forks source link

Blink1 respond to Notifications on a Mac? #45

Open timberwolfl opened 7 years ago

timberwolfl commented 7 years ago

Is it possible to create a way for the blink(1) to perform a pattern whenever a notification appears on a Mac? For example the notification that a new update is available? Thanks.

todbot commented 7 years ago

Hi! I would love to add that feature but from what I can tell, there's no way to listen to the notification stream on MacOS. I've discovered how to send notifications but that's it. If you know of another open source Mac application that does listen to notifications, please let me know. There may be some tricks we can pull from it.

atchoo78 commented 7 years ago

Here's some thoughts, in lack of better alternatives at the moment:

[I made this the other day (Gist)] (https://gist.github.com/atchoo78/697747ce21495fa0d67ba15c54635a01)

It's basically a python script that's "observing" certain notification center updates from iTunes, and every time there's a change (IE when a new song starts), it sends the song info via HTTP to my Raspberry PI, running a lightweight web server that I configured to display the HTTP POST results on a dot matrix LED display. Point being: I think scripting bridge is the only way to go without writing a full blown cocoa app, especially when fumbling around in the darkest mudholes of macOS core. It should be fairly easy to configure a similar script, "observing" just about any event thrown at the notification center….

However, I don't have the slightest clue how, right now😜 I just connected some dots to accomplish my goal (disappearing even further down in the rabbit hole 🐇)

atchoo78 commented 6 years ago

Here's a working (well, almost I guess) and a bit simplified example of the Gist I mentioned earlier. It's listening for notifications in the notification center, from specified app identifiers. There might be something in there you can modify to suit the purpose, since the key elements are pretty similar (from what I can see, at least).


#!/usr/bin/env python

import Foundation
from AppKit import *
from PyObjCTools import AppHelper
import requests
import os

BPURL = "http://localhost:8934/blink1/pattern/play?pname=green%20flash"

class GetSongs(NSObject):
    def getMySongs_(self, song):
        song_details = {}
        ui = song.userInfo()
        song_details = dict(zip(ui.keys(), ui.values()))
        playerState = (song_details['Player State'])
        if not('Stopped' in playerState):
            theArtist = song_details['Artist']
            theSong = song_details['Name']
            theAlbum = song_details['Album']
            nowPlaying = song_details['Artist'] + ' : ' + song_details['Name']
        else:
            return
        if("spotify" in song_details):
            activator = "com.spotify.client"
        else:
            activator = "com.apple.iTunes"     
        if('Playing' in playerState):
            blinkSongInfo = requests.post(url = BPURL).encode('utf-8'))

## Notice - check syntax for Terminal Notifier (https://github.com/julienXX/terminal-notifier) or some other app with similar functionality.
## Pseudo code coming up:

# os.system("terminal-notifier -title " + theArtist + " -message " + theSong + " -subtitle " + theAlbum + " -activate " + activator + " -sender com.thingm.blink1control2") # ...or whatever the app Identifier is :-)

nc = Foundation.NSDistributedNotificationCenter.defaultCenter()
GetSongs = GetSongs.new()
nc.addObserver_selector_name_object_(GetSongs, 'getMySongs:', 'com.apple.iTunes.playerInfo',None)
nc.addObserver_selector_name_object_(GetSongs, 'getMySongs:', 'com.spotify.client.PlaybackStateChanged',None)

AppHelper.runConsoleEventLoop()
`