Detecting when Safari has launched - macos

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 for NSWorkspaceDidLaunchApplicationNotification. When you receive the notification, examine the instance of NSRunningApplication it provides (object in the notification's userInfo for the key NSWorkspaceApplicationKey) to determine if it was Safari that was launched.

Check the NSDistributedNotificationCenter and NSWorkspace classes.
The following post might be helpful: How to Listen For an Application Launch Event in Mac OS X?

Related

How do I check if OSX is in the process of logging out / shutting down?

I'm writing a Qt application and there's a yet-unsolved bug where Qt doesn't pass notifications of logout/shutdown to apps in osx (it works in windows). When you logout/shutdown my app's windows are closed, I'd like to use that time to somehow query osx to see if it's in the process of logging out / shutting down.
Any ideas?
If you know how to register for notifications, you can register for the NSWorkspaceWillPowerOff notification, which will call your observer method when either a power-down or a logout is requested.

Reacting upon iTunes launch with NSDistributedNotificationCenter

I'm trying to intercept when iTunes starts up on Mac OS X, so I can relaunch my application to work around few bugs in the iTunes Framework.
What I did is to temporarly disable the sandboxing of my application and listen to all NSDistributedNotificationCenter notifications in order to examine them and pick the ones I want.
What I found is that upon start iTunes apparently sends this event:
object: com.apple.iTunes.help name: HelpBookRegistrationDidChange userInfo: (null)
which seems rather unique but also doesn't give an exact clue it is related to a startup event only at 100%.
Does anyone know if this is safe way to intercept such event?
If so, once my app is sandboxed again I need to ask temporary permission to listen to this kind of event, how can I do that in code?
Do you believe scripting bridge can help in this case?
You don't need to disable sandboxing for this, just observe NSWorkspaceDidLaunchApplicationNotification in the notification center provided by NSWorkspace, not the distributed one.
You'll get an instance of NSRunningApplication in the user info of the notification, which you can use to determine whether the launched app was iTunes (use the bundleIdentifier property).

Detect global paste events in cocoa

within my app I'd like to know when something is pasted from the clipboard, even if it does not happen in my application. So if a copy happens in let's say Safari and the paste is happening in let's say TextEdit, then I'd like to get a notification of that in my own application. Is this anyhow possible?
I found two articles on hooking API calls on Mac OS
Is it possible to hook API calls on Mac OS?
and
Hooking Cocoa API?
but i have no idea if this can help me.
Any help is appreciated!
Thanks a lot!
You can observe the keyboard input using Quartz Event Services or Carbon Event Manager.
Note that sandboxing your app will break this feature.

Detect Application Launch Event

Is there a way to somehow hook into the system event of an Application Launch in Mac OS X? For example, let's say that I want a simple script or program to run each time a specific application is opened. I'm hoping that there is some sort of Cocoa API to do this, but I have not found one yet.
Ah, I figured it out on my own. There's a class called NSWorkspace that contains a NSNotificationCenter that you can add observers too. I added an observer for name "NSWorkspaceDidLaunchApplicationNotification" and this seems to do the trick.

Observe other application quit or sudden termination in cocoa app

I need to create wrapper around application, so I created little cocoa app that opens application with [[NSWorkspace sharedWorkspace] launchApplication:…], can I register some event when that application quits or terminates (I certainly need to get event if app is finished good or bad way). I know that I can ask if such application is running every second, but I hope that there is a better way.
Have a look at Technical Note TN2050 "Observing Process Lifetimes Without Polling"
Particularly the NSWorkspace notifications NSWorkspaceDidLaunchApplicationNotification and NSWorkspaceDidTerminateApplicationNotification

Resources