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.