code: def getForegroundWindow(self):
          
          active_app = NSWorkspace.sharedWorkspace().frontmostApplication().localizedName()
          for w in self._get_window_list():
              if "kCGWindowOwnerName" in w and w["kCGWindowOwnerName"] == active_app:
                  return w["kCGWindowNumber"]
docs: Returns a handle to the window in the foreground