简体   繁体   English

从可可应用程序中查找程序包安装程序(例如DivXInstaller.pkg)是否正在运行

[英]Finding, from cocoa application, if a package installer (like DivXInstaller.pkg) is running

I do not know if this is possible, so I hope that the question does not appear stupid: I can get in a cocoa application, or even using a bash script, the pid of a specific running Application. 我不知道这是否可行,所以我希望这个问题不会显得愚蠢:我可以进入可可应用程序,甚至可以使用bash脚本获取特定运行的应用程序的pid。 So I can take actions (such as alert and ask if you want to close it). 因此,我可以采取措施(例如警报并询问您是否要关闭它)。 We can the same with a package (pkg), knowing its id (like "org.someIdentity.pkg") in some way? 我们可以用一个包(pkg)相同,以某种方式知道它的ID(例如“ org.someIdentity.pkg”)吗?

EDIT this is what i use in cocoa: 编辑这是我在可可粉中使用的:

    - (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];


    }
}

EDIT 编辑

what i used with bash: 我用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

EDIT also the bash script can be used in cocoa in this way: 还可以通过以下方式编辑bash脚本在可可中的使用:

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

I added a comment with some questions, but I will provide some kind of answer too. 我添加了一些问题的评论,但我也会提供某种答案。

As mentioned in my comment: If the applications run with absolute path you can probably look for processes with org.someIdentity 如我的评论中所述:如果应用程序以绝对路径运行,则可能可以使用org.someIdentity查找进程。

If applications you want to 'monitor' are running from the same path(s), you could probably do something along these lines (bash): 如果要“监视”的应用程序是从相同路径运行的,则可以按照以下方式(bash)执行一些操作:

apps="org.someIdentity org.someOtherIdentity"
for app in ${apps}; do
  echo "Checking app ${app}..."
  lsof /Applications/${app}
done

This is a crude example - you might have to expand on the paths a bit (I'm not on a Mac), but the idea is that the application/package names exist in the file structure (as directories). 这是一个粗略的示例-您可能必须在路径上进行一些扩展(我在Mac上不是),但是想法是应用程序/程序包名称存在于文件结构中(作为目录)。 Running lsof on these directories will tell us which processes are using resources in that directory (ie opened files). 在这些目录上运行lsof将告诉我们哪些进程正在使用该目录中的资源(即打开的文件)。 Depending on your needs you can probably pass lsof some more parameters. 根据您的需要,您可能可以传递lsof其他参数。 I'm not saying this is a very pretty approach, though ;) 不过,我并不是说这是一种非常漂亮的方法;)

If using OS X 10.8 or later, pgrep is the best tool to return a list of process IDs by application name. 如果使用OS X 10.8或更高版本,则pgrep是按应用程序名称返回进程ID列表的最佳工具。

$ pgrep firefox
905

Maybe this helps a bit: 也许这会有所帮助:

app=${1:-VirtualBox}   #tested on it

run_searched() {
    echo "The installer installing $1 as: $2"
}
run_other() {
    echo "The installer NOT installing $1. (but installing: $2)"
}
not_running() {
    echo "The installer is not running"
}

isasdir="$HOME/Library/Saved Application State/com.apple.installer.savedState"
isasfile="$isasdir/windows.plist"
if [[ -d "$isasdir" && -f "$isasfile" ]]
then
    win=$(plutil -p "$isasfile" | grep '"NSTitle"' | sed 's:.*"NSTitle".*=>.*"\([^"]*\)":\1:')
    if [[ "$win" =~ $app ]]
    then
        run_searched "$app" "$win"
    else
        run_other "$app" "$win"
    fi
else
    not_running
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM