Select and access local SWF files within an AIR application

Well, it took me a little time to figure out how to select and load a local SWF file into an SWFLoader instance. Several times I got this error message:

SecurityError: Error #3015: Loader.loadBytes() is not permitted to load content with executable code.

After some research I found this “dreaming in flash” blog entry which basically contains the solution I was looking for.

But then it took me a little more time to figure out how to use this with the SWFLoader. So here is a basic code example:

import flash.display.Loader;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.system.LoaderContext;
import flash.utils.ByteArray;
import mx.controls.SWFLoader;
 
// File reference
var swfFile: File;
 
// Open the SWF file
var fileStream: FileStream = new FileStream();
	fileStream.open( swfFile, FileMode.READ );
 
// Read SWF bytes into byte array and close file
var swfBytes: ByteArray = new ByteArray();
	fileStream.readBytes( swfBytes );
	fileStream.close();
 
// Prepare the loader context to avoid security error
var loaderContext: LoaderContext = new LoaderContext();
	loaderContext.allowLoadBytesCodeExecution = true; // that's it!
 
// Now you could use this with a Loader instance
var loader: Loader = new Loader();
	loader.loadBytes( swfBytes, loaderContext );
 
// Or you could use this with a SWFLoader instance
var swfLoader: SWFLoader = new SWFLoader();
	swfLoader.loaderContext = loaderContext;
	swfLoader.source = swfBytes;

Microsoft Word error during HTML import

Since I have to use Microsoft Word 2008 for Mac I figured out some issue: During opening a clean XHTML file with external CSS declarations Word can find these CSS files only in the same directory. I tried serveral ways to declare the href tag, but only the last one works…

<link href="../style.css" rel="stylesheet" type="text/css" />
<link href="./../style.css" rel="stylesheet" type="text/css" />
<link href="..\style.css" rel="stylesheet" type="text/css" />
<link href="style.css" rel="stylesheet" type="text/css" />

Maybe there is a solution? I can’t figure it out.

Adobe technology platform RIA guide

This is simply the link list contained in the recently released Adobe technology platform ActionScript Reference PDF.

API References

Documentation

Developer Centers

Downloads

Miscellaneous Resources

Mailing Lists

Forums

Write with AIR applications to the application resource directory

AIR applications are not permitted to write to the application resource directory. This seems to be a security feature and makes sense, because some systems would not allow to write into this directory at system level.

But for testing purposes you might hack this by using a single line of code:

// Writing into that file would fail
var yourFileInAppDir: File = File.applicationDirectory.resolvePath( "yourFile.xml" );
// This is the simple hack:
yourFileInAppDir = new File( yourFileInAppDir.nativePath );

But this only avoids a AIR security error! You must be sure to have write access to the directory!

Using Sprite.buttonMode and Sprite.mouseChildren

Today I found something I really did not understand very well. I had to debug a application because one button displayed the handCursor and another did not. Both use the same script and actually I didn’t find out why the behaviour of this buttons was like that but I fixed it through a workaround.

The first thing I understood today is, that it’s possible to receive all mouse events without using buttonMode! Simply add a listener to your objects and you can work with them.

But if you like to work with a button that contains additional sprites (which might be very useful) you definitely should take care about their behaviour. By default mouse events will be dispatched by the lowest sprite, exept you set mouseChildren to false…

Have a look at this little code example:

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
 
	public class ButtonModeTest extends Sprite
	{
		public function ButtonModeTest()
		{
			run();
		}
 
		private function run(): void
		{
			//-- Start listening
			addEventListener( MouseEvent.CLICK, onMouseEvent );
			addEventListener( MouseEvent.MOUSE_OVER, onMouseEvent );
			addEventListener( MouseEvent.MOUSE_OUT, onMouseEvent );
 
			//-- Button 1 on the main stage
			var button1: Sprite = createButton( 1 );
			addChild( button1 );
 
			//-- Button 2 within the container
			var button2: Sprite = createButton( 2 );
			var button2Container: Sprite = new Sprite();
			button2Container.addChild( button2 );
			button2Container.x = 150;
			button2Container.name = "button2Container";
			addChild( button2Container );
 
			// By default the mouse events will be dispatched by the buttons themself
			// (the lowest sprites)
 
			// Setting containers buttonMode to true doesn't force the container to
			// dispatch the mouse events ( so what the hell is button mode?)
			button2Container.buttonMode = true;
 
			// Setting containers useHandCursor to true only works when it's buttonMode
			// is also true. But the events will still be dispatched from the lowest
			// sprite (even its a button!?!)
			button2Container.useHandCursor = true;
 
			// Only setting mouseChildren to false forces the container to dispatch
			// the mouse events
			button2Container.mouseChildren = false;
		}
 
		private function createButton( index: uint ): Sprite
		{
			var button: Sprite = new Sprite();
			button.name = "button" + index; 
			button.graphics.beginFill( 0, 1 );
			button.graphics.drawRect( 0, 0, 100, 100 );
			button.graphics.endFill();
 
			return button;
		}
 
		private function onMouseEvent( event: Event ): void
		{
			trace( "Event (" + event.type + ") dispatched from : " + event.target.name );
		}
	}
}