mike chambers | about

Bash Scripts for working with ActionScript 3 in TextMate

Thursday, October 9, 2008

I have switched over to using TextMate for some of my experimentations with ActionScript. I like how lightweight it is, its extensibility, command completion functionality, and ease of setting up new projects. I find it is perfect for quickly testing new code and ideas.

I have put together a couple of bash scripts, which coupled with the ActionScript 3 and Flex TextMate bundles have made working in TextMate a little easier for me.

The first script is called autocompile, which takes a class file, and compiles it anytime the file changes. This is really useful in TextMate as you can see any compile errors as you code.



# mxmlc autocompile bash script
# create by mike chambers
# http://www.mikechambers.com

#how often it check for changes in seconds

#make sure that a file name was passed in
if [ -z $1 ];then
        echo "You must specify a file to compile"
        exit 0

#make sure that the file exists
if [ ! -e $1 ]; then
        echo "$1 does not exist"
        exit 0;

#function to call mxmlc
function compile()
        mxmlc $1

#compile on load
compile $1

#get and store the last modified date of the file
MODDATE=`stat -f %m $1`

#loop every 2 seconds
while [ true ]
        #get the modified date
        TDATE=`stat -f %m $1`

        #check to see if it has changed
        if [ "$TDATE" != "$MODDATE" ]; then
                #file changed. Store new date

                compile $1

        #sleep until we check file again
        sleep $LOOP_INTERVAL

Using the script is easy, just pass in the name of the file you want it to compile:

autocompile Foo.as

The next script is called as. Basically, it takes the name of an ActionScript class file, generates the file from a template, opens it within a new project in TextMate, and automatically compiles the file when it is changed. This makes it very simple to setup a new project and start coding.


# script for creating and setting up ActionScript projects in
# TextMate
# create by mike chambers
# http://www.mikechambers.com

#make sure that a file name was passed in
if [ -z $1 ];then
        echo "You must specify a Class file name"
        exit 0

#get the class name (remove .as)
classname=`echo $1 | cut -d'.' -f1`

#dir that contains the as.template file
#make sure to set this
#there should be a way to get this automatically

#write the contents of the template to the class file.
cat $wdir/as.template | sed s/CLASSNAME/$classname/ > $1

#open the current directory and file in textmate
mate .

#begin to autocompile it
autocompile $1

This requires that the following as.template file is in the directory referenced in the script (in the wdir variable).


        import flash.display.Sprite;
        public class CLASSNAME extends Sprite
                public function CLASSNAME()

In order to create the new project, create a new directory, cd to it and run the as command, passing in the name of the ActionScript class file you want it to create.

mkdir Foo
cd Foo
as Foo.as

This will create a new File.as file that contains the stub code for the Foo class. It will then open the Foo.as file in a new project in TextMate, and will compile the class anytime it changes (printing any compile errors and warnings to the terminal).

Note that I could automate the directory creation also, although I decided not to do that right now.

Couple of notes:

I also have the ActionScript 3 and Flex bundles installed within TextMate. I setup my mm.cfg so that it writes trace statements and warnings to the flashlog.txt file, and then I open that file in the Console so I can view runtime debug info (you can also just tail -f the file in terminal). (You can open the console from the ActionScript 3 Bundle in TextMate : Bundles > ActionScript 3 > Debug > Open Flash Log in Console)

Finally, I modify the Run command for the file in TextMate to open the SWF in the standalone debug Flash player (which I have set to be the default handler for the swf file type). In order to do this, I just modified the ActionScript 3 Bundle Run command (CMD-R) to execute:

name=`echo $TM_FILEPATH | cut -d'.' -f1`
open $name.swf

So, now as I type, I can see the errors as I make them, and can just hit CMD-R to test the SWF and see the runtime debug info in the console.

One additional thing I might do is to modify the autocompile script to play a sound when the compile fails.

Post any questions, suggestions or errors in the comments.

twitter github flickr behance