Question

Je veux écrire une application simple de la barre de menu pour Mac OS X. L'utilisateur ne voudra utiliser cette application lorsque Safari est ouvert. Pour ne pas encombrer inutilement la barre de menus, je veux cacher et afficher la barre de menus icône selon que Safari est ouvert ou non.

Y at-il une notification peut-être que mon application pourrait inscrire? La seule solution de contournement que je peux imaginer est en cours d'exécution du sondage les processus et voir si Safari est lancé, mais cela ne semble pas être une façon élégante de résoudre mon problème ...

Était-ce utile?

La solution

NSWorkspaceDidLaunchApplicationNotification et NSWorkspaceDidTerminateApplicationNotification. (Il y a équivalents carbone Événements).

Autres conseils

Utilisez kEventAppFrontSwitched en carbone Event Manager pour recevoir des notifications lorsqu'une autre application est activée.

Utilisez ce code: http://cl.ly/2LbB

// usleep(40500);

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

courra le sélecteur -doStuff lors de l'exécution Safari. Si vous obtenez une erreur, supprimez la ligne usleep().

Vous avez même problème, mais grâce à JWWalker, la documentation et Google ont écrit ce code:

// 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
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top