Skip arguments between Objective-C applications

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

I know that in Java it is possible to use the following syntax from the command line:

java JavaApp arg1 arg2 arg3 arg4

And it is possible to access them through the main 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 arguments from the command line of a Mac application to a Cocoa Mac application

+4
source share
4 answers

All your answers worked correctly for me, but I found another solution that works best for me. I needed to start a Cocoa application from a command line tool, which I reached with the following line:

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

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

The next problem that appeared was to capture arguments from a Cocoa application. "How to get arguments from AppDelegate.m if main.m is the one that receives them and just returns int."

Among Apple infrastructures and libraries, I found one that definitely solved the problem. This library is called crt_externs.h and contains two useful variables: one for examining the number of arguments and one for getting the arguments themselves.

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

So, inside the AppDelegate from the Cocoa application, we will write the following code to parse the arguments in NSString:

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

As we can see, we go directly to position 1 of the array of arguments, 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 :)

Greetings and happy coding!

0
source

I don't understand why your Restarter app should be a Cocoa app. However, assuming this is the case, command line arguments are available in the NSUserDefaults argument domain. This link shows how to call the program from the command line and how to specify the default name.

Cocoa's way to start a separate process is to use NSTask .

+3
source

If both the command line and graphics applications are written in Objective-C, you can use the NSDistributedNotificationCenter to send notifications between processes. The documentation for the NSDistributedNotificationCenter can be found in the Notification Programming Guide .

Alternatively, Cocoa GUI applications accept command-line options just like any other C program, that is, the argv and argc parameters of the main routine.

+1
source

Below is the category that I wrote in NSWorkspace , which allows you to pass an array of arguments of type argv to the resulting application.

 @interface NSWorkspace (MDAdditions) - (BOOL)launchApplicationAtPath:(NSString *)path arguments:(NSArray *)argv error:(NSError **)outError; @end @implementation NSWorkspace (MDAdditions) - (BOOL)launchApplicationAtPath:(NSString *)path arguments:(NSArray *)argv error:(NSError **)outError { NSParameterAssert(path != nil); BOOL success = YES; if (outError) *outError = nil; FSRef itemRef; OSStatus status = FSPathMakeRef((const UInt8 *)[path UTF8String], &itemRef, NULL); if (status != noErr) { if (anError) *anError = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil]; return NO; } LSApplicationParameters appParameters = {0, kLSLaunchDefaults, &itemRef, NULL, NULL, (argv ? (CFArrayRef)argv : NULL), NULL }; status = LSOpenApplication(&appParameters, NULL); if (status != noErr) { NSLog(@"LSOpenApplication() returned %d for %@", (int)status, path); if (outError) *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil]; return NO; } return YES; } @end 

UPDATED:

As mentioned in answers to Accessing Command Line Arguments in Objective-C , the prefix << 23 → of the _NSGetArgv() and _NSGetArgc() functions is usually a sign that they are private and should be avoided if an alternative (that is) is available.

To get the arguments passed to the executable, you can use NSProcessInfo -arguments , as in the following code:

 NSArray *argv = [[NSProcessInfo processInfo] arguments]; NSArray *args = [argv subarrayWithRange:NSMakeRange(1, argv.count - 1)]; NSLog(@"args == %@", args); 
+1
source

Source: https://habr.com/ru/post/1412592/


All Articles