tags:

views:

51

answers:

3

I want to write a simple menubar app for Mac OS X. The user will only want to use that app when Safari is opened. To not clutter the menubar unnecessarily, I want to hide and show the menubar icon depending on whether Safari is open or not.

Is there maybe some notification that my app could register for? The only workaround I can imagine is poll the running processes and see if Safari is launched, but that doesn't seem to be an elegant way to solve my problem...

+1  A: 

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

Jim Huang
A: 

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.

Alexsander Akers
How does the `usleep` call help with “an error”? What sort of “error” are you referring to?
Peter Hosey
I had been using some open source code and I got an error of some sort and uncommenting that line fixed the issue.
Alexsander Akers
A: 

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

JWWalker