I was wondering if it is possible to pass arguments between Mac applications and if it possible, how.
I know that in Java is possible using the following syntax from the command line:
java JavaApp arg1 arg2 arg3 arg4
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]);
}
Edit: 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:
nohup is unix service that allows you to attach processes to itself so if you close the terminal window, the process remains alive.
Next problem that came along was capturing the arguments from within the Cocoa app. “How would I get the arguments from
AppDelegate.mifmain.mis the one that receives them and just returns an int.”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.
So, inside at the AppDelegate’s from the Cocoa app we would write the following code to parse the arguments into NSString’s:
As we can see we skip directly to the position 1 of the arguments array since position 0 contains the path itself:
Thank you all for your time and help. I learned a lot from you guys. I also hope this answer helps someone else 🙂
Cheers and happy coding!