I have a launch daemon and I would like it to run a function every time an application launches.
I'm currently using NSWorkspace
to check for the launch of applications.
parasited.plist in /Library/LaunchDaemons/
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>parasited</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/parasited</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>OnDemand</key>
<false/>
</dict>
</plist>
parasited main.m
int main(int argc, const char * argv[])
{
@autoreleasepool {
[[[NSWorkspace sharedWorkspace]notificationCenter]addObserver:[[Parasited alloc] init] selector:@selector(inject) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
}
}
It seems like my process, parasited
keeps restarting and I can't figure out why.
My goal is to run a function everytime an app such as "Safari, Mail, Finder, etc" launches.
Your help is appreciated!
Your application has no central run loop in which to loop and wait for notifications.
As an experiment, add a
CFRunLoopRun()
call after your NSWorkspace call. CFRunLoopRun will loop until the process is terminated.See Matt Gallagher's demystifying NSApplication by recreating it post to learn more about the run loop and the role it plays in keeping your application running and processing events.