Opening a file in a non-default application with Adobe AIR

On a current project I had to launch files in an application that was not Windows’ default app for opening. Specifically – I needed to open images in Photoshop or Fireworks from my AIR app, even when the default app for .png and .jpg is the Windows Photo Viewer.

As you might already know the File API has had the  file.openWithDefaultApplication() method since AIR 2.0. But this wasn’t going to cut it since I needed to open in a specific application. So I turned to the NativeProcess API which allows you to launch a native process from your AIR app (i.e run an .exe on Windows). I decided I should make a post on how to do it since I couldn’t find any good resources or examples for it.

Firstly I recommend whatching thee following two part tutorial on NativeProcesses from Lee Brimelow at GotoAndLearn…

AIR 2.0 Native Process Part 1
AIR 2.0 Native Process Part 2

… and then continue.

Once you have your project set up properly to allow the native process, you first need to create a File referance to the app you want to open. In this example we’ll use Photoshop: (note: I am using Photoshop CS5.1 on 64bit windows, so your path might be different)

1
var photoshop:File = new File("C:/Program Files/Adobe/Adobe Photoshop CS5.1 (64 Bit)/Photoshop.exe");

Next we create the arguments Vector which will hold the startup parameters – in this case the location of the image we want to open in Photoshop:

1
2
var args:Vector.<String> = new Vector.<String>();
args.push("location of an image eg C:/Pictures/image.jpg");

Then we create the NativeProcessStartUpInfo object for the NativeProcess which holds the reference to the .exe and the parameters you want to pass in:

1
2
3
4
// create the startup info object
var startupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
startupInfo.executable = photoshop;
startupInfo.arguments = args;

Finally, we instantiate a NativeProcess object and start it, passing in the startup info we created earlier:

1
2
var process:NativeProcess = new NativeProcess();
process.start(startupInfo);

And that’s it. If all goes well you should have opened the image in Photoshop.

Here is the complete code:

1
2
3
4
5
6
7
8
9
10
11
var photoshop:File = new File("C:/Program Files/Adobe/Adobe Photoshop CS5.1 (64 Bit)/Photoshop.exe");

var args:Vector.<String> = new Vector.<String>();
args.push("location of an image eg C:/Pictures/image.jpg");

var startupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
startupInfo.executable = photoshop;
startupInfo.arguments = args;

var process:NativeProcess = new NativeProcess();
process.start(startupInfo);
Posted toActionScript AIR Flash