My Books

  • Follow me on Twitter
My Photo

Subscribe my blog

  • Get this widget from Widgetbox
  • Add to Google
  • RSS FEEDS
  • Enter your Email here to subscribe :



    Powered by FeedBlitz

Blog Widget by LinkWithin

« Ready to learn the new FXG to create basic vector and bitmap shapes? | Main | Speaking at The ActionScript Conference 2008 »

Create an "Open Recent Files" cross-operating system menu in your AIR application

Using the   NativeMenu and NativeMenuItem classes supported by Adobe AIR APIs is pretty easy to create native menu. 
The native menu classes allow you to create the following types of menus:

  • Application menu
  • Window menus
  • Context menus
  • Dock icon menu on Mac OS X
  • system tray icon menus
  • Pop-up menus
  • Flex menus
  • Custom menus

With a few lines of ActionScript code you can create native menus for your AIR desktop applications:

var myMenu:NativeMenu = new NativeMenu();
var fileMenu:NativeMenuItem = myMenu.addItem( new NativeMenuItem( "File" ) );
fileMenu.submenu = buildFileMenu();

var openPDFMenu:NativeMenuItem = myMenu.addItem( new NativeMenuItem( "Open Recent Files" ) );
openPDFMenu.submenu = createPDFMenu();

The code above creates two menu items, File and Open Recent Files, within a native menu  using the NativeMenu and NativeMenuItem classes.
In order to create and display a system menu into your AIR application you must  set the application menu  the menu property of the nativeWindow  (for Windows system) or nativeApplication (for Mac OSX system) to a NativeMenu object (or to a callback function that returns a NativeMenu object):

// works in windows system
if( NativeWindow.supportsMenu ) stage.nativeWindow.menu = createAppMenu();

// Works in MAC
if( NativeApplication.supportsMenu ) NativeApplication.nativeApplication.menu = createAppMenu();

I've written two different lines depending on the operating system the application will be run. This is one of the few cases which developers have to handle with a condition tht verifies the operating system is run.

But one cool thing  very useful in a desktop application that works with local files is a Open Recent Files menu item features. This menu item  acts like a history list of opened or created file.
The NativeMenuItem class has the data property that allows to create a reference to an Object for a menu item. You can use it to create a menu item that points to a file on local system as shown in the following code:

var pdfMenu:NativeMenu = new NativeMenu();

var file:File = File.documentsDirectory.resolvePath("Flash.pdf")
var menuItem:NativeMenuItem = pdfMenu.addItem(new NativeMenuItem(file.name));
menuItem.data = file;

The data property is set to the file object that contains the reference to a PDF file, Flash.pdf, contained inside the stadard directory: documentsDirectory.

This is an example of an AIR file that creates a native menu with two menu items: File and Open Recent Files. The latter allows you to directly open a PDF file on the local computer (you must put a PDF file named Flash.PDF on your user's document directory for testing):

This is an example of an AIR file that creates a native menu with two menu items: File and Open Recent Files. The latter allows you to directly open a PDF file on the local computer (you must put a PDF file named Flash.PDF on your user's document directory for testing):

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:com="com.casario.*"
layout="absolute"
width="800"
height="600"
backgroundColor="0x000000"
verticalScrollPolicy="off"
horizontalScrollPolicy="off"
applicationComplete="init()">

<mx:Script>
<![CDATA[
import mx.controls.Alert;

private function init():void
{
// works in windows system
if( NativeWindow.supportsMenu ) stage.nativeWindow.menu = createAppMenu();

// Works in MAC
if( NativeApplication.supportsMenu ) NativeApplication.nativeApplication.menu = createAppMenu();

}

private function createAppMenu():NativeMenu
{

var myMenu:NativeMenu = new NativeMenu();
var fileMenu:NativeMenuItem = myMenu.addItem( new NativeMenuItem( "File" ) );
fileMenu.submenu = buildFileMenu();

var openPDFMenu:NativeMenuItem = myMenu.addItem( new NativeMenuItem( "Open Documents" ) );
openPDFMenu.submenu = createPDFMenu();

return myMenu;
}

private function buildFileMenu():NativeMenu
{
var fileMenu:NativeMenu = new NativeMenu();

var closeAppMenu:NativeMenuItem = fileMenu.addItem( new NativeMenuItem( "Close App" ));
closeAppMenu.addEventListener( Event.SELECT , closeApp );

return fileMenu;
}

private function createPDFMenu():NativeMenu
{
var pdfMenu:NativeMenu = new NativeMenu();

var file:File = File.documentsDirectory.resolvePath("Flash.pdf")
var menuItem:NativeMenuItem = pdfMenu.addItem(new NativeMenuItem(file.name));
menuItem.data = file;

menuItem.addEventListener( Event.SELECT , openPDF );

return pdfMenu;

}

private function openPDF( event:Event ):void
{
/* TODO: APIRE UN FILE PDF
var videoFilter:FileFilter = new FileFilter("Video", "*.flv;*.mp4;*.mov;*.mpg;*.m4a;*.mp4v;*.3gp;*.3g2");
var file:File = new File();
//file.addEventListener( Event.SELECT , handleVideoSelection );
//file.browseForOpen("Video" , [videoFilter] );
*/
var uri:String = new String();

if (Capabilities.os.search("Mac") >= 0)
    {
    uri = ""

    }else{
    uri = "file://";
    }

   

if(HTML.pdfCapability== HTMLPDFCapability.STATUS_OK)
{
              pdf.location= uri + event.target.label;
            }else{
              pdf.location="http://www.adobe.com/products/acrobat/readstep2.html";
            }

}

   private function closeApp( event:Event ):void
{
stage.nativeWindow.close();
}


]]>
</mx:Script>

<mx:HTML id="pdf"  width="800" height="600" />

</mx:WindowedApplication>

Comments

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Pistach.io

Speaker at

WebDeveloper's Journal Author

  • My favourite Flash Lite 3 mobile phone
  • Web Developer's & Designer's Journal by Sys Con Media
  • Web Developer's & Designer's Journal Blogger
    Web Developer's & Designer's Journal by Sys Con Media
  • FullAsGoog Aggregator
  • Macromedia WebLogs Aggregator

June 2009

Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30