Creating Your First HTML/JavaScript AIR App on the Mac

Author: 

AIRDev.org

Once you have the SDK installed and setup, as described in Getting Started, you may want to test it out by compiling a simple test app. Doing so is very simple. Open "Terminal.app" on a Mac, which can be found in your /Applications/Utilities folder. Then, make a new directory in the Finder. (it can be anywhere, as long as adl and adt are in your path, which is detailed in Getting Started). Go to the directory in the Terminal, by typing "cd" followed by a space, and then dragging the icon of the folder in the Finder to the Terminal, and press enter. Now, we're going to make the required files with TextEdit. Open TextEdit, and open two blank documents. In the menu, go to Format->Make Plain Text for both of them. Then save one as "application.xml", and one as "application.html". Copy the following code into "application.xml".

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://ns.adobe.com/air/application/1.0.M4" appId="com.airdev.htmlexample" version="1.0">
	<name>HTML Example</name>
	<installFolder>AIRDev/Examples</installFolder> 
	<description>A simple AIR application written in HTML.</description>
	<copyright>© 2007 AIRDev.org</copyright>
	<rootContent systemChrome="standard" transparent="false" visible="true" width="600" height="400">
		application.html
	</rootContent>
</application>

You just created the XML file that defines what your application will be like. Change it around if you want, and then save and close.

Now, onto the actual application. We're going to use a simple JavaScript app. Switch to application.html in TextEdit, and copy this into it:

<html>
<head>
<script type="text/javascript">
//By: Chris Campbell
//Created: May 20, 2005
//Last Modified: June 27th, 2005
//www.particletree.com


window.onload = attachFormHandlers;

var gShow; //variable holding the id where feedback will be sent to.
var sUrl = "http://airdev.org/stuff/formvalidation.php?validationtype=ajax&val=";//url is the page which will be processing all info
var gErrors = 0; //number of errors is set to none to begin with
var http = getHTTPObject();//don't worry about this



function attachFormHandlers()
{
	var form = document.getElementById('form1') 

	if (document.getElementsByTagName)//make sure were on a newer browser
	{
		var objInput = document.getElementsByTagName('input');
		for (var iCounter=0; iCounter<objInput.length; iCounter++)
		objInput[iCounter].onblur = function(){return validateMe(this);} //attach the onchange to each input field
	}
	form.onsubmit = function(){return validate();} //attach validate() to the form
}

/*validateMe is the function called with onblur each time the user leaves the input box
passed into it is the value entered, the rules (which you could create your own), and the id of the area the results will show in*/
function validateMe(objInput) {

	sVal = objInput.value; //get value inside of input field
	
	sRules = objInput.className.split(' '); // get all the rules from the input box classname
	sRequired = sRules[1]; // determines if field is required or not
	sTypeCheck = sRules[2]; //typecheck are additional validation rules (ie. email, phone, date)
    gShow = sRules[3]; //gShow is the td id where feedback is sent to.
  
	//sends the rules and value to the asp page to be validated
	http.open("GET", sUrl + (sVal) + "&sRequired=" + (sRequired) + "&sTypeCheck=" + sTypeCheck, true);
  
	http.onreadystatechange = handleHttpResponse; 	// handle what to do with the feedback 
	http.send(null);  
}


function handleHttpResponse() {
	//if the process is completed, decide to do with the returned data
	if (http.readyState == 4) 
  	{
		
  		sResults = http.responseText.split(","); //results is now whatever the feedback from the asp page was
		//whatever the variable glo_show's (usermsg for example) innerHTML holds, is now whatever  was returned by the asp page. 
    	document.getElementById(gShow).innerHTML = "";
		document.getElementById(gShow).appendChild(document.createTextNode(sResults[0]));
  	}
}


function validate()
{
var tables; 

tables = document.getElementsByTagName('td')

	for (i=0; i<tables.length; i++)//loop through all the <td> elements 
	{
		// if the class name of that td element is rules check to see if there are error warnings
		if (tables[i].className == "rules")
		{
			//if there is a Valid or its blank then it passes
			if (tables[i].innerHTML == 'Valid' || tables[i].innerHTML == '' )
			{
				tables[i].style.color = '#000000';//the color is changed to black or stays black
			}
			else
			{
				gErrors = gErrors + 1; //the error count increases by 1
				tables[i].style.color = '#ff0000';//error messages are changed to red
			}
		}
	}
		
	if (gErrors > 0)
	{
		//if there are any errors give a message
		alert ("Please make sure all fields are properly completed.  Errors are marked in red!");
		gErrors = 0;// reset errors to 0
		return false;
	}
	else return true;

}


function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try 
		{
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
		xmlhttp = false;
		}
	}
	return xmlhttp;
}
</script>
</head>
<body>
<fieldset>
<legend>Ajax Form</legend>
<form name="form1" id="form1" method="post" action="http://airdev.org/stuff/formvalidation.php?validationtype=php" onSubmit="return validate();">
<table width="500">
	<tr>
		<td width="130">Username </td> 
		<td width="170"><input type="text" name="user" tabindex="1" id="user" class="validate required none usermsg"/></td>
		<td id ="usermsg"  class="rules">Required</td>
	</tr>
	<tr>
		<td>Email </td>
		<td><input type="text" name="email" tabindex="2"  id="email" class="validate required email emailmsg" /></td>
		<td id="emailmsg" class="rules">Required</td>
	</tr>	
	<tr>
		<td><input type="submit" name="Submit" value="Submit" tabindex="5" /></td>
	</tr> 
</table>
</form>
</fieldset>
</body>
</html>

Or, put your own application code into the document. Once you're done this, it's time to compile! In Terminal, type:

adt -package htmlexample.air application.xml application.html

Once the command completes, you will have "htmlexample.air" in the folder, which you can then distribute, or install. Happy developing!

Average: 5 (1 vote)