Created
March 12, 2017 06:33
-
-
Save abhishekjairath/8bfb259c681ef52545b32c88db6336f5 to your computer and use it in GitHub Desktop.
Using 'nodobjc' a nodeJS->ObjectveC bridge to listen to distributed notification centre of macOS/OSX.
This file contains hidden or 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
var $ = require('nodobjc') | |
$.framework('Foundation') | |
$.framework('AppKit') | |
var GetSongs = $.NSObject.extend('Delegate'); | |
GetSongs.addMethod('getMySongs:', 'v@:@', function(self, _cmd, notif){ | |
var userInfo = notif('userInfo') | |
var keys = userInfo('keyEnumerator'); | |
var key; | |
var song = {}; | |
while(key = keys('nextObject')){ | |
var value = userInfo('objectForKey', key) | |
song[key.toString()] = value.toString(); | |
} | |
console.log(song); | |
}); | |
GetSongs.register(); | |
var gs = GetSongs('alloc')('init'); | |
var nc = $.NSDistributedNotificationCenter('defaultCenter'); | |
var selector = $.NSSelectorFromString($('getMySongs:')); | |
var notification = $('com.spotify.client.PlaybackStateChanged'); | |
nc( | |
'addObserver', gs, | |
'selector', 'getMySongs:', | |
'name', notification, | |
'object', null) | |
var app = $.NSApplication('sharedApplication'); | |
function runLoop() { | |
var pool = $.NSAutoreleasePool('alloc')('init'); | |
try { | |
app('nextEventMatchingMask', $.NSAnyEventMask.toString(), | |
'untilDate', $.NSDate('distantFuture'), | |
'inMode', $.NSDefaultRunLoopMode, | |
'dequeue', 1); | |
} catch(e) { | |
console.error('run loop error', e.message); | |
} | |
pool('drain'); | |
setTimeout(runLoop,1000); | |
//process.nextTick(runLoop); | |
} | |
runLoop(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment