I’m trying to get my mac app to do something whenever Safari is launched. The obvious way to detect Safari launch is by polling for running processes. Is there a better way to do this?
I was thinking there might be some API that I could use to register callbacks on, or perhaps there’s a notification center event that I can observe.
You can add yourself to
NSWorkspace‘s notification center as an observer forNSWorkspaceDidLaunchApplicationNotification. When you receive the notification, examine the instance ofNSRunningApplicationit provides (object in the notification’s userInfo for the keyNSWorkspaceApplicationKey) to determine if it was Safari that was launched.