如何识别/标记OS X应用实例 [英] How to identify/mark OS X app instance

查看:77
本文介绍了如何识别/标记OS X应用实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以使用不同参数多次启动的应用程序,并且我需要一种方法让一个应用程序实例查看其他应用程序对参数的要求(主要是确保两个实例不使用相同的参数运行,并且专注于类似实例).我当前正在使用pid文件,但想知道是否有一种方法可以以某种方式将正在运行的实例标记为其他实例可见.我为每个实例更改CFBundleName,但它似乎不可见(只是原始名称,没有更改).有没有比pid文件更好的方法?

I have an app which can be launched multiple times with different parameters and I need a way for one app instance to see what the other ones have for parameters (mainly to ensure that two instances do not run with the same params AND to focus on similar instance). I am currently using pid files but wonder if there is a way to mark running instance in some way visible from the other instances. I change CFBundleName for each instance, but it seems not visible (just the original name, not the changed). Is there a better way than pid files?

一些细节:主应用程序是一个容器,该容器运行另一个可以访问该容器的内部应用程序(即更改CFBundleName等)

Some detail: the main application is a container which runs another internal application which has access to the container (i.e. to change CFBundleName etc.)

推荐答案

我假设参数"是指命令行参数?您可以使用popen运行ps,然后捕获并解析所需信息的输出.如果您已经知道其他进程的pid,则可以查找,然后使用grep来查找应用程序的名称,以减少需要查看的输出.

I assume by "parameters" you mean the command-line arguments? You could run ps using popen, then capture and parse the output for the information you need. If you already know the pids for the other processes, you can look for that, and grep for the name of your app to reduce the output you need to look at.

这里是一个例子.您可以看到参数.

Here is an example. You can see the arguments.

$ /bin/ps -Axo pid,args|grep TextWrangler|grep -v grep

643 /Applications/TextWrangler.app/Contents/MacOS/TextWrangler
645 /Applications/TextWrangler.app/Contents/Helpers/Crash Reporter.app/Contents/Helpers/crash-catcher -targetPID 643 -targetBundlePath /Applications/TextWrangler.app -showEmailAddressField 1

以下是使用popen的方法,并将输出通过管道传递到grep,以查找您的命令名,pid和参数:

Here is how to use popen, and pipe the output to grep to look for your command name, pid, and arguments:

std::string cmdline("/bin/ps -Axo pid,args|grep '");
cmdline += appName;
cmdline += "'|grep -v grep";
// The output will look like: "S 428 APPNAME ARGS" with one space between entries.
// popen creates a pipe so we can read the output of the program we are invoking.
FILE *instream = popen(cmdline.c_str(), "r");
if(instream) {
    // read the output, one line at a time.
    const int MAX_BUFFER = 1024;
    char buffer[MAX_BUFFER];
    while (!feof(instream)){
        if (fgets(buffer, MAX_BUFFER, instream) != NULL) {
            std::string temp(buffer);
            temp.trim();    // Get rid of leading and trailing whitespace. (Exercise for the reader.)
            if(!temp.empty()) {
                // First col is the state.
                std::string::size_type i = temp.find(" "); 
                std::string state = temp.substr(0, i);
                // Skip zombies.
                if("Z" != state) {
                    // Second col is the pid.
                    // Advance i to the next char after the space.
                    ++i;
                    std::string::size_type j = temp.find(" ", i); 
                    std::string pidStr = temp.substr(i, j - i);

// Here we know the pid for APPNAME. You can further parse for the command line arguments here.

            }
        }
    }
}
// close the pipe.
pclose(instream);

这篇关于如何识别/标记OS X应用实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆