通过Objective-C的应用程序之间的争论 [英] Pass arguments between Objective-C applications

查看:181
本文介绍了通过Objective-C的应用程序之间的争论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果有可能的Mac应用程序之间,如果可能的话,怎么样。

I was wondering if it is possible to pass arguments between Mac applications and if it possible, how.

我知道,在Java中,可以使用以下语法在命令行:

I know that in Java is possible using the following syntax from the command line:

Java的JavaApp ARG1参数3 ARG4

java JavaApp arg1 arg2 arg3 arg4

和这是可能访问这些通过主的ARGS []数组。

And that is possible to access those through the main's args[] array.

public static void main(String[] args) {
        System.out.println("d");
        for (int i = 0; i < args.length; i++)
            System.out.println(args[i]);
}

编辑:我想从的传递命令行参数的Mac适用的到的可可的Mac 的应用

I want to pass the arguments from a command line Mac application to a Cocoa Mac application

推荐答案

您所有的正确答案为我工作,但我发现适合我的好需要另一种解决方案。
我需要从一个命令行工具,我与以下行实现发射可可应用程序:

All your answers worked properly for me, but I found another solution that suits better my needs. I needed to launch a Cocoa app from a Command Line Tool, which I achieved with the following line:

system("nohup /PATH/Arguments.app/Contents/MacOS/Arguments argument1 argument2 &");

的nohup 的是UNIX服务,它可以让你,所以如果你关闭终端窗口,这个过程仍然活着流程连接到自身。

nohup is unix service that allows you to attach processes to itself so if you close the terminal window, the process remains alive.

这走过来是从可可应用程序中获取的参数下的问题。 我怎么会得到从 AppDelegate.m 如果 main.m文件的参数是接收他们,只是返回一个一个int。

Next problem that came along was capturing the arguments from within the Cocoa app. "How would I get the arguments from AppDelegate.m if main.m is the one that receives them and just returns an int."

在苹果公司的框架和库,我发现一个正好解决了这个问题。这个库被称为 crt_externs.h 并包含两个有用变量,一个学习的参数的数目,和另一个以获得参数本身

Among Apple's frameworks and libraries I found one that exactly solved the problem. This library is called crt_externs.h and contains two useful variables, one to learn the number of arguments, and another one to obtain the arguments themselves.

extern char ***_NSGetArgv(void);
extern int *_NSGetArgc(void);

所以,里面的的 AppDelegate中的的从可可应用程序,我们这样写code解析参数到的的NSString的的:

So, inside at the AppDelegate's from the Cocoa app we would write the following code to parse the arguments into NSString's:

char **argv = *_NSGetArgv();
NSString *argument1 = [NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding];
NSString *argument2 = [NSString stringWithCString:argv[2] encoding:NSUTF8StringEncoding];

我们可以看到我们直接跳到参数数组的位置1,因为位置0包含路径本身:

As we can see we skip directly to the position 1 of the arguments array since position 0 contains the path itself:

argv[0] = '/PATH/Arguments.app/Contents/MacOS/Arguments'
argv[1] = 'argument1'
argv[2] = 'argument2'

感谢大家的时间和帮助。我从你们那里学到了很多东西。我也希望这个答案可以帮助别人:)

Thank you all for your time and help. I learned a lot from you guys. I also hope this answer helps someone else :)

欢呼和编码快乐!

这篇关于通过Objective-C的应用程序之间的争论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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