Add to iPhoto

  • Revision slug: Mozilla/js-ctypes/Examples/Add_to_iPhoto
  • Revision title: Add to iPhoto
  • Revision id: 110995
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment checkpoint save; 419 words added, 20 words removed

Revision Content

{{ gecko_minversion_header("1.9.3") }}

{{ draft() }}

This extension for Mac OS X serves as a demonstration of how to use js-ctypes to call Mac OS X Carbon, Core Foundation, and other system frameworks from an extension written entirely in JavaScript.

Note: This extension relies on changes added to Gecko as of the April 16, 2010 nightly of mozilla-central.

You can download an installable version of this extension on AMO.

Once installed, when you right-click on an image, you'll see among the options in the contextual menu an option to "Add Image to iPhoto". Choose it, and iPhoto will start up (if it's not already running) and import the image.

Declaring the APIs

The first thing we have to do is declare the Mac OS X APIs we'll be using. This extension uses a number of methods and data types, as well as constants, from three system frameworks.

Since a lot of this stuff is repetitive, we'll only look at selected parts of the code to get an idea how things work. You can download the extension and poke through the code inside it if you'd like to see all of it.

For the sake of organization, I chose to implement each system framework (and, mind you, I only declare the APIs I actually use, not all of them) as a JavaScript object containing all the types and methods that framework's API.

Core Foundation

The majority of the system routines we'll be using come from Core Foundation. Among these are routines for managing CFString, CFURL, and CFArray objects, among others. These are core system data formats that are used by other frameworks, and we'll be making use of them.

The Core Foundation API is implemented by the CoreFoundation object, which consists of two methods to initialize and shut down the library, a reference to the library, and all the types and methods declared to support Core Foundation.

Initializing Core Foundation

The init() method, which sets everything up, looks like this:

  init: function() {
    this.lib = ctypes.open("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation");
    
    // declaring all the APIs goes here
  }

Shutting down Core Foundation

While the Core Foundation system framework itself doesn't need to be shut down, we do need to close the library we opened using the js-ctypes API; that's where the shutdown() method comes in:

  shutdown: function() {
    this.lib.close();
  }

Revision Source

<p>{{ gecko_minversion_header("1.9.3") }}</p>
<p>{{ draft() }}</p>
<p>This extension for Mac OS X serves as a demonstration of how to use js-ctypes to call Mac OS X Carbon, Core Foundation, and other system frameworks from an extension written entirely in JavaScript.</p>
<div class="note"><strong>Note:</strong> This extension relies on changes added to Gecko as of the April 16, 2010 nightly of mozilla-central.</div>
<p>You can <a class=" link-https" href="https://addons.mozilla.org/en-US/firefox/addon/146328/" title="https://addons.mozilla.org/en-US/firefox/addon/146328/">download an installable version</a> of this extension on AMO.</p>
<p>Once installed, when you right-click on an image, you'll see among the options in the contextual menu an option to "Add Image to iPhoto". Choose it, and iPhoto will start up (if it's not already running) and import the image.</p>
<h2>Declaring the APIs</h2>
<p>The first thing we have to do is declare the Mac OS X APIs we'll be using. This extension uses a number of methods and data types, as well as constants, from three system frameworks.</p>
<p>Since a lot of this stuff is repetitive, we'll only look at selected parts of the code to get an idea how things work. You can download the extension and poke through the code inside it if you'd like to see all of it.</p>
<p>For the sake of organization, I chose to implement each system framework (and, mind you, I only declare the APIs I actually use, not all of them) as a JavaScript object containing all the types and methods that framework's API.</p>
<h3>Core Foundation</h3>
<p>The majority of the system routines we'll be using come from Core Foundation. Among these are routines for managing <a class=" external" href="http://developer.apple.com/Mac/library/documentation/CoreFoundation/Reference/CFStringRef/Reference/reference.html" title="http://developer.apple.com/Mac/library/documentation/CoreFoundation/Reference/CFStringRef/Reference/reference.html"><code>CFString</code></a>, <a class=" external" href="http://developer.apple.com/mac/library/documentation/CoreFoundation/Reference/CFURLRef/Reference/reference.html" title="http://developer.apple.com/mac/library/documentation/CoreFoundation/Reference/CFURLRef/Reference/reference.html"><code>CFURL</code></a>, and <a class=" external" href="http://developer.apple.com/mac/library/DOCUMENTATION/CoreFoundation/Reference/CFArrayRef/Reference/reference.html" title="http://developer.apple.com/mac/library/DOCUMENTATION/CoreFoundation/Reference/CFArrayRef/Reference/reference.html"><code>CFArray</code></a> objects, among others. These are core system data formats that are used by other frameworks, and we'll be making use of them.</p>
<p>The Core Foundation API is implemented by the <code>CoreFoundation</code> object, which consists of two methods to initialize and shut down the library, a reference to the library, and all the types and methods declared to support Core Foundation.</p>
<h4>Initializing Core Foundation</h4>
<p>The <code>init()</code> method, which sets everything up, looks like this:</p>
<pre class="brush: js">  init: function() {
    this.lib = ctypes.open("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation");
    
    // declaring all the APIs goes here
  }
</pre>
<h4>Shutting down Core Foundation</h4>
<p>While the Core Foundation system framework itself doesn't need to be shut down, we do need to close the library we opened using the js-ctypes API; that's where the <code>shutdown()</code> method comes in:</p>
<pre>  shutdown: function() {
    this.lib.close();
  }
</pre>
Revert to this revision