質問

Mac OS X向けのシンプルなメノバーアプリを書きたいと思っています。ユーザーは、Safariが開かれたときにそのアプリを使用することを望みます。メノバーを不必要に乱雑にしないようにするには、Safariが開いているかどうかに応じて、メノバーのアイコンを隠して表示したいと思います。

私のアプリが登録できるという通知は多分ありますか?私が想像できる唯一の回避策は、実行中のプロセスを投票し、Safariが起動されるかどうかを確認することですが、それは私の問題を解決するためのエレガントな方法ではないようです...

役に立ちましたか?

解決

NSWorkspaceDidLaunchApplicationNotificationNSWorkspaceDidTerminateApplicationNotification. 。 (同等の炭素イベントがあります。)

他のヒント

使用する カーボンイベントマネージャーでkeventappfrontswitched 別のアプリケーションがアクティブになったときに通知を取得します。

このコードを使用してください: http://cl.ly/2lbb

// usleep(40500);

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

これにより、セレクターが実行されます -doStuff Safariが実行されるとき。エラーが発生した場合は、 usleep() ライン。

同じ問題が発生しましたが、Jwwalkerのおかげで、ドキュメントとGoogleはこのコードを書きました。

// 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
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top