Observe other application quit or sudden termination in cocoa app - cocoa

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

Related

User preferences are not saved from XPC service

In my main app bundle I have supporting XPC service included. App is sandboxed and everything works quite fine, except that when I call [[NSUserDefaults standardUserDefault] setObject:forKey:] method and than - synchronize method from the XPC service app, preferences are not written and data cannot be retrieved next time I need it.
I didn't find anything related to this problem in Apple's documentation, except that the sandboxed app cannot access preferences of other apps. That's all right, I don't need it. XPC service has its own container in ~/Library/Containers, where it should be able to store its own data, I'd suppose. But obviously it's not the case for some reason.
I probably missed something, but cannot find what. Is there anything special which needs to be done (adding some entitlement or so) in order to make this work?
Thanks for any tips.
I believe you'll need to use Group Containers to share the preferences and I have achieved something similar (a non-UI LSUIElement app sharing preferences with its conventional Preferences app countpart) using RMSharedPreferences.

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).

How does Reminders launch the app?

I am having a few problems getting my Windows Phone 7 app-specific Reminders to work properly - the app re-closes after a fraction of a second after starting to show the splash-screen.
In order to debug this, I'd like to know how a Reminder launches an app, which events are fired and so on.
For what it might be worth - here is what I found out.
A Reminder restarts the app, triggering the Launching-event in App.xaml.cs. That is - you need to re-load anything you need to make the app work from Isolated Storage or the web
When the Launching event is done with, the app goes straight to the requested view - skipping any loading-page you might have made for your app. Again, remember to load whatever resources you need from Isolated Storage.
Then the app fires the view's constructor, before OnNavigatedTo and Loaded goes off.

Detecting when Safari has launched

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?

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.

Resources