Pregunta

Quiero escribir una aplicación sencilla barra de menú de Mac OS X. El usuario sólo tendrá que usar esa aplicación cuando se abre Safari. Para no saturar innecesariamente la barra de menús, quiero ocultar y mostrar la barra de menú del icono dependiendo de si Safari está abierto o no.

¿Hay tal vez algo de la notificación de que mi aplicación podría registrarse para? La única solución que puedo imaginar es la encuesta de los procesos en ejecución y ver si se lanza Safari, pero eso no parece ser una manera elegante de resolver mi problema ...

¿Fue útil?

Solución

NSWorkspaceDidLaunchApplicationNotification and NSWorkspaceDidTerminateApplicationNotification. (There are equivalent Carbon Events.)

Otros consejos

Use kEventAppFrontSwitched in Carbon Event Manager to get notifications when another application becomes active.

Use this code: http://cl.ly/2LbB

// usleep(40500);

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

This will run the selector -doStuff when Safari runs. If you get an error, uncomment the usleep() line.

Got same problem, but thanks to JWWalker, documentation and google wrote this 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
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top