To monitor a URL
1. In your project's primary HTML file, include the Shockwave file Script 13.1 lt script src servicemonitor.swf type application x-shockwave- flash gt lt script gt To use the URLMonitor class, an application must include this file. For the type attribute, make sure you specify its value as application x-shockwave-flash. 2. In a JavaScript block, create the two objects var url new air.URLRequest 'http www.example.com' var monitor new air.URLMonitor url This code is pretty much the same as the...
To handle menu events
1. Open Script 7.1 in your text editor or IDE, if it is not already. Throughout this entire chapter you'll continue to build on this one example until it's fully functional or reasonably so . 2. Anywhere within the JavaScript, define a function that should be called when the user selects File gt Exit Script 7.2 function doExit e lt html gt lt -- Script 7.2 -- gt lt head gt lt title gt Menu Events lt title gt lt script type text javascript lt script Function for handling the Exit menu item...
To create a fullscreen window
1. In your project's HTML file, begin a new JavaScript function Script 6.6 lt script type text javascript gt function makeFullScreenO 2. Within the makeFullScreenO function, change the stage. displayState property window.nativeWindow.stage. This rather verbose line is all you need to enable full-screen mode. 3. Within the makeFullScreenO function, change the HTML button document.getElementById 'btn' . onclick makeNormalScreen document.getElementById 'btn' . innerHTML 'Normal or press The HTML...
To read a directory
1. In your project's primary HTML file, begin a new JavaScript function Script 9.3 I'll start by creating a new project with its own root HTML page. Within it an anonymous function will be defined that is automatically called once the window has loaded. This is necessary because the page will add elements to the DOM, and the document's body will only exist after the page has loaded. 2. Within the anonymous function, get the contents of a directory var dir air.File.desktopDirectory var contents...
To work with binary data
1. In your project's primary HTML file, create the required objects Script 10.4 var file air.File.createTempFile var stream new air.FileStream This program will store some session data the kind of data that an application might track while the user is running it in a temporary file. The createTempFile method takes care of identifying and creating a file for this purpose. 2. Begin a function that will write some data to a file The application has just two buttons O Figure 10.9 . When the Write...
To use prepared statements
1. In your project's primary HTML file, create the required objects Script 12.2 var conn new air.SQLConnection var db air.File. var insert new air.SQLStatement This application will use three objects. The first is of type SQLConnection, which is required to connect to a database. The second is of type File, representing the actual database file. The third is a SQLStatement object, which will represent the query to be executed. 2. Create an anonymous function that does the prep work...
Using Prepared Statements
In the INSERT example in the previous chapter, it was noted that an admitted security hole was present. Namely, the user's input was being added to the query without taking extra precautions. This allows for SQL injection attacks, where the user purposefully enters malicious text in an attempt to break, damage, or simply learn something about the database. With some database applications, SQL injection attacks can be prevented by using an escaping function that sanctifies the submitted text....
To copy data to the clipboard 1
1. Open Script 8.3 in your text editor or IDE, if it is not already. 2. Add a clause to the body tag so that the doPaste function is called when the user invokes Edit gt Paste Script 8.5 lt body style margin 10px oncopy doCopy event oncut doCut event onpaste doPaste event gt The added code specifies that when a paste event happens, the doPaste function should be called. 4. Open editFunctions.js Script 8.4 in your text editor or IDE, if it is not already. Script 8.5 The main HTML page is updated...
To transmit data
1. In your project's primary HTML file, create the form Script 13.4 lt h3 gt Enter your license information lt h3 gt lt p gt Registered To lt input type text id name gt lt p gt lt p gt License Code lt input type text id license gt lt p gt lt p gt lt input type submit value Submit So that you can follow the logic more easily, I'll start by creating the form Figure 13.5 . It contains two text inputs and a button that calls the checkLicense function when clicked. 2. Within the JavaScript code,...
Using AI RAliasesjs
As mentioned earlier, your AIR applications will make frequent and extensive use of the AIR and Flash APIs, resulting in lines like var fp new window.runtime.flash. filesystem.FileO To make code easier to type, read, and debug, Adobe has created the AIRAliases .js file. This is a JavaScript document that creates shortcuts to the most commonly used AIR and Flash APIs. You'll find this file in the SDK's frameworks folder. By using the aliases file, instead of the line as written above, you can...
F
fetching records in groups, 245-248 file URL, 48 File class, 156, 287 File objects copying and, 177 creating, 156, 157, 335 databases created without, 208 filtering arguments for browseForOpen methods, 160 properties of, 164 representing files and directories that don't exist, 158 FileMode class, 156 FileMode constants, 184 filename element, 21, 22 files. See also downloading PDF files XML files and specific files .air, 27-28, 353 AIRAliases.js, 49-50 AIRSourceViewer.js, 71 allowing users to...
o z
4. Find the call to the AC_FL_RunContent function cabs flash swflash.cab', 'width','217', 'height','180', 'id','badge', 'align','middle', 'src','badge', 'quality','high', 'bgcolor',' FFFFFF', 'name','badge', 'allowscriptaccess','all', 'pluginspage','http www. 'flashvars','appname myapp.air amp airversion 1.0 amp imageurl test.jpg', 'movie','badge' This function, which is defined within the AC_RunActiveContent. j s file that's included by this page, does all the work. The function call in the...
Receiving Data
The first example in this chapter shows how easy it is to connect to a URL. That code can also confirm two things that the user has a live network connection and that the named Web site is available. But you'll almost always want your applications to do more than just this normally, they should either retrieve data from that URL or send data to it. To retrieve data, start by creating the URLRequest object var url new air.URLRequest From there, instead of using a URLMonitor object, which can...
To create platformspecific menus
1. Open Script 7.2 in your text editor or IDE, if it is not already. 2. Replace the line where the menu is added to the program with Script 7.3 if air.NativeWindow.supportsMenu window.nativeWindow.menu else if air.NativeApplication. supportsMenu nativeApplication.menu These two conditions were explained earlier. Here, they've just been put into an if-else if conditional. N Script 7.3 By using a conditional to check what kinds of menus the current operating system supports, this application can...
Using the Sandbox Bridge
In the AIR security model, applications are limited by a same-origin policy Content is restricted from directly interacting with content from another source. With this previous example, this means that content in the application sandbox Script 15.2 is limited in what it can do with content in the non-application sandbox Script 15.1 and vice versa. To overcome this limitation, you can use the sandbox bridge. Adobe AIR adds two properties to the window object childSandboxBridge and...
To access a native window
1. In your project's primary HTML file, create an anonymous function Script 6.5 lt script Instead of defining a function that creates the window, the window will be created within an anonymous function that is automatically run when the page is loaded. By using this technique, the JavaScript for creating the native window is only executed after the page has completely loaded, as opposed to being executed while the page is loading, which would be the case if no function was used. Table 6.3 These...
To use a sandbox bridge
1. In an HTML file, include all the necessary YUI files Script 15.3 lt link rel stylesheet type text css href http yui.yahooapis. lt script type text javascript lt script type text javascript lt script src http yui.yahooapis. lt script src http yui.yahooapis. These lines come verbatim from the YUI library documentation. Including these scripts is necessary to create the simple editor Yahool's term . Note that these files are all being included from Yahool's Web site they are not packaged with...
Tips Aku
Your application can access all the application descriptor file's settings by using this code var data air.NativeApplication. nativeApplication. applicationDescriptor After that line, data is a string of text. You'll then need to turn it into XML using a DOMParser object see Chapter 14, Using Other Media to access the element values. You can see an example of this in the Updating an Application section of this chapter. The publisher ID of the current program is available in air....
To support drag and drop in 1
1. Open index.html Script 8.9 in your text editor or IDE, if it is not already. 2. Within the body tag, identify the function to be called on when dragging begins Script 8.10 lt body style margin 10px oncopy doCopy event oncut doCut event onpaste doPaste event ondragover doDragOver event Added is the association of the doDrag function with the beginning of a dragging action. Script 8.10 In this final version of the application, a JavaScript function dictates what can be done with content being...
Updating and Deleting Records
Thus far you've learned how to execute CREATE, INSERT, and SELECT queries. These are the most important three, but two more still need to be covered UPDATE and DELETE. Both are executed just like every other query but handling the results is much more like an INSERT than a SELECT, because no records will be returned by them. Code that both updates and deletes records in a table would look like this without error handling and creating the requisite user-defined functions var conn new...
Running an Application
After you've successfully installed an AIR application, you'll have myriad ways to run it. You could probably figure out all these on your own, but the point is that once you've installed the runtime and the application, you'll see that AIR applications behave just like any other program on your computer. Check the Start application after installation box during the installation process Figures 1.9 and 1.10 . On Windows, use the desktop shortcut if one was created Figure 1.12 . On Windows, use...
The URLRequest Class
Of the various networking-related classes you'll use in an AIR application, URLRequest will be used the most. A URLRequest object only provides a reference to the resource to which your application should connect, so it doesn't do much but will be needed by the classes that do. When you create a URLRequest object, you can provide it with the URL the application will use var url new air.URLRequest 'http www.example.com' A URLRequest object can use many different URL schemes http, https,file,...
Downloading Files
Another way that a desktop application might interact with a server besides accessing pages and reading in the response is to download a file from it. The concept is the same, but the amount of data and what the application does with that data is different. Start, of course, with a URLRequest object From there, you could use the URLLoader class to download the file data, reading in the response as binary data. However, if you know you'll be handling binary data, the URLStream class provides a...
Tip Nuc
When creating an AIR application that uses your certificate, be sure to enter the certificate's password in the export window Figure 3.26 shows the window without the password filled in . Figure 3.29 As of AIR version 1, you have a choice of two encryptions for the certificate. Figure 3.29 As of AIR version 1, you have a choice of two encryptions for the certificate. Figure 3.30 The newly created certificate is now listed and has been selected as the default compare with Figure 3.27 . Figure...
Customizing Windows
The previous section showed how to create a new native window without any modifications. Once you have that working, you can start using the NativeWindowInitOptions object to tweak some of the window's behavior. Table 6.2 lists all of the possible settings. The defaults which the previous example implicitly demonstrated are used to create a normal window using the standard system chrome see the What Is the Chrome sidebar with no transparency and to create a window that is resizable,...
Creating a New Native Window
The second way to create a new application window besides using window.open is to create a native window. A native window looks and behaves like the primary application window so it'll better fit into the operating system. Native windows can also be more controlled and customized see Creating a New Look later in the chapter . To make a new native window, start by creating an object of type NativeWindowlnitOptions This object will be used to establish the window's attributes. Once you have this...
Adding Keyboard Equivalents
Often, items in a menu have a keyboard equivalent, also called a keyboard shortcut or an accelerator. Keyboard equivalents consist of the key and the modifier or modifier keys. Of course, you're already familiar with this concept Ctrl C Windows or Command C Mac OS X copies the currently selected item to the clipboard. To add a keyboard equivalent to your AIR application, you start with the menu item Then set the item's keyEquivalent property to the primary key Note that you should use a...
An Asynchronous Example
The text editor example developed thus far Scripts 10.1 and 10.2 performs synchronous transactions. This means that after a request to read from or write to a file, nothing else happens until that reading or writing is completed. When dealing with small amounts of data, this isn't a problem. With more data, the wait may be apparent to the end user, which is never a good thing. Performing asynchronous transactions isn't hard, but it does require more code. You'll need to create event listeners...
File and Directory Browsing
Commonplace in most applications is the ability for users to select a file or directory from their computer. For example, users might choose a file to edit or select a directory where a file should be saved. Creating a browse for file Figure 9.1 or browse for directory Figure 9.2 prompt in AIR is easy. Start by creating an object of File type in either case If you want to start the user off in a given location, use the appropriate shortcut reference without the new keyword var selection...
Basic Concepts and Code
As the simple test case in Chapter 2, Creating an Application, shows, with Adobe AIR you can make an executable desktop application using just HTML. The problem is that with HTML alone your program won't do anything useful. To make a functional application, you'll need to use additional technologies, starting with JavaScript. Although JavaScript can produce some nice results, there's no reason to stop there. It's easy enough to use ActionScript and Flash, even if you're not terribly familiar...
Big Endian Little Endian
Binary data is trickier than plain text because of the big endian, little endian problem. Some text characters and all numbers require multiple bytes to be represented. On some computer systems, the important byte is stored first, followed by the less important bytes this is big endian ordering . On other computer systems, the important bytes are left to last little endian . If data is stored using big endian but read as if it was in little endian or vice versa , the result will be a mess. You...
Playing Sounds
AIR applications have the capability of playing MP3 files using the Sound class AIR does not directly support playing other sound file types. To start, you'll need to create a URLRequest object, which will be a reference to the sound file the URLRequest class is discussed in more detail in Chapter 13, Networking . To create a URLRequest object, you provide its constructor the method called when an object of that type is created with a URL value. For a file on the user's computer, the URL would...
Reading from Files
Reading from and writing to files requires a File object and a FileStream object. As always, the File object is a reference to a file on the user's computer existing or to be created var file air.File.applicationDirectory. - resolvePath 'data.txt' That line associates file with data. txt, which is found in the directory where the application was installed. Next, create an object of type FileStream Then open the file for reading and or writing, using the stream.open method. It takes two...
Deleting Files and Directories
Files and directories can be deleted using the deleteFile and deleteDirectory methods or the deleteFileAsync and deleteDirectoryAsync versions . var file air.File.documentsDirectory. By default, the delete directory methods will only get rid of empty directories. To delete a directory that may have some files or subdirectories in it, pass the method an argument with the value true If you'd rather take less drastic steps, you can move the file or directory to the trash. That way the item won't...
Fundamentals
As I've done in some other chapters, I'll begin by covering some of the basic information you'll need before working with files and directories. You should read these three topics before moving on to the actual examples. When it comes to working with files and directories, there are three defined classes of note Of these, File is by far the most important. You'll use a File object to refer to a file or a directory on the user's computer. Once you have a File object, you can create new files and...
Handling Menu Events
One crucial step is missing in the previous example The menu items don't actually do anything. As with most functionality in Adobe AIR applications, creating an object is only half the process the other half is tying the object to an action. This means using events. Two events are pertinent to menus displaying and select. A displaying event is triggered just before a menu appears in the nanosecond between the time a user clicks on it and when it's displayed . The most obvious example of when...
Performing Transactions
Transactions are a different way of executing database queries. With the standard method used in this and the previous chapter, a query is run as soon as its execute method is called. More important, its effects are permanent There is no way of undoing an executed query. An alternative method is to run queries within a transaction. By doing so, you can create checks that guarantee everything worked as expected. Then you can either make the queries permanent or undo their collective effects,...
Associating File Types
Your application descriptor file can also indicate what types of files should be associated with the application. If the named file type or types isn't already associated with another program on that computer, the AIR application will become the default program for files of that type. To associate a file type with a program, add a fileTypes element with one fileType subelement for each associated file type. The fileType element has required name and extension subelements, along with optional...
Tips Dat
You may notice that the total number of bytes loaded sometimes differs from the stated number of total bytes. This can happen if the file doesn't communicate the correct total number of bytes it contains and is not necessarily a problem . To improve the security and reliability of this application, you could ensure that the user-submitted URL begins with http , https , orftp and that it ends with .mp3. The JavaScript substring function can be used for these purposes. You could also use the...
The XMLHttpRequest Object
The XMLHttpRequest object, which is part of JavaScript, has been around for years but has really gained popularity recently thanks to the rise of Ajax. XMLHttpRequest is a class that defines the functionality for HTTP HyperText Transfer Protocol interactions. Even if that sounds like gibberish to you, you're actually quite familiar with the concept When you load a Web page in your browser, you're making an HTTP request normally . Using XMLHttpRequest, JavaScript in one page can make that same...
Drag and Drop Out
Along with support for dragging content into an application's window, you can also choose to support a user's ability to drag application content into other programs. The net effect is the same as being able to copy or cut content within the application, and then paste it elsewhere. Looking back at Table 8.2, the event to be watched for here is ondragstart. That event occurs when the user selects something, clicks on it, and then starts moving the cursor. The nice thing is that no special steps...
o To use these new techniques
1. In your project's primary HTML file, create a File object Script 16.2 var file air.File. - gt applicationStorageDirectory. - gt resolvePath 'prefs.xml' Most applications use a preferences file, which is what this object is a reference to. That file will contain XML data and be stored in the application's storage directory. Chapter 14 demonstrates this concept in a fully functional example. You'll also want to copy the prefs. xml file Script 14.6 into this project's folder. 2. Begin an...
Debugging
One of the most important aspects of learning any new programming language or technology is knowing how to debug a problem when it occurs. No matter how skilled or smart you are, problems will occur, bugs will creep into your code, and something just won't work the way it should. In this chapter, you'll learn about some specific tech- O niques and actual code you can use to help solve the problems you encounter as you u develop your own AIR applications. g Because this book addresses AIR...
Handling Errors
A key difference between average and professional applications is how errors are handled. In a poorly written program, no consideration is made for errors, so it seems as if the expectation is they'll never occur. A complete program addresses every possible error, no matter how much extra code it takes. When interacting with databases, errors are rather common particularly during the development stage , so you should account for them. To properly handle errors that occur, add an event listener...
Handling Window Events
With native windows, many things that the user might do with the window maximize, minimize, resize, move, and close count as events. Handling events in general is discussed in Chapter 4, but you'll need to know how to handle specific types of window events to do some of the exercises that follow in this chapter. As a quick recap, the process for handling any event is Create a function to be called when an event occurs. Tell the window to associate that event with that function. You accomplish...
Using Aptana Studio
Aptana Studio, available at www. aptana. com, is an open-source IDE written in Java. Because it's a Java application, it runs on pretty much every operating system, as long as the Java runtime has already been installed keep in mind that you also need the Java runtime to use the ADL and ADT development tools . Aptana Studio is available in two versions, a free Community Edition and a Professional Edition listed at 199 US at the time of this writing . The commercial version supports a few more...
Using Dreamweaver
Dreamweaver is perhaps the most popular IDE available for Web developers. Because AIR and Dreamweaver are Adobe products Dreamweaver became so after Adobe purchased Macromedia , it's no surprise that you can use Dreamweaver to create AIR applications. Unlike Aptana Studio, Dreamweaver is a commercial application only, but you can download a 30-day trial version at www. adobe. com. To develop AIR applications in Dreamweaver, you must install the AIR Extension, which is simple enough, so there's...
Updating Your Path on Mac OS X
The two tools installed by the SDK the AIR Development Tool adt and the AIR Debug Launcher adl are used from the command line. This means you'll run them, on Mac OS X, through the Terminal application, not in a graphical interface. The command-line syntax is really easy to use, but there is one catch Both programs must be known by your computer. To accomplish this, you need to add the SDK folder's bin directory to your system's path. The path is simply a listing of where the computer can find...
Updating Your Path on Windows
The two tools installed by the SDK the AIR Development Tool adt and the AIR Debug Launcher adl are used from the command line. This means you'll run them, on Windows, through a console prompt, not in a graphical interface. The command line syntax is really easy to use, but there is one catch Both programs must be known by your computer. To accomplish this, you need to add the SDK folder's bin directory see Figure 2.2 to your system's path. The path is simply a listing of where the computer can...
Installing an Application
The AIR runtime needs to be installed only once on your computer for you to be able to install any number of AIR applications. Throughout the course of this book you'll create several AIR apps which is the point of the book, after all , but many apps are available and can be downloaded from the Internet as well. Along with any number of third-party AIR applications, Adobe provides some of its own, one of which I'll use in these steps. 1. Download an application from the Internet. You can find a...


















