这里是新手。我正在尝试为应用程序启动创建一个小型侦听器,并且我已经有了:
// 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
但是,您可以忽略警告。
我的问题:有没有办法也检测到启动后台应用程序,例如像粘度等菜单栏应用程序?苹果是这么说的
系统不发帖
[NSWorkspaceWillLaunchApplicationNotification] 用于后台应用程序或
对于具有以下功能的应用程序LSUIElement
关键在他们的Info.plist
文件。
如果您想知道所有应用程序(包括后台应用程序)何时
启动或终止,使用键值观察来监控值
由返回runningApplications
method.
Here: https://developer.apple.com/documentation/appkit/nsworkspacewilllaunchapplicationnotification?language=objc https://developer.apple.com/documentation/appkit/nsworkspacewilllaunchapplicationnotification?language=objc
我至少会尝试向侦听器添加对后台应用程序等的支持,但我不知道如何去做。有任何想法吗?