隐藏停靠栏图标而不隐藏菜单栏

时间:2014-04-26 16:22:24

标签: cocoa osx-mavericks nsmenu

我使用this thread中的提示隐藏我的应用的停靠图标。如果最后显示停靠栏图标,则也应显示菜单栏。只有Jiulong's answer我才能做到这一点。菜单栏仍然隐藏。

所以基本上'应用程序是代理商'被设置为' 1'在InfoPList中,使用此代码:

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"LaunchAsAgentApp"]) {   
    ProcessSerialNumber psn = { 0, kCurrentProcess };
    TransformProcessType(&psn, kProcessTransformToForegroundApplication);
    SetSystemUIMode(kUIModeNormal, 0);
    [[NSWorkspace sharedWorkspace] launchAppWithBundleIdentifier:@"com.apple.dock"    options:NSWorkspaceLaunchDefault additionalEventParamDescriptor:nil launchIdentifier:nil];
    [[NSApplication sharedApplication] activateIgnoringOtherApps:TRUE];
}

那么为什么菜单栏不显示,直到我隐藏并重新聚焦应用程序?有没有解决这个问题?我看到'Quick Search Box' for mac app在启动时没有显示菜单栏...


编辑:我联系了Apple,他们给了我碳和非碳解决方案。鉴于一个新项目,“应用程序是代理商”。设为'是'是在Plist文件中,此代码可以在AppDelegate类中使用:

#define USE_CARBON  0

//
//  Note: NSLogDebug is defined in the projects pre-compiled (.pch) file
//

@implementation AppDelegate
{
    BOOL show_icon;
}

// Application will finish launching
- (void)applicationWillFinishLaunching:(NSNotification *)notification {
    NSLogDebug();
    NSURL *receiptUrl = [[NSBundle mainBundle] appStoreReceiptURL];
    if (![[NSFileManager defaultManager] fileExistsAtPath:[receiptUrl path]]) {
        // exit(173);
    }

#if 1
    show_icon = YES;
#else
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *hasDockIconDefaultsKey = @"Has Dock Icon?";
    // note: toggles value on each run (normally set from user pref pannel)
    show_icon = [userDefaults boolForKey:hasDockIconDefaultsKey];
    [userDefaults setBool:!show_icon forKey:hasDockIconDefaultsKey];
#endif // if 1
    if (show_icon) {
        [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
        [NSApp setPresentationOptions:NSApplicationPresentationDefault];
        [NSMenu setMenuBarVisible:NO];
        [NSMenu setMenuBarVisible:YES];
    }

    [NSApp activateIgnoringOtherApps:YES];
}   // applicationWillFinishLaunching

// Application did finish launching
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSLogDebug();
    // Insert code here to initialize your application
    if (show_icon) {
#if USE_CARBON
        ProcessSerialNumber psn = {0, kCurrentProcess};
        OSStatus returnCode = TransformProcessType(&psn, kProcessTransformToForegroundApplication);
        if (noErr != returnCode) {
            NSLog(@"TransformProcessType error: %d (0x%0X)", returnCode, returnCode);
        }

        ProcessSerialNumber psnx = {0, kNoProcess};
        GetNextProcess(&psnx);
        SetFrontProcess(&psnx);
#else   // if 0
        NSWorkspace *sharedWorkspace = [NSWorkspace sharedWorkspace];
        NSRunningApplication * menuBarOwningApplication = [sharedWorkspace menuBarOwningApplication];
        (void) [menuBarOwningApplication activateWithOptions:NSApplicationActivateIgnoringOtherApps];
#endif
        [self performSelector:@selector(setFront) withObject:nil afterDelay:0.];
    }
}   // applicationDidFinishLaunching

// Close app when main window is closed
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
    return (YES);
}

- (void)setFront;
{
#if USE_CARBON
    ProcessSerialNumber psn = {0, kCurrentProcess};
    SetFrontProcess(&psn);
#else // if USE_CARBON
    [[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps];
#endif  // USE_CARBON
}

@end

请注意,我也提交了错误报告。

1 个答案:

答案 0 :(得分:1)

首先,您应该在现代代码中使用-[NSApplication setActivationPolicy:]而不是TransformProcessType()-[NSApplication setPresentationOptions:]而不是SetSystemUIMode()。如果切换到那些不足以解决菜单栏未更新的问题,我建议您尝试使用-setPresentationOptions:+[NSMenu setMenuBarVisible:]隐藏菜单栏,然后​​立即撤消该操作。

此外,尝试激活Dock时放弃该业务。

所以,比如:

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"LaunchAsAgentApp"]) {
    [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
    [NSApp setPresentationOptions:NSApplicationPresentationDefault]; // probably not necessary since it's the default
    [NSMenu setMenuBarVisible:NO]; // these two lines may not be necessary, either; using -setActivationPolicy: instead of TransformProcessType() may be enough
    [NSMenu setMenuBarVisible:YES];
}
相关问题