I think you can do something like this:
let center = NSNotificationCenter.defaultCenter()
center.addObserverForName_object_queue_usingBlock(NSWindowDidBecomeKeyNotification, null, null, __mocha__.createBlock_function('v16@?0@"NSNotification"8', function (notification) {
console.log(notification.object().title())
}))
setTimeout(function(){
center.removeObserver_name_object(null, NSWindowDidBecomeKeyNotification, null)
}, 2000)
If you flip between the windows in the 2000 millisecond duration then you will see the titles of the windows logged. A word of caution though - it seems that this is a little buggy. Iโm sure Iโm doing something wrong memory wise because when you run the script again it seems to crash Sketch. Might be a bug in cocoascript. I would be more comfortable with this if I was using a framework to observe notifications. In fact, Iโve used a similar technique in one of my plugins before (using a framework) and it works fine there.
Some error related to this:
-[__NSFrozenDictionaryM callJSFunction:withArgumentsInArray:]: unrecognized selector sent to instance 0x600001fb9740