ApplescriptまたはObjective-Cのいずれかで、特定のアプリケーションがいつ開いたかを検出する方法はありますか?私の目標は、「QuickTime Player」が開いたときにメッセージを表示する機能をアプリケーションに追加することですが、Appleデベロッパのドキュメントには、このような方法を示すものは何も見つかりませんでした。
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 に答える