How to get WindowID?

Hello,
How to get the ID of the active window (WindowID)?
I tested this but always returns 0 !

Soft Declare Function NSClassFromString Lib "Cocoa" (className As CFStringRef) As Ptr
Soft Declare Function sharedApplication Lib "Cocoa" Selector "sharedApplication" (NSObjectClass As Ptr) As Ptr
Soft Declare Function mainWindow Lib "Cocoa" Selector "mainWindow" (NSApp As Ptr) As Ptr
Soft Declare Function windowNumber Lib "Cocoa" Selector "windowNumber" (NSWindow As Ptr) As Integer

Function GetActiveWindowID() As Integer

// Obtenez la classe NSApplication
Dim NSApplicationClass As Ptr = NSClassFromString("NSApplication")

// Get the shared instance of NSApplication
Dim NSApp As Ptr = sharedApplication(NSApplicationClass)

// Get the main application window
Dim NSMainWindow As Ptr = mainWindow(NSApp)

//Get the window number of the main window
Dim windowID As Integer = windowNumber(NSMainWindow)

// Return window ID
Return windowID

End Function

Thanks

I execute the command from an s/menu in the menubar (that may be important)

I don’t know if it helps, but with the Window function Window(0) is always the front most window.

1 Like

thank you, but it is the ID of the active window of the active application, and not of my application, that I am looking for.
With MBS maybe…

[NSApplication sharedApplication] is your app instance, so if this declare were working it would operate as Window(0) does.

To track window titles the way those automatic time tracking apps do you need to use the accessibility API. Here’s a python script I found by searching for info if that helps you figure out the declares: Find the frontmost/active window in OS X · GitHub

1 Like

A little too complex for me but thank you.