小小菜鸟的web菜园子

web开发学习。好记性不如烂笔头。每天进步一点点!

导航

通过AIR执行命令行。

通过AIR执行命令行,只有一个缺点是目前必须通过FLEX IDE设置命令行参数,
谁能不同过FLEX IDE设置,直接把参数在程序里设置,请留下方法。十分感谢~

A question came up over on the Apollo Coders group about how to access command line arguments in an AIR application. I know that there are other explanations + documentation out there on this topic, but I thought I'd regurgitate what I have learned.

In Flex Bulder create an AIR application. I named mine "CommandLine", and the CommandLine.mxml looked like this:

 

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
</mx:WindowedApplication>

 

It's an empty app that does nothing, as expected when it is first created. This application will do the brilliant act of displaying the command line arguments using an alert. So, let's add a script block and import the alert.

 

<mx:Script>
<![CDATA[
import mx.controls.Alert;
            
]]>
</mx:Script>

 

Arguments are attached the invokeEvent ckass. So, at the top level (WindowedApplication), let's listen to the invoke event:

 

<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" invoke="onInvoke(event)">

 

And we'll need to write an onInvoke function inside our script block:

 

private function onInvoke(event:InvokeEvent):void{
Alert.show(event.arguments.toString());
}

 

This function is named onInvoke. It accepts the InvokeEvent as an argument. The InvokeEvent has a property named arguments. Arguments is an array. This method converts it to a string and displays it.

Run the app and you'll see an empty alert box. Let's add some arguments. In Flex Builder, if you select the play drop down and go to "run...". The menu will look something like this:

And it will bring up this configuration screen:

You'll notice the config screen has a place for command line arguments. In this situation I typed test=test, test2=test1 . So far as Flex is concerned, Command line arguments are separated by a space; so I get two command line arguments: "test=test," and "test2=test1".

A few things that you might want to be aware of:

  • The invoke event is fired every time the application is launched, either by double clicking a shortcut or by double clicking the .exe file (or whatever your preferred method of choice is for launching an application). It does not matter if the application is already loaded, the invoke event will be re-run. You may want to write logic to prevent the app from initializing the command line arguments twice.
  • I'm not sure how to add command line arguments to the AIR Application shortcuts as part of the AIR installation process. Does anyone know? In my situation, the AIR app is being launched by another application, so that application handles creating and passing the command line arguments.
  • In it's current form, there is no concept of named arguments. So, the arguments in my above example are "Test=test". I do not have an argument named "test" with a value "test". I bet you could write a parser using regex and a Dictionary to generate your named arguments. Maybe I'll experiment with this at some future time.

 

The link I saved to the original thread is here although it does not appear to be working at the time of this writing. Maybe I got the link wrong. I currently have 11 things on my list of "writing ideas" and sometimes it takes a while for this stuff to peculate.

 

来自:http://www.jeffryhouser.com/index.cfm/2008/3/20/Accessing-command-line-arguments-in-AIR

posted on 2008-05-04 17:24  『小小菜鸟』  阅读(1279)  评论(0编辑  收藏  举报