Is it possible to create a 'command line'

2019-01-23 16:20发布

I'd like to be able to write a .swf file that is runnable as a command line app. In other words, I would be able to create actionscript classes which can interact with stdin and stdout, and could then execute that .swf directly in the command line.

I suspect that this isn't really possible. Can anyone confirm that?

EDIT: A couple of the answers pointed out that using Flash for command line work probably isn't the best choice. I wholeheartedly agree in most situations. The reason I am asking about this is because I want to do some AS3 code generation, and reflecting on AS3 classes within the runtime would be easier than parsing the code or walking the intermediary XML that asdoc produces. I'm doing the XML approach now in Ruby, but would love to have a cleaner solution!

10条回答
手持菜刀,她持情操
2楼-- · 2019-01-23 16:45

Now it is possible with AIR 2.0. Check this article to start.

查看更多
ら.Afraid
3楼-- · 2019-01-23 16:46

YES! It actually is possible.

You can create a pure AS3 AIR project (without any application window) and run from the command line using ADL (AIR Debug Launcher).

ADL will execute your SWF and will pass whatever arguments you give it directly to your application at runtime—all from the command line! To read the arguments from AS3 just add this code to your main class:

package
{
   import flash.desktop.NativeApplication;
   import flash.display.Sprite;
   import flash.events.InvokeEvent;

   public class CmdLine extends Sprite
   {
      public function CmdLine()
      {
         NativeApplication.nativeApplication.addEventListener(
            InvokeEvent.INVOKE, onInvokeEvent); 

         function onInvokeEvent(invocation:InvokeEvent):void { 
            trace(invocation.arguments); 
         } 
      }
   }
}

Your main class will still extend Sprite, but you won't see any UI unless you create NativeWindow objects. If you're using Flash Builder, just create a new AIR project and rename the extension of the main .mxml file to .as (before you finish the wizard).

Here is more about ADL: Using the AIR Debug Launcher (ADL)

Also, this will be very useful: AIR application invocation and termination

You can do all your output using trace(), write files, or even write directly to stdout, as seen here.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-23 16:50

Apparently there is the Tamarin project which aims to create an open source implementation of AS3. This page gives a little detail of compiling an AS3 script and running it from a command line.

I'm not getting a good idea of how stable Tamarin is, but it might be your best bet for now. On the other hand, I have to strongly agree with @zenazn that you would be better off long-term learning a language more designed for general purposes, but if really want to just use Actionscript, don't let anyone stop you :)

查看更多
做自己的国王
5楼-- · 2019-01-23 16:53

I had a similar question recently. It took me a few days to answer it for myself, but you can create a .swf and execute it entirely from the command line.

AS3 Filesystem Hello World

查看更多
登录 后发表回答