Capturing Command Line Arguments in an Apollo Application
mikechambers May 13th, 2007
Here is a simple example that shows how to capture command line arguments passed to a Flex based Apollo application.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete="onCreationComplete()">
<mx:Script>
<![CDATA[
private function onCreationComplete():void
{
//register for the Invoke Event, called whenever
//the app is launched or called from the command line
Shell.shell.addEventListener(InvokeEvent.INVOKE, onInvoke);
}
private function onInvoke(event:InvokeEvent):void
{
//arguments passed to app are stored as array in event.arguments
outputField.text += "Invoke : " + event.arguments + "\n";
}
]]>
</mx:Script>
<mx:TextArea right=”10″ left=”10″ top=”10″ bottom=”10″ id=”outputField”/>
</mx:Application>
Basically, an InvokeEvent is broadcast whenever the app is launched, either by clicking the icon, or calling it from the command line. Any arguments passed on the command line will be contained as an array of strings in event.arguments.
Note, in Flex Builder 2.0.1, there is no way to test passing command line arguments (there will be in the next version of Flex Builder). You have to use adl to launch the application, passing arguments to it like so:
adl InvokeExample-app.xml -- foo bar "bim bam"
Basically, everything after “–” will be passed to the application as command line arguments.
Another thing to remember, is that you can only have one instance of an Apollo application running at a time. If you need to have multiple instances of the application running, then listen for the InvokeEvent, and when it is fired, you can open a new NativeWindow with you main application UI in it. This may require you to rethink a little how your application is structured, but will essentially allow you to present multiple app UIs to the user. Ill post more on this later.
Finally, you can also listen for InvokeEvents from an HTML based application. I had hoped to post an example here, but ran into what looks like a bug. Ill post more info in another post once I figure out what is going on.
You can find more information on this in the docs.






Good stuff Mike. I was wondering how this would play out.
This is good, but will Apollo support some kind of custom protocol handling as well? So we could use something like apollo://apollo-app-id-here/someaction?arg1=val1&arg2=val2
Could be very useful for integrating Apollo apps with regular browser based apps.
great post, but mike, is there any way we can run shell command from apollo app? if so this will be very helpful.
I agree with Erki, custom protocol handling would be very useful? Any chance this is in the works?
Count me in for the Custom Protocol handler!
That is pretty cool.
Thanks for posting this mike! I’ve been wondering about the command line argument issue with some of my apollo stuff where I’d definetely need to pass command line arguments. I also agree with Erki that a custome protocol would be hot!!! Keep it rocking guys.
Cheers,
Sam
[...] у Майка Чемберза. За дополнительной информацией [...]
Great example, thanks Mike!
But a way to run shell commands from an air-app. would be more than hot!
cu,
Johannes
“great post, but mike, is there any way we can run shell command from apollo app? if so this will be very helpful.”
“Great example, thanks Mike!
But a way to run shell commands from an air-app. would be more than hot!”
hear hear … is this doable…?… very keen to know.
i made a temorary bridge with applescript folder actions for getting your command lines out of apollo and executing..
how to do it here..
http://thatblokemike.com/blog/index.php?mode=viewid&post_id=79
[...] Mike Chambers » Blog Archive » Capturing Command Line Arguments in an Apollo Application (tags: command line air arguments invoke howto style) [...]