【发布时间】:2014-09-17 14:19:29
【问题描述】:
我不知道这是否可能,所以我希望这个问题不会显得愚蠢: 我可以进入一个可可应用程序,甚至可以使用 bash 脚本,一个特定正在运行的应用程序的 pid。所以我可以采取行动(例如警告并询问您是否要关闭它)。 我们可以对一个包 (pkg) 进行同样的处理,以某种方式知道它的 id(如“org.someIdentity.pkg”)?
编辑 这是我在可可中使用的:
- (void)unWantedApp:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSLog(@"userInfo == %@", userInfo);
if ([NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.blabla"] || [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.blabla2"]) {
[[NSAlert alertWithMessageText:[NSString stringWithFormat:NSLocalizedString(@"blabla is running..", nil), [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleNameKey]]
defaultButton:nil alternateButton:nil otherButton:nil informativeTextWithFormat:NSLocalizedString(@"Well, I hope you are not joking with more then one Tool at same time...", nil)] runModal];
}
}
编辑
我在 bash 中使用了什么:
isOpen=$(ps auxwww | grep '/Applications/blabla.app' | grep -v 'grep' | wc -l)
if [ -e '/Applications/blabla.app' ]; then
if [ $isOpen -ge 0 ]; then
# do what you are intresting for... But a packages is not in Application folder and is opened by Installer.app :-(
fi
fi
编辑 bash 脚本也可以通过这种方式在 cocoa 中使用:
#include <stdio.h>
#include <stdlib.h>
#define SCRIPT "\
#/bin/bash \n\
if [ -e '/Applications/blabla.app' ];then\n\
#variable and other statement here(escape all special caracter and manually add the new line ->> \n)\n\
else\n\
exit 0\n\
fi"
int main()
{
system(SCRIPT);
return 0;
}
【问题讨论】:
-
你提到了 bash 脚本......那么
ps命令有什么问题?很难说你真正想要完成什么。 -
PID 与正在运行的进程相关联,而不是与应用程序包相关联。
-
...好吧,一旦该应用程序包中的某些内容运行,它就是一个进程。带有 PID。您如何确定要开始的应用程序列表?我假设您无论如何都在挑选一些有趣的应用程序,因此找到一个进程名称应该不那么难。如果应用程序以绝对路径运行,您可能仍然可以查找 org.someIdentify。
-
是的,很难找到我要找的东西,所以我问了一个问题。我唯一能找到的是“安装者”,但不是他的孩子(如果我们可以这么说的话),但这还不够。感谢您的评论。
-
org.someIdentity?是的!是我找不到正在运行的包的东西。是的,我的应用程序很有趣....并且希望确保其他应用程序(或执行类似操作或冲突的程序包)没有运行。
标签: objective-c macos bash cocoa installation