objc icon indicating copy to clipboard operation
objc copied to clipboard

How to get NSView or NSWindow?

Open humbleCoder1990 opened this issue 3 years ago • 3 comments

I want to get current NSView or NSWindow. Is it possible?

humbleCoder1990 avatar Nov 01 '21 15:11 humbleCoder1990

Not sure what you mean, tbh. It seems to me like that's something that would fall into the scope of AppKit, rather than this library.

lukaskollmer avatar Jan 21 '22 10:01 lukaskollmer

I want to get current NSView or NSWindow. Is it possible?

Have you reslove this problem? Do you know the method , thanks

Lijian1122 avatar Jan 27 '22 09:01 Lijian1122

This is essentially how you'd do it:

// Import AppKit to get access to its symbols (like NSApplication)
const objc = require('objc');
objc.import('AppKit');

// Once the application has started, and a window is available, you
// can get a reference to it like this:
const mainWindow = objc.NSApplication.sharedApplication().mainWindow();

For an example of starting up an application from scratch, see https://github.com/lukaskollmer/objc/pull/43 (where I actually initialise the NSWindow myself, so getting a reference to it is easy).

If you want to control an already-running app using this (which it seems many Electron users do), I have no idea whether that's even possible (if nothing else due to macOS sandboxing apps from each other). It would be easiest to start up and manage the entire app using this framework rather than attach to an existing one.

shirakaba avatar Jan 05 '23 12:01 shirakaba