Bring any window to the front

A question was asked on the apple dev mailing list on how to bring a window with a certain name to the front.

Since I was already working on how to get the list of windows on the screen, I just added some AppleScript to bring the window to the front.

This snippet doesnt use any undocumented techniques, it uses the CGWindow API introduced in Leopard.

I dont consider this code to be production ready, but it works.

Of course, TIMTOWTDI 😉

See the code after the jump:


- (BOOL) bringWindowWithNameToFront:(NSString *) windowName
{
// Try to find a window named "windowName", and if found,
// bring it to the front
// Stephan Burlot 4/3/08

  CFArrayRef            windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
  ProcessSerialNumber   myPSN = {kNoProcess, kNoProcess};
  BOOL                  returnValue = FALSE;

  for (NSMutableDictionary *entry in (NSArray *)windowList) {
    NSString *currentWindow = [entry objectForKey:(id)kCGWindowName];
    if ((currentWindow != NULL) && ([currentWindow isEqualToString:windowName]))
    {
      NSString *applicationName = [entry objectForKey:(id)kCGWindowOwnerName];
      int pid = [[entry objectForKey:(id)kCGWindowOwnerPID] intValue];
      GetProcessForPID(pid, &myPSN);

      NSString *script= [NSString stringWithFormat:@"tell application \"System Events\" to tell process \"%@\" to perform action \"AXRaise\" of window \"%@\"\ntell application \"%@\" to activate\n", applicationName, windowName, applicationName];
      NSAppleScript *as = [[NSAppleScript alloc] initWithSource:script];
      [as compileAndReturnError:NULL];
      [as executeAndReturnError:NULL];  // Bring it on!
      [as release];
      returnValue = TRUE;
    }
  }

  CFRelease(windowList);
  return returnValue;
}

Inspiration camed from the following codes: