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?




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.
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
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.
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
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.
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
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.
http://myserver/test/test.html works, but c:\\temp\\test.doc doesn't. Any ideas as to what I have overlooked?
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.
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>
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
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
Thanks,
CS
http://aperture.fluorinefx.com/?page_id=4
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.
cheers-
Dustin
cheers-
Dustin
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.
> 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.
I will use it on my webpage.