Mike Chambers

code = joy

Capturing Command Line Arguments in an Apollo Application

with 13 comments

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.

Written by mikechambers

May 13th, 2007 at 10:48 am

Posted in air

13 Responses to 'Capturing Command Line Arguments in an Apollo Application'

Subscribe to comments with RSS or TrackBack to 'Capturing Command Line Arguments in an Apollo Application'.

  1. Good stuff Mike. I was wondering how this would play out.

    John C. Bland II

    13 May 07 at 10:48 pm

  2. 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.

    Erki Esken

    13 May 07 at 11:11 pm

  3. great post, but mike, is there any way we can run shell command from apollo app? if so this will be very helpful.

    sujon

    14 May 07 at 2:33 am

  4. I agree with Erki, custom protocol handling would be very useful? Any chance this is in the works?

    Jeremy

    14 May 07 at 8:19 am

  5. Count me in for the Custom Protocol handler!

    CJ Peters

    15 May 07 at 9:32 am

  6. That is pretty cool.

    68cuda

    18 May 07 at 5:29 am

  7. 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

    Samuel Agesilas

    25 May 07 at 7:26 am

  8. [...] у Майка Чемберза. За дополнительной информацией [...]

  9. Great example, thanks Mike!
    But a way to run shell commands from an air-app. would be more than hot!

    cu,
    Johannes

    Johannes Boyne

    23 Jun 07 at 9:10 am

  10. “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.

    tbm

    23 Jul 07 at 2:19 am

  11. 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

    tbm

    30 Jul 07 at 1:59 am

  12. [...] Mike Chambers » Blog Archive » Capturing Command Line Arguments in an Apollo Application (tags: command line air arguments invoke howto style) [...]

  13. Here’s the javascript code to listen to invoke events

    var onInvoke = function (invokeEvent) {
    air.trace(‘on invoke called with: ‘ + invokeEvent.arguments.toString());
    if ((invokeEvent.arguments.length > 0) && invokeEvent.arguments[0] == “dev”) {
    // user specified “dev” as the first argument
    }}
    };
    air.NativeApplication.nativeApplication.addEventListener(air.InvokeEvent.INVOKE, onInvoke);

    Topper Bowers

    17 Jul 08 at 10:02 am

Leave a Reply