Skip to content

Instantly share code, notes, and snippets.

@brumm
Created June 4, 2019 11:18
Show Gist options
  • Save brumm/958fbc4f5418ce60f963c25e9c96aa55 to your computer and use it in GitHub Desktop.
Save brumm/958fbc4f5418ce60f963c25e9c96aa55 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#include <node.h>
#include <string>
using namespace v8;
void getWindows(const FunctionCallbackInfo<Value>& args) {
Isolate *isolate = args.GetIsolate();
Local<Array> winArr = Array::New(isolate);
CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
CFArrayRef windowList = CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);
NSUInteger i = 0;
for (NSDictionary *info in (NSArray *)windowList) {
Local<Object> result = Object::New(isolate);
NSString *applicationName = info[(id)kCGWindowOwnerName];
NSNumber *ownerPid = info[(id)kCGWindowOwnerPID];
NSString *windowName = info[(id)kCGWindowName];
NSNumber *windowNumber = info[(id)kCGWindowNumber];
int alpha = [info[(id)kCGWindowAlpha] intValue];
int layer = [info[(id)kCGWindowLayer] intValue];
if (layer > 0 || alpha == 0) continue;
CGRect bounds;
CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)info[(id)kCGWindowBounds], &bounds);
result->Set(
String::NewFromUtf8(isolate, "id"),
Number::New(isolate, [windowNumber intValue])
);
result->Set(
String::NewFromUtf8(isolate, "alpha"),
Number::New(isolate, alpha)
);
result->Set(
String::NewFromUtf8(isolate, "layer"),
Number::New(isolate, layer)
);
result->Set(
String::NewFromUtf8(isolate, "owner"),
String::NewFromUtf8(isolate, [applicationName UTF8String])
);
result->Set(
String::NewFromUtf8(isolate, "ownerPid"),
Number::New(isolate, [ownerPid intValue])
);
result->Set(
String::NewFromUtf8(isolate, "title"),
String::NewFromUtf8(isolate, [windowName UTF8String])
);
result->Set(String::NewFromUtf8(isolate, "x"), Number::New(isolate, bounds.origin.x));
result->Set(String::NewFromUtf8(isolate, "y"), Number::New(isolate, bounds.origin.y));
result->Set(String::NewFromUtf8(isolate, "width"), Number::New(isolate, bounds.size.width));
result->Set(String::NewFromUtf8(isolate, "height"), Number::New(isolate, bounds.size.height));
winArr->Set(i, result);
i++;
}
args.GetReturnValue().Set(winArr);
}
void Init(Local<Object> exports, Local<Object> module) {
NODE_SET_METHOD(exports, "getWindows", getWindows);
}
NODE_MODULE(addon, Init)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment