12

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!

Pete Hodgson
  • 15,644
  • 5
  • 38
  • 46
  • 1
    If it would save a lot of effort to put your logic in AS3, it might be worth hammering out a bridge in Ruby (or whatever) between stdin/stdout and a socket connection with a Flash or AIR app. Ruby could, say, invoke an AIR app with a port as an argument, wait for a connection, and then start piping both directions. The AS3 side would work as you describe, if you pretend your Socket/XMLSocket is a pipe to stdin/out. – fenomas Jun 30 '09 at 18:50
  • Ohhh, now that's an interesting approach!. I was just reading last night about an amqp client in as3. I could maybe us rabbitMQ or similar to broker message passing between a ruby driver and an as3 swf that was pulling the code. – Pete Hodgson Jul 01 '09 at 02:06
  • I've done something reasonably similar with Java to get around AIR's inability to listen for incoming connections. I used xsocket in Java and an XMLSocket on the flash side and there wasn't much to it. On the Java side I was bridging to a TCPIP port, rather than stdout/in, but I don't suppose it's much different.. – fenomas Jul 01 '09 at 16:59
  • Dupe of http://stackoverflow.com/questions/9440435/actionscript-3-as-a-console-app it's perfectly possible, you can even import any C lib you like with redtamarin, see https://code.google.com/p/redtamarin/ it's truely awesome. – Kamek May 20 '13 at 03:59

10 Answers10

23

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.

Community
  • 1
  • 1
Peter
  • 3,998
  • 24
  • 33
8

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

Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
  • Tamarin IS the AVM2 Virtual Machine that executes compiled ActionScript opcode (ABC, or Actionscript Byte Code) within Flash Player 9+. Adobe has donated this VM to the Mozilla foundation in the hope they will integrate it with FireFox to JIT compile (Just In Time) Javascript to ABC for much faster execution within the browser. Similar to what Google are doing with Chrome/Chromium. – Luke Aug 04 '09 at 23:16
  • Tamarin is now obsolete/defunct. – Stian OK Jan 07 '16 at 22:15
2

There's no way to do this with a bare SWF right now.

However, you can publish your Flash content as an AIR app. The app can then be invoked from the command line, and you can collect the arguments from the arguments property of an InvokeEvent. The basic idea looks like this:

NativeApplication.nativeApplication.addEventListener( 
            InvokeEvent.INVOKE, onInvoke );
// ...
function onInvoke( e:InvokeEvent ) {
    var numArguments:int = e.arguments.length;
    // ...
}

Note, however, that this is essentially a one-way street. You can grab the command-line arguments, but Flash still doesn't grok the idea of stdin and stdout.

fenomas
  • 11,131
  • 2
  • 33
  • 57
2

Actually, there is a project that makes it possible. RedTamarin is a project that extends AS3 (technically, the Tamarin project which is the Adobe/Mozilla ECMAScript project) to have access to low-level libraries (ie. POSIX). In its current state it appears to be good for stuff like shell-scripting-like programs which is what it sounds like what you're looking for.

Give it a try:

http://code.google.com/p/redtamarin/

RickDT
  • 2,104
  • 1
  • 23
  • 25
2

You can interact with stdin, stdout and stderr with redtamarin

http://code.google.com/p/redtamarin/

see examples/docs here

http://code.google.com/p/redtamarin/wiki/System#stdout

http://code.google.com/p/redtamarin/wiki/System#stderr

http://code.google.com/p/redtamarin/wiki/System#stdin

there is a difference between Flash and ActionScript 3 Flash is a runtime, AS3 is a language

I don't see why AS3 would not be a good programming language for the command line and/or the server side

Now, redtamarin is just that, a runtime that allow you to run your AS3 source code on the command line.

Also, depending on your needs, you can use it in different ways

  1. to run script on the command line

    $ ./redshell myscript.as

  2. run ABC or SWF files on the command line

    $ ./redshell myscript.abc

    $ ./redshell myscript.swf

  3. run an exectuable

    $ ./myscript

When you will run an AS3 script it will be dynamically interpreted, using ASC you will be able to compile this same script to an ABC file that can also be run from the command line.

If for example you need to assemble numerous ABC files together, you can use swfmake to merge them into SWF file and the runtime will run that SWF file too from the command line.

Finally, if you need to bundle everything in one executable, you can use createprojector to take your ABC or SWF file and merge it with the runtime itself to obtain an independent executable.

Redtamarin provide native API that cover file system access, sockets, operating system info, etc.

zwetan
  • 86
  • 3
1

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

Alexander K.
  • 1,649
  • 2
  • 15
  • 21
0

You could have a look at Haxe with is very similar to AS3 and could compile NekoVM Bytecode, which could be run on the command line.

Also interesting could be HippoHX, it is a kind of framework to create desktop applications out of flash movies. (similar to AIR, but with full access to the system.)

Gama11
  • 31,714
  • 9
  • 78
  • 100
TheHippo
  • 61,720
  • 15
  • 75
  • 100
0

If you are really that inclined, you could open a local socket, and then have a helper program, running from the command-line communicate with the open SWF.

This might be a good time to learn another language. May I suggest Java?

bgw
  • 2,036
  • 1
  • 19
  • 28
0

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

Community
  • 1
  • 1
Paul Gilmore
  • 438
  • 1
  • 5
  • 14
-6

Nope--not possible. The best you can do is a standalone app (which can be made in Flash or with a Projector version of flash player, available from the Adobe website).

And why would you want to--Flash is awesome because of the great GUI capabilities. There are plenty of other programming languages that are much better suited for the command line (Python or Ruby or, god forbid, even Perl)

zenazn
  • 14,295
  • 2
  • 36
  • 26
  • 1
    thanks for the response. I added an explanation to me question as to why I wanted to use as3 on the command line. – Pete Hodgson Jun 30 '09 at 16:41
  • 1
    This answer is not entirely correct, there are ways executing a SWF via the command line *can* be accomplished. – Peter Apr 16 '12 at 21:17