lukaskollmer / objc

🔮 NodeJS ↔ Objective-C bridge (experimental)
MIT License
98 stars 20 forks source link

How to get NSView or NSWindow? #31

Open humbleCoder1990 opened 2 years ago

humbleCoder1990 commented 2 years ago

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

lukaskollmer commented 2 years ago

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.

Lijian1122 commented 2 years ago

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

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

shirakaba commented 1 year ago

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.