Using AIR to launch other applications

Lets say you want to open an MS Word file from your web page. You can just link to it using a normal HREF. Browser settings and the operating system will take care of the rest. If it's a PDF, Reader will open. Word will launch for a .doc file. Excel for an excel sheet. ( Or perhaps your browser independent equivalents ).

How do you give the same functionality to an AIR Application? This exact question came up on the Flex Coders list recently and the general concensus was that it could not be done. That surprised me, because I figured you could do something very similiar to what you would do in a web page and let the operating system handle it. This would (I assume) not violate ay security protocols inside AIR. Am I right?

I played around with it and found one solution to the problem. It's far from perfect, though. I accomplished this using the NavigateToURL functionality. The code is really simple, just a button to open the file:


<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script><![CDATA[
import flash.net.URLRequest;
        
public function clickButton():void{
var request : URLRequest = new URLRequest('C:\\projects\\test.doc');
navigateToURL(request )    
}
]]>
</mx:Script>
    
<mx:Button click="clickButton()" />
</mx:WindowedApplication>

Click the button, it creates a URL Request and then uses navigateToURL to launch it. In my tests, this will launch a browser instance outside of AIR; and that browser instance will deal with the file, launching word, or a text editor, or whatever depending upon the file.

I can understand how, in some situations this would not be ideal. Perhaps in AIR next we need a navigateToApplication?

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Jun Heider's Gravatar Right on Jeffry! That's a damn creative way to work around the AIR limitation. "_
# Posted By Jun Heider | 4/22/08 12:07 PM
Doug McCune's Gravatar Just to note, there is no way to know what this will do on other users' machines. Just because one certain file type is launched properly by your testing browser doesn't mean that it will work for someone else. You are relying on the configuration of your browser on your system.
# Posted By Doug McCune | 4/22/08 12:17 PM
Jeffry Houser's Gravatar Doug,

Agreed!

Not only does this rely on the browser configuration, it also relies on the machine configuration. However, I don't think there are any more (or less) considerations for this approach than when you link to a document in a web page.

It is hit or miss unless you have control over the client configurations / installed browser, such as machines on a corporate Intranet.
# Posted By Jeffry Houser | 4/22/08 2:01 PM
Sidney de Koning's Gravatar HI Jeffry,

First off, enjoy reading your blog, very refreshing. Now for the comment;
What if you are on a Mac and not a Windows machine? What i would do i build some way of checking on what platform you are. (You can do this on multiple things, the system tray icon for example only lives on a windows machine, and i think there is also a OS string in the System.Capabilities class).
Then i would default to a specific directory with the File api; like so;

var appDir:File    = File.applicationDirectory;
var appStoreDir:File= File.applicationStorageDirectory;
var desktopDir:File = File.desktopDirectory;
var docDir:File    = File.documentsDirectory;
var userDir:File    = File.userDirectory;
var rootDirArr:Array = File.getRootDirectories();

Then try looking for the file.
To get back to your post; it is a very clever way to let the os sort these things for itself, dont bother AIR with this. Nice one mate!

Cheers,

Sidney de Koning
# Posted By Sidney de Koning | 4/23/08 4:24 AM
Jeffry Houser's Gravatar Sidney,

Thanks for reading. Although, honestly, I'm not sure why platform is an issue related to this. If you have an AIR hook to the file you can use a property (nativePath off the top of my head), with navigateToURL

In theory, an AIR application should not do anything to tie it into one OS or another.
# Posted By Jeffry Houser | 4/23/08 11:02 AM
Mrinal Wadhwa's Gravatar Hi Jeffery,

This is not a viable solution ... although it may have worked on your machine .. there are several things that could happen .. the most likely one is that the user will see a bunch of popups saying he is trying to execute an application form the internet .. in IE on windows he'll most probably see an open/save dialog .. you did not see it probably because you might have the office plugins enabled in your browser but this need not be the setting on the users machine.

Mrinal
# Posted By Mrinal Wadhwa | 4/23/08 3:06 PM
Jeffry Houser's Gravatar Mrinal,

I'm unclear why the user would see pop-ups about trying to access an application from the Internet; can you expand on that? First off, I'm assuming the browser is dealing with the file type based on it's settings. Second, it's not accessing a file over the Internet, it's accessing a local file.

I think the viability of this approach depends on many factors. In my machine I got an "open save" dialog w/ Firefox. because that is how my machine is configured to deal with such files. I considered that as "Working" and a successful test. I'm not sure what you think happened.

As I state in my post and the comments here, this is very contingent upon the user's browser and OS settings.
# Posted By Jeffry Houser | 4/23/08 10:39 PM
Mrinal Wadhwa's Gravatar Yes, i believe the path would get translated to a file:// url and the browser will do what ever it does when you say put a file url in the address bar .. in case of an .exe it will say be warned you are trying run an exe from the internet (I'm not on a windows box so can't confirm this but this is my best guess)

Now i don't consider even an open save dialog as working because in case of a desktop application the user in most cases should not see an open save dialog because ...for example lets say i build a presentation app like powerpoint .. now this app should be able to launch other applications like powerpoint does .. without showing any security warning or an open save dialog .. there's no way to do this with AIR
_
Mrinal
# Posted By Mrinal Wadhwa | 4/23/08 11:01 PM
Jeffry Houser's Gravatar Mrinal,

If someone used this to try to launch an .exe using this method, I can envision a warning pop-up.

When I said I considered this approach working, I meant that what I expected to happen did happen. The link was launched, and the browser handled it based on my browser setting. I'm sorry this approach won't work for whatever application you're building.
# Posted By Jeffry Houser | 4/24/08 9:51 AM
Quantium's Gravatar Consider All. You can call a *.bat file writed by yourself. I'm go to try this
# Posted By Quantium | 4/30/08 3:00 PM
Flüge Australien's Gravatar Quantiums´ idea is the best. It´s quintessence of the outcome od this discussion. Nice work, guys!
# Posted By Flüge Australien | 8/26/08 10:26 AM
Maggy's Gravatar I too thought of using navigateToURL() to launch other applications from an AIR app, but the app I've built is cross-platform, and when trying to achieve the same functionality in a Mac environment, it doesn't treat the file's native path in the same direct way at all; it tacks on additional folders to the path.
# Posted By Maggy | 9/8/08 10:43 AM
Gary Geiser's Gravatar This code works well for me if I am referencing an address in the http: scope, but throws a 2148 error if I try to apply it to the file: scope. In other words, the string -
http://myserver/test/test.html works, but c:\\temp\\test.doc doesn't. Any ideas as to what I have overlooked?
# Posted By Gary Geiser | 9/11/08 5:09 PM
Jeffry Houser's Gravatar Gary,

Can you expand on "doesn't work"? Do you see an error? What error? Or does the command just get ignored?

Browser settings could be a factor depending what you want to do.
# Posted By Jeffry Houser | 9/11/08 7:42 PM
Gary Geiser's Gravatar .... as stated above, 2148 error is encountered. "Only local-with-filesystem and trusted local SWF files may access local resources." I have seen this before (don't remember the fix) but it had nothing to do with security.
# Posted By Gary Geiser | 9/12/08 3:49 PM
JY's Gravatar On a mac, try :

var request : URLRequest = new URLRequest('file:///Users/jamesyoung/Desktop/test.webloc'');

test.webloc :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd&quo...;
<plist version="1.0">
<dict>
   <key>URL</key>
   <string>http://www.jeffryhouser.com/index.cfm/2008/4/22/Us...;
</dict>
</plist>
# Posted By JY | 9/18/08 8:29 PM
JY's Gravatar FYI: pointing to a .png opened up the image in Preview.
# Posted By JY | 9/18/08 8:30 PM
CS's Gravatar Kudos, It was nice to see launching an application from Flex-AIR. Could we get a handle
to the application instance (just like in java native access, EnumWindows() psapi.dll ). Is there
a way to see what applications are running (accessing TaskBar).

CS
# Posted By CS | 9/29/08 1:44 AM
Jeffry Houser's Gravatar CS,

I do not believe there is a way in AIR to find out that sort of information about the underlying OS. You might be able to use Merapi to do something like this.

www.merapiproject.net
# Posted By Jeffry Houser | 9/29/08 7:51 AM
CS's Gravatar Thank you jeffry for the pointer. Essentially i liked to know functionality of Adobe Connect's feature of sharing application window (not the entire desktop).

Thanks,
CS
# Posted By CS | 9/29/08 11:08 AM
Dustin's Gravatar FlourineFX Aperture framework has an example that gets OS info:
http://aperture.fluorinefx.com/?page_id=4
# Posted By Dustin | 10/1/08 8:23 PM
Jeffry Houser's Gravatar Flourine is definitely interesting.

However, I was under the impression it worked with C .DLLs [or something similar]. Is that correct?

I would have looked deeper if it somehow supported / worked with the .NET Framework.
# Posted By Jeffry Houser | 10/1/08 9:17 PM
Dustin's Gravatar yea, it does use dll's. So I guess not good for a non PC OS. But is atleast one more tool for the quiver.

cheers-
Dustin
# Posted By Dustin | 10/1/08 10:44 PM
Dustin's Gravatar btw, I was trying the batch file approach without luck. the file will always open in the browser as a text file. Anyone have luck figuring out how to get around this?

cheers-
Dustin
# Posted By Dustin | 10/1/08 10:46 PM
Sam Nicholson's Gravatar Hi,

Im writing an app which has links to external websites, is there a way that i can change focus from the app to the launched browser once the link is clicked? At the moment, it all seems to be ruinning in the background.
# Posted By Sam Nicholson | 12/7/08 2:11 PM
Gary Oviatt's Gravatar We have had the problem of running an EXE file from html pages and have solved it by using LaunchInIE (http://www.whirlywiryweb.com/). This will allow you to register specific applications that you want to allow to run from a web page. I'm now working on an AIR app that needs to launch an executable and will likely side step the issue by launching an HTML file with the navigateToURL method and let the HTML code launch the app. This isn't very elegant, and is O/S dependent, but if you have control over the user's operating environment, this is one way to get the job done. It would be nice, however, if someone had a more elegant solution.
# Posted By Gary Oviatt | 12/23/08 4:48 PM
Gummistiefel's Gravatar My experience is that arguments can only be passed via the "open" command if they refer to real files. For example, this returns an error:

> open -a MyApp /nosuchfile.txt
The file /nosuchfile.txt does not exist

But this does work:

> open -a MyApp /thisfileexists.txt

Consistent with this behavior, applications receive an "open" event from Mac OS when launched in this way, and open events are intended to refer to files.

So I think your approach will work under certain circumstances, but the approach described in the post will work in more cases.
# Posted By Gummistiefel | 5/26/09 9:23 PM
Fackel's Gravatar It is a very interesting solution to solve the problem of opening word docs.
I will use it on my webpage.
# Posted By Fackel | 6/20/09 3:47 PM
Daniel Rosenstark's Gravatar Thank you so much for this! I had just finished writing a tiny little command proxy in Ruby and was thinking "all this to open a document? This can't be necessary"... just didn't think of your solution, but at least I found it. Nice one!
# Posted By Daniel Rosenstark | 7/20/09 8:03 AM
daman's Gravatar well I want to call an exe to record sound using AIR, now for this small solution that i am developing, i could not affort FMS, and as it is desktop with no internet or relaying features i should not be using FMS, but there is no other way of recording sound, I have detected the mic and the activity of mike but i could not record any sound from it so i need to call exe which will record sound.
so how do i call it
# Posted By daman | 8/12/09 5:27 AM
Jeffry Houser's Gravatar Daman,

In sort, you can't call an exe from AIR. I would recommend looking into a tool such as Merapi to call Java Libraries to do your sound recording. WebORB.NET is another similar solution.

In either case, it will be a nightmare to create an installer for this.

Since AIR has access to the local file system, it may be possible to write your own libraries to record the sound and then convert them to files; mp3 or wav or whatever. It is not something I've researched, though.

Alternatively you could look into technologies other than AIR for your application.
# Posted By Jeffry Houser | 8/12/09 10:35 AM
activetraffic.de's Gravatar ....cheers, that was exactly the solution I´ve been looking for, for AGES!!!!

Thank you very much, I started to get serious "headaces" on this topic!
# Posted By activetraffic.de | 8/18/09 8:03 AM
flex's Gravatar can we open air application from flex?
# Posted By flex | 9/7/09 8:44 AM
Jeffry Houser's Gravatar In theory you can use this method to launch an AIR app from a Flex app. That assumes the browser knows what to do with the '.air' file.

You can also look into badge installs of your AIR app, although I believe that gives you limited control.
# Posted By Jeffry Houser | 9/7/09 9:10 AM
Mike's Gravatar I too thought of using navigateToURL() to launch other applications from an AIR app, but the app I've built is cross-platform, and when trying to achieve the same functionality in a Mac environment, it doesn't treat the file's native path in the same direct way at all; it tacks on additional folders to the path.
# Posted By Mike | 10/6/09 2:23 PM
Suchmaschinenoptimierung Ostheimer's Gravatar I searched for "open MS Word file web page" and found this interesting article. Didn't help me but seemed to be helpful back then.
Thanks,
andreas
# Posted By Suchmaschinenoptimierung Ostheimer | 2/24/10 8:01 PM
All Content Copyright 2005, 2006, 2007, 2008, 2009 Jeffry Houser. May not be reused without permission
BlogCFC was created by Raymond Camden. This blog is running version 5.9.2.002.