4

我正在为 iTunes 开发一种插件。

很多用户都要求,如果他们启动 iTunes,他们想启动插件,这当然是有道理的。但是,我不确定如何执行此操作。

我想到了一个帮助应用程序,这可能是唯一的方法。唯一困扰我的是如何获得通知。当然,我可以一直检查 iTunes 是否正在运行,但我不确定这是否是正确的方法。

我宁愿将我的应用程序添加为该过程的观察者。那可能吗?

如果没有,活动监视器是如何做到的?


解决方案

感谢 Daij-Djan!我让它像这样工作:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
                                                           selector:@selector(iTunesLaunched:)
                                                               name:NSWorkspaceDidLaunchApplicationNotification
                                                             object:nil];

    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
                                                           selector:@selector(iTunesTerminated:)
                                                               name:NSWorkspaceDidTerminateApplicationNotification
                                                             object:nil];
}

-(void) iTunesLaunched:(NSNotification *)notification {
    NSRunningApplication *runApp = [[notification userInfo] valueForKey:@"NSWorkspaceApplicationKey"];
    if ([runApp.bundleIdentifier isEqualToString:@"com.apple.iTunes"])
        NSLog(@"start");

}

-(void) iTunesTerminated:(NSNotification *)notification {
    NSRunningApplication *runApp = [[notification userInfo] valueForKey:@"NSWorkspaceApplicationKey"];
    if ([runApp.bundleIdentifier isEqualToString:@"com.apple.iTunes"])
        NSLog(@"terminate");

}
4

1 回答 1

5

register for NSWorkspace notifications:
NSWorkspaceDidLaunchApplicationNotification
NSWorkspaceDidTerminateApplicationNotification

see https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/Reference/Reference.html


there is also the possibility to KVO the runningApplications property
btw cocoatech has a nice NTRunningAppManager class that does just that

于 2012-12-23T11:22:08.893 回答