Created
September 19, 2016 07:49
-
-
Save bhavyaw/20856e8fb50324174c25f973e5ae4377 to your computer and use it in GitHub Desktop.
This file contains 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
'use strict'; | |
var $ = require('nodobjc'); | |
// Load the AppKit framework. | |
$.framework('AppKit'); | |
// Create delegate that gets notified | |
var Delegate = $.NSObject.extend('Delegate'); | |
function receiveScreenLockNote(self, cmd, notif) { | |
NodeLogger.log('Mac Screen got locked'); | |
} | |
function receiveScreenUnlockNote(self, cmd, notif) { | |
NodeLogger.log('Mac screen got unlocked'); | |
} | |
Delegate.addMethod('receiveScreenLockNote:', 'v@:@', receiveScreenLockNote); | |
Delegate.addMethod('receiveScreenUnlockNote:', 'v@:@', receiveScreenUnlockNote); | |
Delegate.register(); | |
// Instantiate the delegate and set it as observer. | |
var delegate = Delegate('alloc')('init'); | |
var nc = $.NSWorkspace('sharedWorkspace')('notificationCenter'); | |
nc( | |
'addObserver', delegate, | |
'selector' , 'receiveScreenLockNote:', | |
'name' , $.NSWorkspaceScreensDidSleepNotification, | |
'object' , null | |
); | |
nc( | |
'addObserver', delegate, | |
'selector' , 'receiveScreenUnlockNote:', | |
'name' , $.NSWorkspaceScreensDidWakeNotification, | |
'object' , null | |
); | |
// Set up the runloop. | |
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