Read flashlog.txt using AIR2 and NativeProcess

I’m sure you know by now that the Flash Platform is a great choice for making great front end applications to databases, or web services.  However, did you know with the release of AIR2 you can now use Flash or Flex to make gui front ends for existing programs or processes on your machine?  That’s right, AIR2 brings us a new feature called NativeProcess which let’s you communicate with existing programs or processes running on your machine.

Native Processes: Launch and communicate with native “out-of-band” processes. Bundle your own native executables, or call executables that you know are already on the machine. This feature requires that your application be installed with a native installer rather than though a .AIR file (we provide tools for building native installers).

  • Types of installers:
    • OS X: DMG
    • Windows: EXE
    • Linux: Debian and Red Hat Package Manager

See Christian Cantrell’s blog for the rest of the AIR2 new feature list.

I really enjoy building small apps for myself that are either fun or help with workflow.  Recently I was on an HTML5 project that required me to upgrade all my browsers to the absolute latest versions.  Well when I upgraded Firefox to 3.6, I could no longer use my Flash Tracer add-on, which caused a big workflow issue with my current Flex project.  While I know there are plenty of debug alternatives to Flash Tracer, I thought this would be a perfect chance to take AIR2’s NativeProcess out for a spin.  It also gave me a simple demo app for my talk at 360Flex about AIR.

So here’s what the simple logger app looks like.

AIR2's NativeProcess allowed me to build a simple flashlog.txt readerWith AIR2’s NativeProcess, you can build all sorts of cool new AIR apps like a flashlog.txt reader

Now that you’ve been blown away by my design skills, let’s walk through what, how, and where to get this app.

WHAT

AIR2, cross platform ( mac, win ), flashlog.txt reader.

HOW

Cross Platform – to take advantage of NativeProcess, your application must be installed as a native installer.  After you build your application, when you export the release build you select native installer instead of .air file.  When building AIR apps that you want to export as a native installer, you must build your application on the correct platform.  That is, I can’t build the .exe of this application on OS X, only the .dmg installer.

If using NativeProcess, export native installer instead of .AIR

FlashBuilder option for native installer is in export release dialog

Since this app is so small I had no problem storing the OS X and Windows specific code in the same place.  When the app is run I use flash.system.Capabilities.os to determine Windows or MAC.  I know the path to flashlog.txt is now hard coded into Flash Player, but I still have to build the platform appropriate path to flashlog.txt.

MAC – /Users/{username}/Library/Preferences/Macromedia/Flash Player/Logs/flashlog.txt

WIN – C:Users{username}AppDataRoamingMacromediaFlash PlayerLogsflashlog.txt

To grab the current username I parsed the results of File.userDirectory.url.  The last piece to setup is the path to the executable used by NativeProcess.  On OS X I’m using tail, which lives at /usr/bin/tail.  For Windows7, Powershell should live at C:WindowsSystem32WindowsPowerShellv1.0powershell.exe.

TIP : On OS X, you can use the “which” command to locate a program ( EX : which tail ).  On Windows, use the “where” command to locate a program ( EX : where powershell.exe ).  You will want to know this if the paths in this app doesn’t work for you.

Read flashlog.txt – to read flashlog.txt on OS X I’m using NativeProcess + tail.  On windows I’m using NativeProcess + Powershell’s Get-Content cmdlet.  Now that we know platform and paths, it’s time to setup our NativeProcessStartupInfo which will contain the executable we’re going to use, plus any required arguments.  In order to setup your NativeProcessStartupInfo object, you will need to know how to use the nativeprocess via command line first.  Using OS X’s tail as the sample, you can read the contents of flashlog.txt by running this in Terminal.

tail -f /Users/{username}/Library/Preferences/Macromedia/Flash Player/Logs/flashlog.txt
tail -f shows you the contents of a file as it changes

I can run tail in Terminal, or in an AIR application using NativeProcess

Here is the equivalent AS3 setup for running the same command via NativeProcess.  The executable is set to a File which points at tail.  The arguments required by tail are stored in a Vector<String>.

[as3]
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
nativeProcessStartupInfo.executable = new File(&quot;/usr/bin/tail&quot;);

var processArgs:Vector.&lt;String&gt; processArgs = new Vector.&lt;String&gt;();
processArgs.push(&quot;-f&quot;);
processArgs.push( &quot;/Users/&quot;+user+&quot;/Library/Preferences/Macromedia/Flash Player/Logs/flashlog.txt&quot; );

nativeProcessStartupInfo.arguments = processArgs;
[/as3]

Now that the startup info is set, it’s time to setup our NativeProcess listeners, then start the process.

[as3]
var process:NativeProcess = new NativeProcess();

// add listeners
process.addEventListener(NativeProcessExitEvent.EXIT, onNativeProcessExit );
process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onStandardOutput );
process.addEventListener(ProgressEvent.STANDARD_INPUT_PROGRESS, onStandardInput );
process.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR, onIOError );
process.addEventListener(IOErrorEvent.IO_ERROR, onIOError );

process.start(nativeProcessStartupInfo);
[/as3]

And that’s all there is to it, the important bits of reading flashlog.txt using AIR2’s NativeProcess.  NativeProcess has me excited because it opens a whole new world of apps that can be built.

I wrote this app for personal use and my 360Flex talk.  If you have interest in seeing more AIR2 samples, have a look at http://www.delicious.com/ericfickes/360FlexDCLinks+air2.  I put together a bunch of links supporting my talk on HTML and AIR which should keep you busy for a while.

WHERE

Contents of AIR2BUGGER_src.zip

Click me to download the full source AIR2BUGGER

OS X DMG INSTALLER WINDOWS EXE INSTALLER