Building AS3 Files From TextMate

July 30th, 2007

Once again, here is another actionscript post. One of the things that got really annoying to me was re-running the builder to create my swf and test the actionscript I had been working on. I would modify an as file in TextMate, then click on iTerm, hit up and enter to rerun the last command, and then view the swf in Finder. Way too much work. I have a bundle in TextMate called John’s Helpers. Inside that, I created a command called ‘Build File’, set the activation to Key Equivalent ctrl-b and the source to source.actionscript. Below is the code that does the dirty work.

TextMate AS Command

#!/usr/bin/env ruby -w

$LOAD_PATH << "#{ENV['TM_SUPPORT_PATH']}/lib"
require "web_preview"
require "open3"

html_header("Building ActionScript File")

if ENV.has_key?("AS_DOCUMENT_CLASS")
	filename = File.basename(ENV["AS_DOCUMENT_CLASS"])
	dir = File.dirname(ENV["AS_DOCUMENT_CLASS"])
else
	filename = File.basename(ENV['TM_FILEPATH'])
	dir = ENV["TM_DIRECTORY"]
end

puts "

Building #{filename} in #{dir}

"

swf = filename.gsub(".as", ".swf")
dir = dir.gsub(' ', '\ ') # fix space in path

puts "<pre>"

Open3.popen3("cd #{dir} && /Applications/flex_sdk_2/bin/mxmlc #{filename}") do |stdin, stdout, stderr|
	puts stdout.gets
	if error = stderr.gets
		puts "\n\n" + error
	else
		system("open #{File.join(dir, swf)}")
	end
end

# system("cd #{dir} && /Applications/flex_sdk_2/bin/mxmlc #{filename}")
puts "</pre>"

html_footer

Assumptions

  1. You have the free Flex SDK
  2. The Flex SDK is installed in your Applications folder (ie: /Applications/flex_sdk_2/bin/mxmlc)

Two Ways of Using

1) If you simply have a file open and you hit ctrl-b, it will create a swf of the same name in the same directory and then open it using Finder. Simple enough.

2) If you have a TextMate project saved with multiple ActionScript files, you can set a Project Specific Shell Variable named AS_DOCUMENT_CLASS equal to the full path to your main document class (ie: /Users/nunemaker/flash/carousel3/classes/Main.as). If that environment variable is set, no matter what ActionScript file you have open in your project, the main document class will be the only file built when you hit ctrl-b.

2 Responses to “Building AS3 Files From TextMate”

  1. How To Update Facebook, Twitter, and Kopete’s Status All At Once…

  2. hey,

    this one is a really great one. i like it very much. is it possible to show all total errors, not only the first one?

About This Site

Addicted to New is the personal website of John Nunemaker (Noo-neh-maker), a Web Developer enamored of Ruby on Rails and a wide-eyed fan of all things new and cool.