macOS:是否检测所有应用程序启动,包括后台应用程序? [英] macOS: Detect all application launches including background apps?
问题描述
新手在这里。我正在尝试为应用程序启动创建一个小型侦听器,并且我已经有了:
Newbie here. I'm trying to create a small listener for application launches, and I already have this:
// almon.m
#import <Cocoa/Cocoa.h>
#import <stdio.h>
#include <signal.h>
@interface almon: NSObject {}
-(id) init;
-(void) launchedApp: (NSNotification*) notification;
@end
@implementation almon
-(id) init {
NSNotificationCenter * notify
= [[NSWorkspace sharedWorkspace] notificationCenter];
[notify addObserver: self
selector: @selector(launchedApp:)
name: @"NSWorkspaceWillLaunchApplicationNotification"
object: nil
];
fprintf(stderr,"Listening...\n");
[[NSRunLoop currentRunLoop] run];
fprintf(stderr,"Stopping...\n");
return self;
}
-(void) launchedApp: (NSNotification*) notification {
NSDictionary *userInfo = [notification userInfo]; // read full application launch info
NSString* AppPID = [userInfo objectForKey:@"NSApplicationProcessIdentifier"]; // parse for AppPID
int killPID = [AppPID intValue]; // define integer from NSString
kill((killPID), SIGSTOP); // interrupt app launch
NSString* AppPath = [userInfo objectForKey:@"NSApplicationPath"]; // read application path
NSString* AppBundleID = [userInfo objectForKey:@"NSApplicationBundleIdentifier"]; // read BundleID
NSString* AppName = [userInfo objectForKey:@"NSApplicationName"]; // read AppName
NSLog(@":::%@:::%@:::%@:::%@", AppPID, AppPath, AppBundleID, AppName);
}
@end
int main( int argc, char ** argv) {
[[almon alloc] init];
return 0;
}
// build: gcc -Wall almon.m -o almon -lobjc -framework Cocoa
// run: ./almon
注意:当我构建它时,它将运行良好,但是如果在High Sierra上使用Xcode 10进行操作,则将获得 ld
警告,但是您可以忽略它。
Note: when I build it, it will run fine, but if you do it with Xcode 10 on High Sierra, you will get ld
warnings, which you can ignore, however.
我的问题:有没有办法检测到启动后台应用程序,例如粘度等菜单栏应用程序?苹果公司表示
My question: Is there a way to also detect a launch of a background application, e.g. a menu bar application like Viscosity etc.? Apple says that
系统不会针对后台应用发布
[NSWorkspaceWillLaunchApplicationNotification]或针对具有以下功能的应用发布
Info.plist
文件中的LSUIElement
键。
如果您想知道何时启动或终止所有应用程序(包括后台应用程序),请使用键值观察来监视runningApplications
方法。
the system does not post [NSWorkspaceWillLaunchApplicationNotification] for background apps or for apps that have the
LSUIElement
key in theirInfo.plist
file. If you want to know when all apps (including background apps) are launched or terminated, use key-value observing to monitor the value returned by therunningApplications
method.
我至少会尝试向侦听器添加对后台应用程序等的支持,但是我不知道该怎么做。有任何想法吗?
I would at least try to add support for background apps etc. to the listener, but I don't know how to go about it. Any ideas?
推荐答案
如文档所示,您可以使用键值观察来观察 runningApplications <共享工作区对象的/ code>属性:
As the document suggests, you use Key-Value Observing to observe the runningApplications
property of the shared workspace object:
static const void *kMyKVOContext = (void*)&kMyKVOContext;
[[NSWorkspace sharedWorkspace] addObserver:self
forKeyPath:@"runningApplications"
options:NSKeyValueObservingOptionNew // maybe | NSKeyValueObservingOptionInitial
context:kMyKVOContext];
然后,您将实现观察方法(使用Xcode的现成代码段):
Then, you would implement the observation method (using Xcode's ready-made snippet):
- (void) observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
{
if (context != kMyKVOContext)
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
return;
}
if ([keyPath isEqualToString:@"runningApplications"])
{
<#code to be executed when runningApplications has changed#>
}
}
这篇关于macOS:是否检测所有应用程序启动,包括后台应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!