1

So I have a C# application using Mono and Gtk+2 running on Mac Is there some way to get active application window title?

https://stackoverflow.com/a/37368813/3794943 says that I need CGWindowListCopyWindowInfo (I already have the rest of their recipe, like process identifier, front most application, etc.).

Where can I get CGWindowListCopyWindowInfo or something similar from? Or is there some other way to get active window title when using mono on mac os?

skv
  • 85
  • 9

1 Answers1

4

Ok, finally found it here: https://forums.xamarin.com/discussion/comment/95429/#Comment_95429

At first you import that function like this:

[DllImport(@"/System/Library/Frameworks/QuartzCore.framework/QuartzCore")]
static extern IntPtr CGWindowListCopyWindowInfo(CGWindowListOption option, uint relativeToWindow);

Then you call it like this:

    string result = null;
    IntPtr windowInfo = CGWindowListCopyWindowInfo(CGWindowListOption.OnScreenOnly, 0);
    NSArray values = (MonoMac.Foundation.NSArray)Runtime.GetNSObject(windowInfo);

    for (ulong i = 0, len = values.Count; i < len; i++)
    {
        NSObject window = Runtime.GetNSObject(values.ValueAt(i));

        NSString key = new NSString("kCGWindowOwnerPID");
        NSNumber value = (MonoMac.Foundation.NSNumber)window.ValueForKey(key);
        // and so on
    }

P.S. MonoMac package must be added using NuGet

skv
  • 85
  • 9