【问题标题】:Mac OS X - monitor app launch?Mac OS X - 监控应用程序启动?
【发布时间】:2011-04-11 08:19:54
【问题描述】:

我想为 Mac OS X 编写一个简单的菜单栏应用程序。用户只想在 Safari 打开时使用该应用程序。为了避免不必要地弄乱菜单栏,我想根据 Safari 是否打开来隐藏和显示菜单栏图标。

是否有一些通知可以让我的应用注册?我能想象的唯一解决方法是轮询正在运行的进程并查看是否启动了 Safari,但这似乎不是解决我的问题的一种优雅方式......

【问题讨论】:

    标签: macos cocoa nsnotificationcenter observers nsworkspace


    【解决方案1】:

    NSWorkspaceDidLaunchApplicationNotificationNSWorkspaceDidTerminateApplicationNotification。 (有等效的碳事件。)

    【讨论】:

      【解决方案2】:

      使用kEventAppFrontSwitched in Carbon Event Manager 在另一个应用程序激活时获取通知。

      【讨论】:

        【解决方案3】:

        使用此代码:http://cl.ly/2LbB

        // usleep(40500);
        
        ProcessNotif * x = [[ProcessNotif new] autorelease];
        [x setProcessName: @"Safari"];
        [x setTarget: self];
        [x setAction: @selector(doStuff)];
        [x start];
        

        这将在 Safari 运行时运行选择器 -doStuff。如果出现错误,请取消注释 usleep() 行。

        【讨论】:

        • usleep 呼叫如何帮助“错误”?您指的是哪种“错误”?
        • 我一直在使用一些开源代码,但我遇到了某种错误,取消注释该行解决了这个问题。
        【解决方案4】:

        遇到了同样的问题,但感谢 JWWalker,文档和谷歌编写了以下代码:

        // i need to register on button event, you can do it even in applicationDidFinishLaunching
        - (IBAction)Btn_LoginAction:(id)sender {
            ...
            NSNotificationCenter *center = [[NSWorkspace sharedWorkspace] notificationCenter];
            [center addObserver:self selector:@selector(appLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
            [center addObserver:self selector:@selector(appTerminated:) name:NSWorkspaceDidTerminateApplicationNotification object:nil];
        }
        
        // remember to unregister
        - (void)ManageLogout:(NSInteger)aResult {
            ...
            NSNotificationCenter *center = [[NSWorkspace sharedWorkspace] notificationCenter];
            [center removeObserver:self name:NSWorkspaceDidLaunchApplicationNotification object:nil];
            [center removeObserver:self name:NSWorkspaceDidTerminateApplicationNotification object:nil];
        }
        
        - (void)appLaunched:(NSNotification *)note {
            [GTMLogger myLog:kGTMLoggerLevelDebug fmt:@"MainWinDelegate::appLaunched: %@ (%@)\n", [[note userInfo] objectForKey:@"NSApplicationBundleIdentifier"], [[note userInfo] objectForKey:@"NSApplicationProcessIdentifier"]];
        
            if ( [[[note userInfo] objectForKey:@"NSApplicationBundleIdentifier"] isEqualToString:@"app.you.monitor.bundle.identifier"] ) {
                // do stuff
            }
        }
        
        - (void)appTerminated:(NSNotification *)note {
            [GTMLogger myLog:kGTMLoggerLevelDebug fmt:@"MainWinDelegate::appTerminated: %@ (%@)\n", [[note userInfo] objectForKey:@"NSApplicationBundleIdentifier"], [[note userInfo] objectForKey:@"NSApplicationProcessIdentifier"]];
        
            if ( [[[note userInfo] objectForKey:@"NSApplicationBundleIdentifier"] isEqualToString:@"app.you.monitor.bundle.identifier"] ) {
                // do stuff
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2012-01-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多