MakeKeyAndOrderFront From Menu Bar Applications Not Working?

Discussion in 'Mac Programming' started by Darkroom, Nov 28, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    bit of an issue here... i've been playing around with menu bar applications and i can't seem to make a new window key when opening one from the menulet's pull down menu.

    i can understand (kinda/sorta) why it's not working, because the application is only running in the background, so opening a new window from this background app shouldn't make the window key because the app isn't key, or something like that.

    but other menu bar apps like "Google Notifier" have selectable popup windows (like Preferences, etc.) that are always made key and ordered front when selected...

    is there some secret to doing this?

    i built a small test app to demonstrate this so you can see what's happening. the first time you build the app and select the window to open, it will be the key app, but only because you just built from xcode. But if you launch the build from Finder (included), or while the app is running after being built from xcode if you then select Finder (or any other app) and then open the popup window from the menulet it will not be key, eventhough my code tells the window to become key and ordered front...

    help here would be great.
     

    Attached Files:

  2. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #2
    so i'm not sure if this is the standard way to solving this issue, but i managed to make this work by using a simple cocoa-embedded applescript at the start of the IBAction that opens a new window.

    Code:
    NSString *bringAppToFrontScript = @"\
                                      tell application \"[I]appName[/I]\"\n\
                                      activate\n\
                                      end tell";
    NSDictionary *errorDict;
    NSAppleEventDescriptor *returnDescriptor = NULL;
    NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:bringAppToFrontScript];
    returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
    [NSAppleScript release];
    
     
  3. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    The preferred method of doing this is via
    Code:
    [NSApp activateIgnoringOtherApps:YES];
     
  4. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    Nice! ha... yeah, i prefer this method... thanks :)
     

Share This Page