Bash Scripts for working with ActionScript 3 in TextMate
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.
autocompile
#!/bin/bash
# mxmlc autocompile bash script
# create by mike chambers
# http://www.mikechambers.com
#how often it check for changes in seconds
LOOP_INTERVAL=2
#make sure that a file name was passed in
if [ -z $1 ];then
echo "You must specify a file to compile"
exit 0
fi
#make sure that the file exists
if [ ! -e $1 ]; then
echo "$1 does not exist"
exit 0;
fi
#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 ]
do
#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
MODDATE=$TDATE
#compile
compile $1
fi
#sleep until we check file again
sleep $LOOP_INTERVAL
done
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.
#!/bin/bash
# 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
fi
#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
wdir="/Users/mesh/bin/astmp"
#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).
as.template
package
{
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:
- You must have the TextMate command line tools installed and in your path (TextMate > Help > Terminal Usage).
asandautocompilemust be placed within your path.- You must update the
wdirvariable in the as script to point to the path where the as.template file is stored. - Make sure to set the
asandautocompilescript to be executable : chmod 755 - The Flex SDK must be installed with the bin directory (that contains
mxmlc) added to your path.
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.







Thanks Mike , you have Mad Skillz!!!
copyleftdev
9 Oct 08 at 12:27 pm
Hi,
Welcome to TextMate. I’ve come to use it exclusively now, even ahead of Flex Builder. I’ve just been getting my head around TextMate bundle structures, so this may be a bit off, but an easier way to do the autocompilation could be to use the existing “Compile current class” action in the AS3 bundle, but bind it to cmd-S so it’s called whenever you save. The template stuff can also be done in the bundle: take a look at some of the templates in the Flex and AS3 bundles for ideas. Crucially, you can execute scripts when creating a new file from a template. This lets you do stuff like create full projects, and replace placeholders in files etc. I’ve been trying to get it to handle creation of skeleton Mate (that’s Mate the framework, not TextMate) projects, doing stuff like creating classpaths etc.
Matt
Matt Kane
9 Oct 08 at 12:44 pm
@Matt
Thanks for the suggestion. If you bind the compile to CMD-S, then how do you see any compiler errors?
On the templates, the reason I did it this way, was that it seemed like the fastest way to get started. i.e. run command, you are in textmate, in a project with the code ready to go, verses launching TextMate, creating a new project, creating a new file from a template, and then saving everything in the correct place.
mike chambers
mesh@adobe.com
mikechambers
9 Oct 08 at 12:48 pm
@Mike
You can send the output to a tooltip which will display when you save. Alternatively you could just send it to a file. My adl script sends everything to a logfile which it then opens in Console.app, though I use fcsh so I don’t use it for compilation.
I can see the value of doing the templates like that. It would be nice if there was a way of creating something like PS droplets in TM for that sort of thing.
Matt
Matt Kane
9 Oct 08 at 2:22 pm
Mike this is great stuff! TextMate (and customization like this) is the reason we have not been able to get too comfy in Flex Builder. You should consider building your own ActionScript 3 bundle, i was surprised how easy/quick it was.
Matthew
9 Oct 08 at 5:38 pm
I love TextMate. Definitely my editor of choice. But, when I use it I miss Flex Builder’s autocomplete and refactoring tools. Anyone have ideas how we could get some semblance of those in TextMate?
Troy Gilbert
10 Oct 08 at 10:46 am
Since I’m stuck in the world of windows, I’ve used e (http://www.e-texteditor.com) quite a bit for Flex editing and love it. E is basically TextMate for windows… you can run most TextMate bundles with E, although typically they need some tweaking to work under windows/cygwin. I’ve been using the MacroMates Actionscript3 and Flex bundles (from http://macromates.com/svn/Bundles/trunk/Review/Bundles/) in E without much problem. Of course, I’ve modded the heck out of them. Oh, and you’ll also need to know about this: http://www.e-texteditor.com/forum/viewtopic.php?t=2650&postdays=0&postorder=asc&start=15 to make the ActionScript3 bundle work.
Scott Bachman
14 Oct 08 at 7:00 am
[...] Flashalisious en Mike Chambers weer een [...]
TextMate met AS3 en Flex Bundle … at PePo’s weblog
14 Oct 08 at 11:40 am
[...] Bash Scripts for working with ActionScript 3 in TextMate at Mike Chambers - [...]
Bookmarks for October 29th from 15:47 to 18:02 | For My Own Amusement
29 Oct 08 at 1:57 am
Nice. Glad to see others using Textmate and AS3 too! It’s been my setup for a while now. I’ve also just switched to fsch, and I highly recommend it over mxmlc. I’m working on a large project that takes over 8 seconds to build with mxmlc, but it recompiles in 1 with small changes in fsch now. Definitely worth it.
Also, for those wanting class auto-completion, this will be amazing:
http://blog.simongregory.com/09/as3-autocompletion-in-textmate/
Can’t wait until Simon finishes it.
Joseph Magnani
18 Nov 08 at 4:21 pm