gpt4 book ai didi

objective-c - macOS:检测所有应用程序启动,包括后台应用程序?

转载 作者:行者123 更新时间:2023-12-03 17:09:06 27 4
gpt4 key购买 nike

这里是新手。我正在尝试为应用程序启动创建一个小型监听器,并且我已经有了:

// 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 警告,不过您可以忽略该警告。

我的问题:有没有办法也检测后台应用程序的启动,例如像粘度等菜单栏应用程序?苹果是这么说的

the system does not post [NSWorkspaceWillLaunchApplicationNotification] for background apps or for apps that have the LSUIElement key in their Info.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 the runningApplications method.

这里:https://developer.apple.com/documentation/appkit/nsworkspacewilllaunchapplicationnotification?language=objc

我至少会尝试向监听器添加对后台应用程序等的支持,但我不知道如何去做。有什么想法吗?

最佳答案

正如文档所建议的,您可以使用键值观察来观察共享工作区对象的 runningApplications 属性:

static const void *kMyKVOContext = (void*)&kMyKVOContext;


[[NSWorkspace sharedWorkspace] addObserver:self
forKeyPath:@"runningApplications"
options:NSKeyValueObservingOptionNew // maybe | NSKeyValueObservingOptionInitial
context:kMyKVOContext];

然后,您将实现观察方法(使用 Xcode 的现成代码片段):

- (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#>
}
}

关于objective-c - macOS:检测所有应用程序启动,包括后台应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52710089/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com