Advertisement
  1. Code
  2. Adobe Air

Build a Custom File Extension AIR App

Scroll to top
Read Time: 11 min

This tutorial will explain how to make your own file extensions in Adobe AIR. I'll show you how to build a small application, save the positions of a couple movieclips within it, and reload them when the application is launched.

Follow along and see if you can come up with your own uses for Custom File Extensions..


Please upgrade your Flash Player


Step 1: Setting up the Document

Open a new Flash Air document, name it "saveFile", and save it in a new folder. Then, open a new ActionScript file, give it the same name, and save it into the same folder as the newly created Flash document.

””””””

If the prompt screen doesn't appear when Flash starts, simply create a new Flash ActionScript 3 document. Save the file, then go to Commands > AIR - Application and Installer Settings. Flash will convert the file to an Air document.

””””””

In the properties panel of the Flash document, type "saveFile" into the Document class field. This will associate the new ActionScript file (our document class) with the Flash document.

””””””

Step 2: Adding the Controls

Create a black square with a height of 52, set the width to be the stage width, and align it to the bottom-left of the stage. Give the square an alpha of 33. In the components panel, drag out three buttons and place them on top of the black square.

””””””

Give one of the buttons an instance name of "open" and change its label to say "Open". The next button will have an instance name of "save" and its label will be "Save". The third buttons name will be "image" and have a label of "Image". Spread them out however you want, select all three buttons and the black square and turn them into a single movieclip that has an instance name of "footer".

””””””

Step 3: Little Circles

On the stage, create a red circle with a height and width of 50px. Convert it to a movieclip, then in the dialog box, press the "Advanced" button. Under "Linkage" check the "Export for ActionScript" box. Give it a class name of "Red" and click "OK".

Next, create a blue circle that is the same size as the red circle. Convert it to a movieclip, export it for ActionScript and give it a class name of "Blue". Delete the two circles from the stage, so that the only remaining movieclip is the footer movieclip.

””””””

Step 4: Download the Adobe JPEG Encoder

Go to http://code.google.com/p/as3corelib/ and download the as3corelib zip folder. With the JPEG encoder, we'll be able to save an image of our little cirlces.

””””””

Step 5: The Document Class Skeleton

This is the basic frame where we'll put all our code.

1
2
package {
3
	import flash.display.Sprite;
4
5
	public class saveFile extends Sprite
6
	{
7
		
8
		public function saveFile()
9
		{
10
			
11
		}
12
	}
13
}

Step 6: The Imports

Here are the import statements to make the Air application work. These will go in the file right below the package declaration and above the public class statement.

1
2
import com.adobe.images.JPGEncoder;
3
	
4
	import flash.desktop.NativeApplication;
5
	import flash.display.BitmapData;
6
	import flash.display.MovieClip;
7
	import flash.display.Sprite;
8
	import flash.events.Event;
9
	import flash.events.InvokeEvent;
10
	import flash.events.MouseEvent;
11
	import flash.filesystem.File;
12
	import flash.filesystem.FileMode;
13
	import flash.filesystem.FileStream;
14
	import flash.net.FileFilter;
15
	import flash.utils.ByteArray;

Step 7: The Variables and Set up Functions

Here are the variables that we're using to create the two little circles on the stage. The offset variables will be used later for the dragging and dropping of the circles.

I have also assigned an invoke event listener to the NativeApplication. This will fire when the application is either launched or when the custom file is clicked. The invoke function will check to see how the app was launched. If it was from a file, it will load the file. If not, it will call the init function.

1
2
public class saveFile extends Sprite
3
	{
4
		private var red:MovieClip;
5
		private var blue:MovieClip;
6
		private var currentClip:MovieClip;
7
		private var xOffset:Number;
8
		private var yOffset:Number;
9
		
10
		public function saveFile()
11
		{
12
			NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onInvoke);
13
			movieClips();
14
			listeners();
15
		}
16
		private function init():void
17
		{
18
			var sw:int = stage.stageWidth;
19
			var sh:int = stage.stageHeight-footer.height;
20
			red.x = sw * Math.random();
21
			red.y = sh * Math.random();
22
			blue.x = sw * Math.random();
23
			blue.y = sh * Math.random();
24
		}
25
		private function movieClips():void
26
		{
27
			red= new Red();
28
			blue = new Blue();
29
			this.addChildAt(red, 0);
30
			this.addChildAt(blue, 1);
31
			this.addChildAt(footer, 2);
32
		}

Step 8: The Listeners Function

This function simply sets up the event listeners for all the buttons and circles on the stage.

1
2
private function listeners():void
3
		{
4
			red.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
5
			blue.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
6
			footer.open.addEventListener(MouseEvent.CLICK, openClick);
7
			footer.save.addEventListener(MouseEvent.CLICK, saveClick);
8
			footer.image.addEventListener(MouseEvent.CLICK, imageClick);
9
		}

Step 9: Moving the Little Circles

Here we set up the functions to move the circles around the stage.

1
2
private function onDown(event:MouseEvent):void
3
		{
4
			currentClip = event.target as MovieClip;
5
			xOffset = mouseX - currentClip.x;
6
			yOffset = mouseY - currentClip.y;
7
			currentClip.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
8
			this.addEventListener(MouseEvent.MOUSE_UP, onUp, false, 0, true);
9
			this.addEventListener(MouseEvent.MOUSE_MOVE, onMove, false, 0, true);
10
		}
11
		private function onMove(event:MouseEvent):void
12
		{
13
			currentClip.x = mouseX - xOffset;
14
			currentClip.y = mouseY - yOffset;
15
			event.updateAfterEvent();
16
		}
17
		private function onUp(event:MouseEvent):void
18
		{
19
			this.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
20
			this.removeEventListener(MouseEvent.MOUSE_UP, onUp);
21
			currentClip.addEventListener(MouseEvent.MOUSE_DOWN, onDown, false, 0, true);
22
		}

Step 10: Saving the Image

When the "Image" button is clicked, it will call the "imageClick" function. This function opens up a dialog save box and you can give your image any name you want. When the user names the image, it will call the "imageSave" function. Inside that function, we use the JPGEncoder class to create the image. The Air app then saves the image and listens for the "onClose" function. That function simply reassigns the little circles to the stage from the temp sprite that was created.

1
2
private function imageClick(event:MouseEvent):void
3
		{
4
			var file:File = File.desktopDirectory;
5
			file.browseForSave("Save Image");
6
			file.addEventListener(Event.SELECT, imageSave);
7
		}
8
		private function imageSave(event:Event):void
9
		{
10
			var temp:Sprite = new Sprite();
11
			var len:int = this.numChildren;
12
			temp.addChild(red);
13
			temp.addChild(blue);
14
			var bitmapData:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
15
			bitmapData.draw(temp);
16
			var jpg:JPGEncoder = new JPGEncoder(100);
17
			var byteArray:ByteArray = jpg.encode(bitmapData);
18
			var saveFile:File = File(event.target);
19
			var directory:String = saveFile.url;
20
			if(directory.indexOf(".jpg") == -1)
21
			{
22
				directory += ".jpg";
23
			}
24
			var file:File = new File();
25
			file = file.resolvePath(directory);
26
			var fileStream:FileStream = new FileStream();
27
			fileStream.addEventListener(Event.CLOSE, onClose);
28
			fileStream.openAsync(file, FileMode.WRITE);
29
			fileStream.writeBytes(byteArray);
30
			fileStream.close();
31
		}
32
		private function onClose(event:Event):void
33
		{
34
			this.addChildAt(red, 0);
35
			this.addChildAt(blue, 1);
36
		}

(Editor's note: Commenter Jesse has let us know that the way the File class works has changed since this tutorial was published. See his comment for more details on how to make your code compatible.)

Step 11: Saving the File

After we've moved the little circles around a bit, we can then save their location for further editing. Here we create our custom file. First we put coordinates into an array, then the arrays are put inside an object. The object is written to a file with our custom file extension. You can give it any extension you want.

After that, we set the app to be the default application for the newly created file extension.

1
2
private function saveClick(event:Event):void
3
		{
4
			var file:File = File.desktopDirectory
5
			file.browseForSave("Save");
6
			file.addEventListener(Event.SELECT, onSaveSelect);
7
		}
8
		private function onSaveSelect(event:Event):void
9
		{
10
			var object:Object = {};
11
			var redArray:Array = [red.x, red.y];
12
			var blueArray:Array = [blue.x, blue.y];
13
			object.RED = redArray;
14
			object.BLUE = blueArray;
15
			var saveFile:File = File(event.target);
16
			var directory:String = saveFile.url
17
			if(directory.indexOf(".tuts") == -1)
18
			{
19
				directory += ".tuts";
20
			}
21
			var file:File = new File();
22
			file = file.resolvePath(directory);
23
			var fileStream:FileStream = new FileStream();
24
			fileStream.open(file, FileMode.WRITE);
25
			fileStream.writeObject(object);
26
			fileStream.close();
27
			NativeApplication.nativeApplication.setAsDefaultApplication("tuts");
28
		}

Step 12: Opening the File

If you want to open your newly created file, simply click the "Open" button. A dialog box appears that looks only for that file extension. The app will then read the object inside the file and places the little circles accordingly.

1
2
private function openClick(event:MouseEvent):void
3
		{
4
			var file:File = File.desktopDirectory;
5
			file.addEventListener(Event.SELECT, onSelect);
6
			file.browseForOpen("Open",[new FileFilter("Tuts Files (*.tuts)", "*.tuts")]);
7
		}
8
		private function onSelect(event:Event):void
9
		{
10
			var file:File = File(event.target);
11
			var fileStream:FileStream = new FileStream();
12
			fileStream.open(file, FileMode.READ);
13
			var object:Object = fileStream.readObject();
14
			red.x = object.RED[0];
15
			red.y = object.RED[1];
16
			blue.x = object.BLUE[0];
17
			blue.y = object.BLUE[1];
18
			fileStream.close();
19
		}

Step 13: Invoking the App

This is the invoke function. Without this function, if you were to launch the application from your new file, it wouldn't know to load it. This function checks to see what told it to open. If it was a file, then it will load that file. If it wasn't, then it simply calls the "init" function which gives the circles a random placement.

1
2
private function onInvoke(event:InvokeEvent):void
3
		{
4
			if(event.currentDirectory != null && event.arguments.length > 0)
5
			{
6
				var directory:File = event.currentDirectory;
7
				var file:File = directory.resolvePath(event.arguments[0]);
8
				var fileStream:FileStream = new FileStream();
9
				fileStream.open(file, FileMode.READ);
10
				var object:Object = fileStream.readObject();
11
				red.x = object.RED[0];
12
				red.y = object.RED[1];
13
				blue.x = object.BLUE[0];
14
				blue.y = object.BLUE[1];
15
				fileStream.close();
16
			} else
17
			{
18
				init();
19
			}
20
		}

Step 14: The Publish Settings

When the file is all tested and working correctly, we are ready to publish. Go to Commands > AIR - Application and Installer Settings, and bring up the publish settings.

””””””

Step 15: Setting up the Custom File Extension

In the Air publish settings, click on the advanced settings.

””””””

It will bring up another dialog box. Click on the "plus" button to add a file extension.

””””””

Fill out the file descriptions, select your custom icons, and click "OK" until you're back to the first publish settings window.

””””””
””””””

Step 16: Publish Your File

The last thing to do is to publish your file. Click on the "Publish AIR File" button. You will need to create a certificate to sign the app with. Simply click "Create" to bring up the settings.

””””””

Fill out the form and click "OK". Flash will prompt you when the certificate is created. When the certificate is made, enter the password and your file will be created.

””””””

Conclusion

This was just a basic example of what can be done with this technique. You could also create some kind of drawing application where you could either save out what you've drawn, or keep editing it. Or if you wanted to create a custom MP3 player, and have your own playlist file format. The possibilities are endless..

I hope you enjoyed following the tut.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.