views:

31

answers:

1

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.

+2  A: 

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.

Yuji
I couldn't figure out how to do this from the man page you are referring to. Could you expand on this one?But +1 for the "last.fm" rundown!
jldupont
Ah, sorry for putting confusion information... `launchd` agents can watch for the appearance of new files in a directory, but it can't watch for the launching of a new app. You can use `kqueue`, but that's not an elegant way... I'll add the info on kqueue.
Yuji
Still, marvelous info for an OS X newbie... thanks!
jldupont