title: Getting current working directory from a Sandboxed command-line app
body: <p>I'm now trying to sandbox my command-line app which is written in Objective-C.</p>
  
  <h3>Issue</h3>
  
  <p>When the app was not sandboxed, I could get the directory where the user invoked the app using <code>[[NSFileManager defaultManager] currentDirectoryPath]</code>.
  However, after sandboxing, the app always returns for current directory <code>/Users/{MY_ACCOUNT}/Library/Containers/{BUNDLE_IDENTIFIER}/Data</code> no matter from where the command-line app was invoked.</p>
  
  <h3>Example Code</h3>
  
  <p>Actually, I wanna know the absolute path to the files which the user passed as command arguments.</p>
  
  <p>So when, for example, I invoke my command-line app like this:</p>
  
  <pre><code>$ pwd
  /Users/MY_ACCOUNT/Desktop
  $ ./foo/my_executable subfolder/myfile.txt
  </code></pre>
  
  <p>The following part:</p>
  
  <pre><code>NSURL *url = [NSURL fileURLWithFileSystemRepresentation:argv[1] isDirectory:NO relativeToURL:nil];
  printf("path: %s\n", [[url absoluteURL] fileSystemRepresentation]);
  </code></pre>
  
  <p>returns on <strong>non-Sandboxed</strong> app correctly:</p>
  
  <pre><code>path: /Users/MY_ACCOUNT/Desktop/subfolder/myfile.txt
  </code></pre>
  
  <p>but on <strong>Sandboxed</strong> app:</p>
  
  <pre><code>path: /Users/MY_ACCOUNT/Library/Containers/BUNDLE_IDENTIFIER/Data/subfolder/myfile.txt
  </code></pre>
  
  <p>This is of course incorrect.</p>
  
  <h3>Question</h3>
  
  <p>Is there any alternative way to get the correct full paths of the passed file path arguments in a Sandboxed command-line app?
  Or is it just impossible and it's better I just give up to sandbox my app?</p>
  
  <hr>
  
  <h2>Solution</h2>
  
  <p>Finally, I found one of the solution by myself.</p>
  
  <p>The <code>environment</code> property in <code>[NSProcessInfo processInfo]</code> has the correct current user working directory even the app is Sandboxed.</p>
  
  <pre><code>NSDictionary *env = [[NSProcessInfo processInfo] environment];
  NSString *currentPath = env[@"PWD"];
  </code></pre>
  
  <p>At least, when I call the command in Terminal.app, it works. I'm not sure whether this <code>PWD</code> exists always.</p>
  
  <p>Furthermore, As @daij-djan pointed out below, we don't have even the read permission for the given arguments. However, anyway the matter of this question is once resolved.</p>
  
  <p>(If there is someone who know the way to read the file of which path was passed as an argument, please tell me as an answer!)</p>