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.

12 Responses to “Capturing Command Line Arguments in an Apollo Application”

  1. John C. Bland IIon 13 May 2007 at 10:48 pm

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

  2. Erki Eskenon 13 May 2007 at 11:11 pm

    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.

  3. sujonon 14 May 2007 at 2:33 am

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

  4. Jeremyon 14 May 2007 at 8:19 am

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

  5. CJ Peterson 15 May 2007 at 9:32 am

    Count me in for the Custom Protocol handler!

  6. 68cudaon 18 May 2007 at 5:29 am

    That is pretty cool.

  7. Samuel Agesilason 25 May 2007 at 7:26 am

    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

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

  9. Johannes Boyneon 23 Jun 2007 at 9:10 am

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

    cu,
    Johannes

  10. tbmon 23 Jul 2007 at 2:19 am

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

  11. tbmon 30 Jul 2007 at 1:59 am

    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

  12. links for 2008-06-24 at Topper’s Blogon 23 Jun 2008 at 8:34 pm

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

Trackback URI | Comments RSS

Leave a Reply