A Demonstration of the NativeProcess APIs in AIR 2

SearchCentral uses the new NativeProcess APIs in AIR 2 in order to integrate with Spotlight and provide very fast local file system search. Here’s a demo:

The NativeProcess APIs are very simple to use. The code below invokes the mdfind command on OS X, and sets up an event listener to read the results from standard out:

private function onSearch():void{var npInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();npInfo.executable = this.mdfindFile; // A reference to the mdfind commandvar args:Vector.<String> = new Vector.<String>;args.push("-interpret");args.push(this.searchTerm.text);npInfo.arguments = args;this.processBuffer = new ByteArray();this.nativeProcess = new NativeProcess();this.nativeProcess.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onStandardOutputData);this.nativeProcess.addEventListener(NativeProcessExitEvent.EXIT, onStandardOutputExit);this.setStatus("Searching for " + this.searchTerm.text + "...", true);this.nativeProcess.start(npInfo);}

The code below shows buffering the data from standard out:

private function onStandardOutputData(e:ProgressEvent):void{this.nativeProcess.standardOutput.readBytes(this.processBuffer, this.processBuffer.length);}

And finally, the code blow is an abbreviated version of the function that parses the data from standard out and puts it into the data grid:

private function onStandardOutputExit(e:Event):void{var output:String = new String(this.processBuffer);var outputArray:Array = output.split("\n");var data:Array = new Array();for each(var path:String in outputArray){var f:File = new File(path);if (!f.exists) continue;var o:Object = new Object();if (f.isDirectory){o.name = "/" + f.name;}else{o.name = f.name;}o.type = f.extension;o.lastModified = f.modificationDate;o.path = f.nativePath;data.push(o);}var dp:ArrayCollection = new ArrayCollection(data);this.fileGrid.dataProvider = dp;this.setStatus(dp.length + " items found");}

The code for SearchCentral will be available on on Adobe Labs as soon as the AIR 2 Beta is public (very soon!). The code for SearchCentral is now available on Google Code. If you have any questions, drop them in the comments below.

7 Responses to A Demonstration of the NativeProcess APIs in AIR 2

  1. Zed-K says:

    This looks really good, I can’t wait to play with the first beta =)Hoping the Linux version will be available too!

  2. lordB8r says:

    This looks promising.One question though, what kind of implications does this pose if your AIR app can now run executibles without user permission?I love seeing how quickly the search mechanism returns results.

  3. yuku says:

    What is the reason that apps that can launch native processes need to have different installers?

  4. Christian Cantrell says:

    @lordB8r: There really aren’t any new security implications. Any installed native application can already do this, so we aren’t giving AIR any special capabilities that all other applications don’t already have. When a user installs any kind of application (AIR or otherwise), they are placing their trust in the application publisher. The ability for AIR 2 apps to launch native processes doesn’t make them any more or less dangerous than any other application.

  5. Christian Cantrell says:

    @yuka: The .air extension and installation experience is being reserved for applications that are cross-platform, and should run pretty much identically across platforms. Applications that use native code won’t run on Mac, Windows, and Linux, so we don’t want end users trying to install applications that won’t work properly. By requiring applications that want to launch native processes to use native installers, we’re ensuring that end users can’t inadvertently install applications that won’t work.

  6. Wow, it’s such a wrap. What a time to be alive! Haha. The possible uses for AIR will be endless now. I am kind of curious though about the need to package platform dependent installers. Since the NativeProcess calls are kind of their own layer, wouldn’t it make sense to give people the ability to specify alternate Native calls? I’m sure there will still be certain applications that use native code that for which there’s just no equivalent in the other os, but your file searching example – for example – could definitely be platform…uhh…maybe not independent – let’s call it platform flexible 🙂

  7. Jeff Boothe says:

    Christian,Great app!!Although native installer is required, will Air’s application updater be available post install?