Search code examples
macoslaunching-application

How to launch a dependent application on OS X?


What is the programmatical mechanism by which an application can be launched at a result of another one being launched?

E.g. upon launching iTunes, the Last.fm desktop launches too.

Or it is just a question of substituting a "batch file" of some sort?

Note: OS X newbie.


Solution

  • There's no standard way to specify a dependent app. So, you need to somehow watch the system. You don't have to actively poll; you need to use some system mechanism to tell you.

    To watch for a GUI app to launch, you need to write a background app, get the NSWorkspace's notification center by

    NSNotificationCenter* nc=[[NSWorkspace sharedWorkspace] notificationCenter];
    

    and watch for NSWorkspaceDidLaunchApplicationNotification. See the Apple doc.

    To watch for a non-GUI app to launch, you need to use kqueue. (This is a BSD feature.) See the man page. Typically you have to watch launchd to fork or execve processes. Note that there are multiple launchd processes, one for root and one for each logged-in users.

    In any case you need to start automatically the background app you write. There're many ways to do that.

    To watch a file or a directory to change, you can use launchd agents. See the man page.

    By the way: the official Last.fm app, on its first launch, installs AudioScrobbler.bundle inside ~/Library/iTunes/iTunes Plug-ins. iTunes reads all the plug-ins when launched, and the plug-in then launches the Last.fm app. It's a rather hackish, non-condoned usage of the interface, because the plug-in is for the visualizer. See Apple doc.