Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a 'command line' swf?

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!

like image 280
Pete Hodgson Avatar asked Jun 28 '09 23:06

Pete Hodgson


2 Answers

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.

like image 82
Peter Avatar answered Oct 01 '22 11:10

Peter


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 :)

like image 27
Mark Rushakoff Avatar answered Oct 01 '22 11:10

Mark Rushakoff