Welcome!

Industrial IoT Authors: Pat Romanski, William Schmarzo, Elizabeth White, Stackify Blog, Yeshim Deniz

Blog Feed Post

Accessing the AIR XML file descriptor through an AIR menu item with Flash CS4 (The Essential Guide to Flash CS4 AIR Development Highlight)

The Essential Guide to Flash CS4 AIR Development book is oriented to Flash developers interested in building desktop applications via Adobe AIR. You can order The Essential Guide to Flash CS4 AIR Development  on Amazon or buy it on local bookstore.

This is the fifth part of the series dedicated to AIR menus (Chapter 6 of the Essential Guide to Flash CS4 AIR Development). You can read the previous articles here:

We start assignig the menu to the main AIR  application. It will only contain one item: ‘App settings’ to which you will associate three elements with as many commands. These three elements will be constructed in the createFirstSubMenu() method . The following ActionScript code defines the createFirstSubMenu():

private function createFirstSubMenu():NativeMenuItem
{

Start by creating a local variable, subMenu , an instance of the NativeMenuItem class, which will be the return value of the method createFirstSubMenu() . When you create the NativeMenuItem object, you tell AIR which text label it will have (App settings in the following code). This NativeMenuItem child of the application menu will be a drop- down menu that contains some commands. To add these child elements to the following instance of the NativeMenuItem class , you have to instantiate an object of the NativeMenu class. This object will then be assigned to its subMenu property . The following code accomplishes
these tasks:


// create submenu
var subMenu:NativeMenuItem = new NativeMenuItem( "App settings" );
// initialize child container
subMenu.submenu = new NativeMenu();

Next, populate the submenu you’ve just created with three instances of the NativeMenuItem class. Then register a different event listener method on each of them for the selection event. Remember, to create an element of the menu, you generate an instance of the NativeMenuItem class. Then you tell the constructor method which label has to be associated with the object. At that point, register an event listener method for the Event.SELECT event . Finally, add the element you’ve created to the submenu. Do this through the addItem() method of the instance of the NativeMenu class, which is assigned to the subMenu property of the object of the NativeMenuItem class.


// create first child, register event listener for
// selection event and assign to submenu

var aboutCommand:NativeMenuItem = new NativeMenuItem( "About.." );
aboutCommand.addEventListener( Event.SELECT, getInformation );
subMenu.submenu.addItem( aboutCommand );

// create second child, register event listener for
// selection event and assign to submenu

var minimizeCommand:NativeMenuItem =
new NativeMenuItem( "Minimize" );
minimizeCommand.addEventListener( Event.SELECT,
minimizeApplication );
subMenu.submenu.addItem( minimizeCommand );

// create third child, register event listener for
// selection event and assign to submenu

var closeCommand:NativeMenuItem = new NativeMenuItem( "Close" );
closeCommand.addEventListener( Event.SELECT, closeApplication );

subMenu.submenu.addItem( closeCommand );
return subMenu;
}

All you have to do now is define the event listener functions you need for the selection events. Your menu will allow you to do the following:

1. Minimize the application
2. Close the application
3. Access the saved description in the XML configuration file of the AIR project

The closeApplication() method will call the immediate closure of the application and, consequently, all its open windows. To close the application, you use the exit() method of the static nativeApplication property of the NativeApplication class. The code follows:

// close application

private function closeApplication( e : Event ):void
{
NativeApplication.nativeApplication.exit();
}

To minimize the active window of the application, you launch the minimizeApplication() method , which uses the minimize() method of the instance of the NativeWindow class. Here’s the code:

// minimize application

private function minimizeApplication( e : Event ):void
{
stage.nativeWindow.minimize();
}

This is the complete ActionScript class that you’ve just created:

package com.comtaste.foed.essentialair.chapter6
{

import fl.controls.TextArea;
import flash.desktop.NativeApplication;
import flash.display.MovieClip;
import flash.display.NativeMenu;
import flash.display.NativeMenuItem;
import flash.display.NativeWindow;
import flash.events.Event;
import flash.filesystem.File;

public class Ch06p02 extends MovieClip
{
// onstage components
public var output:TextArea;
// class properties
private var menuRoot:NativeMenu;


public function Ch06p02()
{

super();
// generate native menu to use
createNativeMenu();
// assign to application menu if we are on Mac OS X
if ( NativeApplication.supportsMenu )
{
NativeApplication.nativeApplication.menu = menuRoot;
}
// assign to window menu if we are on Microsoft Windows
if ( NativeWindow.supportsMenu )
{
stage.nativeWindow.menu = menuRoot;
}
}

// create a complete native menu

private function createNativeMenu():void
{

// instantiate main menu object
menuRoot = new NativeMenu();
// append subMenus to menu root
menuRoot.addItem( createFirstSubMenu() );

}


private function createFirstSubMenu():NativeMenuItem
{

// create submenu
var subMenu:NativeMenuItem =
new NativeMenuItem( "App settings" );
// initialize child container
subMenu.submenu = new NativeMenu();
// create first child, register event listener for
// selection event and assign to submenu
var aboutCommand:NativeMenuItem =
new NativeMenuItem( "About.." );
aboutCommand.addEventListener( Event.SELECT, getInformation );
subMenu.submenu.addItem( aboutCommand );

// create second child, register event listener for
// selection event and assign to submenu
var minimizeCommand:NativeMenuItem =
new NativeMenuItem( "Minimize" );
minimizeCommand.addEventListener( Event.SELECT, minimizeApplication );
subMenu.submenu.addItem( minimizeCommand );
// create third child, register event listener for
// selection event and assign to submenu
var closeCommand:NativeMenuItem =
new NativeMenuItem( "Close" );
closeCommand.addEventListener( Event.SELECT,
closeApplication );
subMenu.submenu.addItem( closeCommand );
return subMenu;

}

// close application

private function closeApplication( e : Event ):void
{

NativeApplication.nativeApplication.exit();

}

// minimize application
private function minimizeApplication( e : Event ):void
{

stage.nativeWindow.minimize();

}
} // close class
} // close package


Accessing the AIR XML file descriptor


Through ActionScript 3, you can access the XML document used by AIR’s runtime to execute your application correctly. Flash CS4 generates the XML document in the authoring phase. In this document, you can see a lot of information regarding the application in execution. The document also includes the description of the application, if provided by the developer.

Flash CS4 provides a specific menu that allows you to create the XML configuration file of the application. You can access this menu through the Edit AIR Settings button on the Document Properties panel in the Flash project. You can see the menu in Figure 6-7. Among the various fields, there is a Description field where you can provide a description of the application that will be presented to the user when the application is being installed.

The XML document that is automatically generated by Flash CS4’s menus for the ch06p02.fla project is the following:

<?xml version ="1.0" encoding="utf- 8" ?>

<application xmlns="http://ns.adobe.com/air/application/1.0">

<id>com.adobe.example.ch06p02</id>
<version>1.0</version>
<filename>ch06p02</filename>
<description>
Application developed to test application
and windows menus for Adobe Air desktop
applications.
</description>
<name>ch06p02</name>
<copyright></copyright>
<initialWindow>
<content>ch06p02.swf</content>
<systemChrome>standard</systemChrome>
<transparent>false</transparent>
<visible>true</visible>

</initialWindow>
<customUpdateUI>false</customUpdateUI>
<allowBrowserInvocation>false</allowBrowserInvocation>
<icon>
</icon>
</application>

Calling the getInformation() method has the following effects:

It accesses the XML descriptor document.
It extracts the description.
It displays it in the output TextArea.

Here’s an example of the getInformation() method:

// called on click on menu item "About.."
private function getInformation( evt : Event ):void
{

The content of the XML descriptor fiIe of the application is stored in the applicationDescriptor property of the NativeApplication class . Assign the value of this document to a local XML variable, appDescriptor, as shown here:

// access to application XML descriptor
var appDescriptor:XML =
NativeApplication.nativeApplication.applicationDescriptor;

To be able to access the nodes of the XML document, first you have to know which XML namespace they’ve been defined in. You can access this information through the namespace() method of the XML class , as shown here:

// retrieve XML descriptor namespace
var nsDeclaration:Namespace = appDescriptor.namespace();

Now you have the content of the XML descriptor file of the application. You’ve also defined the XML namespace to access. Next, you can use the E4X syntax to extract the information you’re interested in.

E4X is a programming language extension that adds native XML support to ECMAScript (which ActionScript is based on). E4X provides a simpler syntax for accessing XML documents and treats XML as a primitive data type.

The form you use for the XML query is the following:

//<XML instance>.<namespace>::<node name>
// read description node from XML

var informationText:String =
String( appDescriptor.nsDeclaration::description[ 0 ] );

Finally, write the definition extracted from the XML descriptor file of the application in the output TextArea.

To wrap the text in the TextArea, use the AIR lineEnding variable of the File class instead of the ActionScript Unicode \n sequence. This variable correctly reports the native line ending for the local operating system. This will be useful if the text gets copied and pasted into an external text document. Here’s the code:

// write into textare 'output' description contents
output.appendText( "Adobe Air, test application for " +
"menus functionalities." + File.lineEnding );
output.appendText( informationText + File.lineEnding );
}

This is the final getInformation( ) method:

// called on click
private function getInformation( evt : Event ):void
{
var appDescriptor:XML =
NativeApplication.nativeApplication.applicationDescriptor;
var nsDeclaration:Namespace = appDescriptor.namespace();
var informationText:String =
String( appDescriptor.nsDeclaration::description[ 0 ] );
output.appendText( "Adobe Air, test application for " +
+ "menus functionalities." + File.lineEnding );
output.appendText( informationText + File.lineEnding );
}

Your application is finished and is ready to use. To do so, go back to Flash, and run the application by selecting the Test Movie command from the Control menu. If you’re working in a Microsoft Windows environment, your application will use a window menu and will look like Figure 6-8. If, on the other hand, you’re working in a Mac OS X environment, it will use an application menu, like all native Mac OS X applications.

In the next part of this series you'll learn how to create and populate context menus for AIR applications.

Read the original blog entry...

More Stories By Marco Casario

Marco Casario is CEO of Comtaste, a company devoted to develop Rich Internet Applications on the Web and for mobile devices.

He collaborates intensively with Adobe Italy as a speaker at conferences and as a consultant for Flash, Flex, and Flash Lite.

Learn more about Marco Casario at his blog http://casario.blogs.com. In 2005, Marco has founded Comtaste, a company dedicated to exploring new frontiers in Rich Internet Applications and the convergence between the web and the world of mobile devices — MobyMobile and YouThru are representative of their recent work. He is founder of the biggest worldwide Flash Lite User Group and of www.augitaly.com, a reference point for the Italian community of Adobe users, in which he carries out the role of Channel Manager for the section dedicated to Flex.

IoT & Smart Cities Stories
Moroccanoil®, the global leader in oil-infused beauty, is thrilled to announce the NEW Moroccanoil Color Depositing Masks, a collection of dual-benefit hair masks that deposit pure pigments while providing the treatment benefits of a deep conditioning mask. The collection consists of seven curated shades for commitment-free, beautifully-colored hair that looks and feels healthy.
The textured-hair category is inarguably the hottest in the haircare space today. This has been driven by the proliferation of founder brands started by curly and coily consumers and savvy consumers who increasingly want products specifically for their texture type. This trend is underscored by the latest insights from NaturallyCurly's 2018 TextureTrends report, released today. According to the 2018 TextureTrends Report, more than 80 percent of women with curly and coily hair say they purcha...
The textured-hair category is inarguably the hottest in the haircare space today. This has been driven by the proliferation of founder brands started by curly and coily consumers and savvy consumers who increasingly want products specifically for their texture type. This trend is underscored by the latest insights from NaturallyCurly's 2018 TextureTrends report, released today. According to the 2018 TextureTrends Report, more than 80 percent of women with curly and coily hair say they purcha...
We all love the many benefits of natural plant oils, used as a deap treatment before shampooing, at home or at the beach, but is there an all-in-one solution for everyday intensive nutrition and modern styling?I am passionate about the benefits of natural extracts with tried-and-tested results, which I have used to develop my own brand (lemon for its acid ph, wheat germ for its fortifying action…). I wanted a product which combined caring and styling effects, and which could be used after shampo...
The platform combines the strengths of Singtel's extensive, intelligent network capabilities with Microsoft's cloud expertise to create a unique solution that sets new standards for IoT applications," said Mr Diomedes Kastanis, Head of IoT at Singtel. "Our solution provides speed, transparency and flexibility, paving the way for a more pervasive use of IoT to accelerate enterprises' digitalisation efforts. AI-powered intelligent connectivity over Microsoft Azure will be the fastest connected pat...
There are many examples of disruption in consumer space – Uber disrupting the cab industry, Airbnb disrupting the hospitality industry and so on; but have you wondered who is disrupting support and operations? AISERA helps make businesses and customers successful by offering consumer-like user experience for support and operations. We have built the world’s first AI-driven IT / HR / Cloud / Customer Support and Operations solution.
Codete accelerates their clients growth through technological expertise and experience. Codite team works with organizations to meet the challenges that digitalization presents. Their clients include digital start-ups as well as established enterprises in the IT industry. To stay competitive in a highly innovative IT industry, strong R&D departments and bold spin-off initiatives is a must. Codete Data Science and Software Architects teams help corporate clients to stay up to date with the mod...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Druva is the global leader in Cloud Data Protection and Management, delivering the industry's first data management-as-a-service solution that aggregates data from endpoints, servers and cloud applications and leverages the public cloud to offer a single pane of glass to enable data protection, governance and intelligence-dramatically increasing the availability and visibility of business critical information, while reducing the risk, cost and complexity of managing and protecting it. Druva's...
BMC has unmatched experience in IT management, supporting 92 of the Forbes Global 100, and earning recognition as an ITSM Gartner Magic Quadrant Leader for five years running. Our solutions offer speed, agility, and efficiency to tackle business challenges in the areas of service management, automation, operations, and the mainframe.