diff --git a/lib/macos.mm b/lib/macos.mm index 038a7b9..bc96115 100644 --- a/lib/macos.mm +++ b/lib/macos.mm @@ -115,7 +115,7 @@ AXUIElementRef getAXWindowById(int handle) { NSNumber *windowNumber = info[(id)kCGWindowNumber]; auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]]; - auto path = app ? [app.bundleURL.path UTF8String] : ""; + auto path = (app && app.bundleURL && app.bundleURL.path) ? [app.bundleURL.path UTF8String] : ""; if (app && path != "") { vec.push_back(Napi::Number::New(env, [windowNumber intValue])); @@ -147,7 +147,7 @@ AXUIElementRef getAXWindowById(int handle) { auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]]; - if (![app isActive]) { + if (app && ![app isActive]) { continue; } @@ -171,6 +171,9 @@ AXUIElementRef getAXWindowById(int handle) { if (wInfo) { NSNumber *ownerPid = wInfo[(id)kCGWindowOwnerPID]; NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]]; + if (!app || !app.bundleURL || !app.bundleURL.path) { + return Napi::Object::New(env); + } auto obj = Napi::Object::New(env); obj.Set("processId", [ownerPid intValue]); @@ -193,6 +196,9 @@ AXUIElementRef getAXWindowById(int handle) { if (wInfo) { NSString *windowName = wInfo[(id)kCGWindowOwnerName]; + if (!windowName) { + return Napi::String::New(env, ""); + } return Napi::String::New(env, [windowName UTF8String]); }