title: Cocoa app open file via double-click or right-click
body: <p>I'm creating an non-document based application that can open and display various image types. In my Info.plist I've added the supported file types with their identifiers (public.png, public.jpeg, public.tiff, etc.). In my AppDelegate I've implemented the <code>-[application:openFile:]</code> method (as well as the <code>-[application:openFiles:]</code> methods).</p>
  
  <p>Here is a sample of what my Info.plist looks like:</p>
  
  <pre><code>&lt;key&gt;CFBundleDocumentTypes&lt;/key&gt;
  &lt;array&gt;
      &lt;dict&gt;
          &lt;key&gt;CFBundleTypeRole&lt;/key&gt;
          &lt;string&gt;Editor&lt;/string&gt;
          &lt;key&gt;LSItemContentTypes&lt;/key&gt;
          &lt;array&gt;
              &lt;string&gt;public.png&lt;/string&gt;
          &lt;/array&gt;
      &lt;/dict&gt;
  
      &lt;dict&gt;
          &lt;key&gt;CFBundleTypeRole&lt;/key&gt;
          &lt;string&gt;Editor&lt;/string&gt;
          &lt;key&gt;LSItemContentTypes&lt;/key&gt;
          &lt;array&gt;
              &lt;string&gt;public.jpeg&lt;/string&gt;
          &lt;/array&gt;
      &lt;/dict&gt;
  
  ...
  
  &lt;/array&gt;
  </code></pre>
  
  <p>When I right-click a PNG and select my application, my application opens but neither the <code>-[application:openFile:]</code> method nor the <code>-[application:openFiles:]</code> method is being called. If, after my application has opened, I right-click the file and click on my application again, the <code>-[application:openFiles:]</code> method gets called and everything works as normal.</p>
  
  <p>I've tried ONLY implementing the <code>-[application:openFile:]</code> method, and everything works the same as before. Same when ONLY implementing the <code>-[application:openFiles:]</code> method. When I implement both the <code>-[application:openFiles:]</code> method takes precedence.</p>
  
  <p>I've added logging to see how my app is initialized at startup. When my app isn't open and I right-click the PNG image, my app's methods are called in the following order:</p>
  
  <pre><code>- [ViewController viewDidLoad]
  - [AppDelegate applicationWillFinishLaunching:]
  - [AppDelegate applicationDidFinishLaunching:]
  </code></pre>
  
  <p>With my app open, right-clicking the image again produces the following method calls:</p>
  
  <pre><code>- [AppDelegate application:openFiles:]
  </code></pre>
  
  <p>After reading the <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSApplicationDelegate_Protocol/index.html#//apple_ref/occ/intfm/NSApplicationDelegate/application:openFile:" rel="nofollow">documentation</a>, it says that if a user double clicks on a file, the order of calls should be:</p>
  
  <pre><code>- [AppDelegate applicationWillFinishLaunching:]
  - [AppDelegate application:openFile(s):]
  - [AppDelegate applicationDidFinishLaunching:]
  </code></pre>
  
  <p>yet the open file method never gets called.</p>
  
  <p>I've already read a bunch of other stackoverflow posts and followed those suggestions, but none of them have worked. Does anyone have any ideas as to why this might be happening?</p>