0

有没有办法在 Applescript 或 Objective-C 中检测某个应用程序何时打开?我的目标是向我正在开发的应用程序添加一项功能,以便在“QuickTime Player”打开时显示一条消息,但我在 Apple 开发人员文档中没有找到任何内容来显示如何执行此类操作。

4

1 回答 1

2

这对于 Objective-C 来说非常简单。这是代码:

从以下位置注册适当的通知NSWorkspace

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    //Fetch the notification center from the workspace
    NSNotificationCenter* center = [[NSWorkspace sharedWorkspace] notificationCenter];

    [center addObserver:self selector:@selector(newApplicationDidLaunch:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];

    [center addObserver:self selector:@selector(newApplicationWillLaunch:) name:NSWorkspaceWillLaunchApplicationNotification object:nil];

}

然后,为通知添加您的选择器。通知的 userInfo 字典将包含您需要知道的所有内容:

-(void)newApplicationDidLaunch:(NSNotification*)notification {

    NSDictionary* userInfo = notification.userInfo;
    //Do what you want here after application launch.
}

-(void)newApplicationWillLaunch:(NSNotification*)notification {

    NSDictionary* userInfo = notification.userInfo;
    //Do what you want here to prepare for application launch.
}

希望有帮助。

于 2013-02-19T22:06:57.220 回答