2

Prerequisites. Visual Studio for Mac, Mono, C#

I'm porting existing C# application from Windows to Mac. And what I currently need is to get information about currently active window/process. Just user-friendly application name and window title.

Windows code uses Win API for this like GetForegroundWindow. Does something similar exist on Mac? Is there some way to get required information?

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
skv
  • 85
  • 9

1 Answers1

1

You are looking for the FrontmostApplication of the shared NSWorkspace, it returns a NSRunningApplication instance.

var foreground_app = NSWorkspace.SharedWorkspace.FrontmostApplication;
Console.WriteLine($"Name: {foreground_app.LocalizedName}");
Console.WriteLine($"Pid: {foreground_app.ProcessIdentifier}");

re: https://developer.apple.com/documentation/appkit/nsrunningapplication?language=objc

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • Great! Thanks! I would also add that I had to add MonoMac.NetStandard to get required classes – skv Aug 02 '18 at 08:29
  • The second part of my question including answer (how to get window title) can be found here: https://stackoverflow.com/questions/52441936/macos-active-window-title-using-c-sharp/52443413#52443413 – skv Sep 21 '18 at 12:13