AS3 Quickie: Check if a variable is Numeric

I’m still doing loads of ActionScript work in both my regular job and in side projects – so thought I might start posting some tiny, useful snippets of AS3 here as I encounter them. This is, I suppose, both for my own archive and for anyone out in the community that cares to pick up on these bits and pieces.

So this quickie comes from a project where I actually have an AIR application running as a service on a Windows 2008 virtual machine. The application monitors a remote folder for uploads from a web application in order to process these uploaded files for distribution.

The files themselves are renamed, upon upload, to a numeric value representing the ID of the object they are associated with in the web application. The server-based AIR app uses this ID to communicate with both the web application and an additional, remote Flash Media Server instance.

The problem is that, sometimes, when the file is uploaded… previous to it being renamed by ColdFusion… it will get snatched up by this AIR app and so the file name is whatever the user named the file previous to upload – not the expected system designated ID. So what’s the solution? Check to see whether the file name is truly numeric or not. Granted – if the user creates a file for upload which is numeric, it will get around this code… the chance of that happening is quite slim though, and even if it does happen, can be dealt with in other ways down the road.

var fileName:String = "2739.mp4";
var extIndex:int = fileName.indexOf(".");
var fileID:Number = Number(fileName.substr(0, extIndex));
if(!isNaN(fileID)){
	trace("Ok! Process file.");
}else{
	trace("Wait for next cycle...");
}

Creating Custom Context Menus in Flash

The assets for my 5 minute quick tip talk for the 2011 Adobe Education Leader Institute are located here. This is the completed example (Flash Professional CS5.5):
[download AEL2011_FlashContextMenu]

Here is an image preview:

Custom Context Menus!

Here is the SWF:

Flash Player 11.2 or greater is required!

And here be the code bits!

package  {
	import flash.display.Sprite;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	import flash.ui.ContextMenuBuiltInItems;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.events.ContextMenuEvent;
	
	public class CustomMenu extends Sprite {
		private const MENUITEM_FVM:String = "©2011 Fractured Vision Media, LLC";
		private const MENUITEM_JOSEPH:String = "Joseph Labrecque";

		public function CustomMenu(){
			buildMenu();
		}
		
		private function buildMenu():void {
			var myMenu:ContextMenu = new ContextMenu();
			myMenu.hideBuiltInItems();
			
			var menuItem0:ContextMenuItem = new ContextMenuItem(MENUITEM_FVM);
			menuItem0.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, visitURL);
			
			var menuItem1:ContextMenuItem = new ContextMenuItem(MENUITEM_JOSEPH);
			menuItem1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, visitURL);
			menuItem1.separatorBefore = true;
			
			myMenu.customItems.push(menuItem0);
			myMenu.customItems.push(menuItem1);
			
			this.contextMenu = myMenu;
		}
		
		private function visitURL(e:ContextMenuEvent):void {
			var site:String = "";
			switch(e.target.caption){
				case MENUITEM_FVM:
					site = "http://fracturedvisionmedia.com/";
					break;
				case MENUITEM_JOSEPH:
					site = "http://josephLabrecque.com/";
					break;
			}
			navigateToURL(new URLRequest(site), "_blank");
		}

	}
	
}

Quick Tip: Display Application Version in your AIR App.

Just a quick tip for those looking to somehow display the AIR application version from the descriptor file. I’ve found it very helpful in troubleshooting is users can say EXACTLY what version of the app they are running.

Application Version

AIR 2.5 and later:

private function getAppVersion():String {
	var appXml:XML = NativeApplication.nativeApplication.applicationDescriptor;
	var ns:Namespace = appXml.namespace();
	var appVersion:String = appXml.ns::versionNumber[0];
	return appVersion;
}

Older versions of AIR:

private function getAppVersion():String {
	var appXml:XML = NativeApplication.nativeApplication.applicationDescriptor;
	var ns:Namespace = appXml.namespace();
	var appVersion:String = appXml.ns::version[0];
	return appVersion;
}