Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何检测某个应用程序何时打开?_Objective C_Macos_Applescript - Fatal编程技术网

Objective c 如何检测某个应用程序何时打开?

Objective c 如何检测某个应用程序何时打开?,objective-c,macos,applescript,Objective C,Macos,Applescript,在Applescript或Objective-C中,是否有一种方法可以检测某个应用程序何时打开?我的目标是在我正在开发的应用程序中添加一项功能,以便在“QuickTime Player”打开时显示消息,但我在Apple开发者文档中没有找到任何说明如何执行类似操作的内容。使用Objective-C非常简单。代码如下: 从NSWorkspace注册适当的通知: - (void)applicationDidFinishLaunching:(NSNotification *)aNotification

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

使用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.
}

希望有帮助。

这太好了。只有一件事:
userInfo
是否包含应用程序的名称?是的。它可以通过
NSApplicationName
键访问。