【问题标题】:Finding, from cocoa application, if a package installer (like DivXInstaller.pkg) is running从可可应用程序中查找包安装程序(如 DivXInstaller.pkg)是否正在运行
【发布时间】: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


【解决方案1】:

也许这有点帮助:

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

【讨论】:

  • @user3123201 已编辑 - 检查它。 ;)(另外,您可以删除上述不必要的 cmets)
  • 谢谢你,这项工作......我也可以翻译成obj-c代码!我需要一些改进,但这是一个完美的起点:)
  • @user3123201 - 请记住,这可能不是受支持的(官方)API - 我只是找到它并尝试过......;)
  • 好的,但到目前为止还没有看到任何东西可以做到这一点。这是第一次(或者可能是第一次尝试?)。再次感谢:-)
【解决方案2】:

我添加了一些问题的评论,但我也会提供一些答案。

正如我在评论中提到的:如果应用程序以绝对路径运行,您可能可以使用 org.someIdentity

查找进程

如果您要“监控”的应用程序从同一路径运行,您可能可以按照以下方式执行操作 (bash):

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

这是一个粗略的示例 - 您可能需要稍微扩展路径(我不在 Mac 上),但想法是应用程序/包名称存在于文件结构中(作为目录)。在这些目录上运行 lsof 将告诉我们哪些进程正在使用该目录中的资源(即打开的文件)。根据您的需要,您可能可以传递 lsof 更多参数。 不过,我并不是说这是一种非常漂亮的方法;)

【讨论】:

    【解决方案3】:

    如果使用 OS X 10.8 或更高版本,pgrep 是按应用程序名称返回进程 ID 列表的最佳工具。

    $ pgrep firefox
    905
    

    【讨论】:

    • ..但没有安装在 Mac OSX 上(我也有命令行工具)
    • @user3123201 pgrep 在 OS X 上为我安装。二进制文件应位于 /usr/bin/pgrep
    • 什么 OSX 版本?看这里:unix.stackexchange.com/questions/225/…
    • @user3123201 我明白了。我已经相应地更新了我的答案。
    • 好的,我在 MacPro 上使用 10.7.5。我的 MacBookPro10,1 上也有 10.9 以提供帮助。但是,这样做 pgrep 一个无法使用的命令,我的应用程序也必须在 10.6 中运行。还是谢谢你
    猜你喜欢
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多