<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mike Chambers &#187; bash</title>
	<atom:link href="http://www.mikechambers.com/blog/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mikechambers.com/blog</link>
	<description>code = joy</description>
	<lastBuildDate>Sun, 15 Jan 2012 05:46:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Bash Scripts for working with ActionScript 3 in TextMate</title>
		<link>http://www.mikechambers.com/blog/2008/10/09/bash-scripts-for-working-with-actionscript-3-in-textmate/</link>
		<comments>http://www.mikechambers.com/blog/2008/10/09/bash-scripts-for-working-with-actionscript-3-in-textmate/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 18:41:01 +0000</pubDate>
		<dc:creator>mikechambers</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[textmate]]></category>

		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1566</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.mikechambers.com%2Fblog%2F2008%2F10%2F09%2Fbash-scripts-for-working-with-actionscript-3-in-textmate%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif&amp;source=mesh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I have switched over to using <a href="http://macromates.com/">TextMate</a> 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.</p>
<p>I have put together a couple of bash scripts, which coupled with the <a href="http://flashalisious.com/2007/07/30/installing-as3-and-flex-bundle-for-textmate/">ActionScript 3 and Flex TextMate bundles</a> have made working in TextMate a little easier for me.<br />
<span id="more-1566"></span><br />
The first script is called <code>autocompile</code>, 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.</p>
<p><strong>autocompile</strong></p>
<div class="highlight">
<pre><span style="color: #408080; font-style: italic">#!/bin/bash</span>

<span style="color: #408080; font-style: italic"># mxmlc autocompile bash script</span>
<span style="color: #408080; font-style: italic"># create by mike chambers</span>
<span style="color: #408080; font-style: italic"># http://www.mikechambers.com</span>

<span style="color: #408080; font-style: italic">#how often it check for changes in seconds</span>
<span style="color: #19177C">LOOP_INTERVAL</span><span style="color: #666666">=</span>2

<span style="color: #408080; font-style: italic">#make sure that a file name was passed in</span>
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #666666">[</span> -z <span style="color: #19177C">$1</span> <span style="color: #666666">]</span>;then
        <span style="color: #008000">echo</span> <span style="color: #BA2121">&quot;You must specify a file to compile&quot;</span>
        <span style="color: #008000">exit </span>0
<span style="color: #008000; font-weight: bold">fi</span>

<span style="color: #408080; font-style: italic">#make sure that the file exists</span>
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #666666">[</span> ! -e <span style="color: #19177C">$1</span> <span style="color: #666666">]</span>; <span style="color: #008000; font-weight: bold">then</span>
<span style="color: #008000; font-weight: bold">        </span><span style="color: #008000">echo</span> <span style="color: #BA2121">&quot;$1 does not exist&quot;</span>
        <span style="color: #008000">exit </span>0;
<span style="color: #008000; font-weight: bold">fi</span>

<span style="color: #408080; font-style: italic">#function to call mxmlc</span>
<span style="color: #008000; font-weight: bold">function </span>compile<span style="color: #666666">()</span>
<span style="color: #666666">{</span>
        mxmlc <span style="color: #19177C">$1</span>
<span style="color: #666666">}</span>

<span style="color: #408080; font-style: italic">#compile on load</span>
compile <span style="color: #19177C">$1</span>

<span style="color: #408080; font-style: italic">#get and store the last modified date of the file</span>
<span style="color: #19177C">MODDATE</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span>stat -f %m <span style="color: #19177C">$1</span><span style="color: #BA2121">`</span>

<span style="color: #408080; font-style: italic">#loop every 2 seconds</span>
<span style="color: #008000; font-weight: bold">while</span> <span style="color: #666666">[</span> <span style="color: #008000">true</span> <span style="color: #666666">]</span>
<span style="color: #008000; font-weight: bold">do</span>
        <span style="color: #408080; font-style: italic">#get the modified date</span>
        <span style="color: #19177C">TDATE</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span>stat -f %m <span style="color: #19177C">$1</span><span style="color: #BA2121">`</span>

        <span style="color: #408080; font-style: italic">#check to see if it has changed</span>
        <span style="color: #008000; font-weight: bold">if</span> <span style="color: #666666">[</span> <span style="color: #BA2121">&quot;$TDATE&quot;</span> !<span style="color: #666666">=</span> <span style="color: #BA2121">&quot;$MODDATE&quot;</span> <span style="color: #666666">]</span>; <span style="color: #008000; font-weight: bold">then</span>
                <span style="color: #408080; font-style: italic">#file changed. Store new date</span>
                <span style="color: #19177C">MODDATE</span><span style="color: #666666">=</span><span style="color: #19177C">$TDATE</span>

                <span style="color: #408080; font-style: italic">#compile</span>
                compile <span style="color: #19177C">$1</span>
        <span style="color: #008000; font-weight: bold">fi</span>

        <span style="color: #408080; font-style: italic">#sleep until we check file again</span>
        sleep <span style="color: #19177C">$LOOP_INTERVAL</span>
<span style="color: #008000; font-weight: bold">done</span>
</pre>
</div>
<p>&nbsp;</p>
<p>Using the script is easy, just pass in the name of the file you want it to compile:</p>
<p><code>autocompile Foo.as</code></p>
<p>The next script is called <code>as</code>. 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.</p>
<div class="highlight">
<pre><span style="color: #408080; font-style: italic">#!/bin/bash</span>

<span style="color: #408080; font-style: italic"># script for creating and setting up ActionScript projects in</span>
<span style="color: #408080; font-style: italic"># TextMate</span>
<span style="color: #408080; font-style: italic"># create by mike chambers</span>
<span style="color: #408080; font-style: italic"># http://www.mikechambers.com</span>

<span style="color: #408080; font-style: italic">#make sure that a file name was passed in</span>
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #666666">[</span> -z <span style="color: #19177C">$1</span> <span style="color: #666666">]</span>;then
        <span style="color: #008000">echo</span> <span style="color: #BA2121">&quot;You must specify a Class file name&quot;</span>
        <span style="color: #008000">exit </span>0
<span style="color: #008000; font-weight: bold">fi</span>

<span style="color: #408080; font-style: italic">#get the class name (remove .as)</span>
<span style="color: #19177C">classname</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span><span style="color: #008000">echo</span> <span style="color: #19177C">$1</span> | cut -d<span style="color: #BA2121">&#39;.&#39;</span> -f1<span style="color: #BA2121">`</span>

<span style="color: #408080; font-style: italic">#dir that contains the as.template file</span>
<span style="color: #408080; font-style: italic">#make sure to set this</span>
<span style="color: #408080; font-style: italic">#there should be a way to get this automatically</span>
<span style="color: #19177C">wdir</span><span style="color: #666666">=</span><span style="color: #BA2121">&quot;/Users/mesh/bin/astmp&quot;</span>

<span style="color: #408080; font-style: italic">#write the contents of the template to the class file.</span>
cat <span style="color: #19177C">$wdir</span>/as.template | sed s/CLASSNAME/<span style="color: #19177C">$classname</span>/ &gt; <span style="color: #19177C">$1</span>

<span style="color: #408080; font-style: italic">#open the current directory and file in textmate</span>
mate .

<span style="color: #408080; font-style: italic">#begin to autocompile it</span>
autocompile <span style="color: #19177C">$1</span>
</pre>
</div>
<p>&nbsp;</p>
<p>This requires that the following <em>as.template</em> file is in the directory referenced in the script (in the <code>wdir</code> variable).</p>
<p><strong>as.template</strong></p>
<div class="highlight">
<pre>package
{
        <span style="color: #008000; font-weight: bold">import</span> flash.display.Sprite<span style="color: #666666">;</span>
        <span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">class</span> CLASSNAME <span style="color: #008000; font-weight: bold">extends</span> Sprite
        {
                <span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">function</span> CLASSNAME()
                {
                }
        }
}
</pre>
</div>
<p>&nbsp;</p>
<p>In order to create the new project, create a new directory, <code>cd</code> to it and run the <code>as</code> command, passing in the name of the ActionScript class file you want it to create.</p>
<pre><code>mkdir Foo
cd Foo
as Foo.as</code></pre>
<p>&nbsp;</p>
<p>This will create a new <em>File.as</em> file that contains the stub code for the <code>Foo</code> class. It will then open the <em>Foo.as</em> file in a new project in TextMate, and will compile the class anytime it changes (printing any compile errors and warnings to the terminal).</p>
<p>Note that I could automate the directory creation also, although I decided not to do that right now.</p>
<p>Couple of notes:</p>
<ul>
<li>You must have the TextMate command line tools installed and in your path (TextMate &gt; Help &gt; Terminal Usage).</li>
<li><code>as</code> and <code>autocompile</code> must be placed within your path.</li>
<li>You must update the <code>wdir</code> variable in the as script to point to the path where the as.template file is stored.</li>
<li>Make sure to set the <code>as</code> and <code>autocompile</code> script to be executable : chmod 755</li>
<li>The Flex SDK must be installed with the <em>bin</em> directory (that contains <code>mxmlc</code>) added to your path.</li>
</ul>
<p>I also have the ActionScript 3 and Flex bundles installed within TextMate. I <a href="http://blog.flexexamples.com/2007/08/26/debugging-flex-applications-with-mmcfg-and-flashlogtxt/">setup my mm.cfg so that it writes trace statements and warnings to the flashlog.txt file</a>, and then I open that file in the Console so I can view runtime debug info (you can also just <code>tail -f</code> the file in terminal). (You can open the console from the ActionScript 3 Bundle in TextMate : Bundles &gt; ActionScript 3 &gt; Debug &gt; Open Flash Log in Console)</p>
<p>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:</p>
<div class="highlight">
<pre><span style="color: #19177C">name</span><span style="color: #666666">=</span><span style="color: #BA2121">`</span><span style="color: #008000">echo</span> <span style="color: #19177C">$TM_FILEPATH</span> | cut -d<span style="color: #BA2121">&#39;.&#39;</span> -f1<span style="color: #BA2121">`</span>
open <span style="color: #19177C">$name</span>.swf
</pre>
</div>
<p>&nbsp;</p>
<p>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.</p>
<p>One additional thing I might do is to modify the <code>autocompile</code> script to play a sound when the compile fails.</p>
<p>Post any questions, suggestions or errors in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikechambers.com/blog/2008/10/09/bash-scripts-for-working-with-actionscript-3-in-textmate/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

