title: Cocoa code for launching an app with parameters
body: <p>I want a Cocoa equivalent of the command line tool <code>open(1)</code>, especially for the usage in this form:</p>
  
  <pre><code>open -a &lt;SomeApplication&gt; &lt;SomeFile&gt; --args &lt;Arg1&gt; &lt;Arg2&gt; ...
  </code></pre>
  
  <p>Take QuickTime as an example. The following command line will open a file using QuickTime, and the arguments can control if QuickTime plays the file on startup.</p>
  
  <pre><code>open -a "QuickTime Player" a.mp4 --args -MGPlayMovieOnOpen 0 # or 1
  </code></pre>
  
  <p>I have read <a href="https://stackoverflow.com/questions/5048677/launching-an-mac-app-with-objective-c-cocoa">Launching an Mac App with Objective-C/Cocoa</a>. <a href="https://stackoverflow.com/a/5048968/711135">prosseek's answer</a>, which I think is equivalent to <code>open -a &lt;App&gt; &lt;File&gt;</code>, works well when I do not specify any argument. <a href="https://stackoverflow.com/a/5048975/711135">ughoavgfhw's answer</a>, which I think is equivalent to <code>open -a &lt;App&gt; --args &lt;Arg1&gt; &lt;Arg2&gt; ...</code>, works well when I do not specify any file to open. But neither can specify a filename and arguments at the same time.</p>
  
  <p>I have also tried to append the filename to the argument list, which is the common way used by unix programs. It seems that some applications can accept it, but some cannot. QuickTime prompts an error saying it cannot open the file. I am using the following code.</p>
  
  <pre><code>NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
  NSURL *url = [NSURL fileURLWithPath:[workspace fullPathForApplication:@"QuickTime Player"]];
  NSArray *arguments = [NSArray arrayWithObjects:@"-MGPlayMovieOnOpen", @"0", @"a.mp4", nil];
  [workspace launchApplicationAtURL:url options:0 configuration:[NSDictionary dictionaryWithObject:arguments forKey:NSWorkspaceLaunchConfigurationArguments] error:nil];
  // open -a "QuickTime Player" --args -MGPlayMovieOnOpen 0 a.mp4
  </code></pre>
  
  <p>It seems that the mechanism in opening files differs from usual arguments. Can anyone explain the internals of <code>open(1)</code>, or just give me a solution? Thanks.</p>