1 Reply Latest reply on Feb 12, 2010 5:40 AM by Gregory Lafrance

    External Links

    Xismo1

      Hey everybody,

       

      I'm just trying to create my first simple app with adobe air. I followed one of the adobe tutorials which helped me setup the app with simple functionality like close and minimize button.

       

      What I need to do is import links on to the application screen. The links need to be located inside a txt file so people can change them. Is something like that possible? I tried putting an as3 code for it, but it doesn't seem to work, even tho if I create blank as3 document it works. Just not under Air document.

       

      Any help would be much appreciated.

        • 1. Re: External Links
          Gregory Lafrance Level 6

          If this post answers your question or helps, please mark it as such.


          Greg Lafrance - Flex 2 and 3 ACE certified

          www.ChikaraDev.com

          Flex / AIR Development, Training, and Support Services

           

          Here you go.

           

          Create a file in the users documents directory with this text:

           

          http://www.google.com
          http://www.yahoo.com
          http://www.youtube.com
          http://www.facebook.com

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" 
            applicationComplete="appCompleteHandler()">
            <mx:Script>
              <![CDATA[
                import mx.controls.LinkButton;
                import mx.controls.Alert;
                import flash.filesystem.*;
                import mx.events.*;
           
                private var currentFile:File;
                private var stream:FileStream = new FileStream();
                private var defaultDirectory:File;
                [Bindable] public var dataChanged:Boolean = false;
                private function appCompleteHandler():void {
                  defaultDirectory = File.documentsDirectory;
                  stage.nativeWindow.width = 500;
                  stage.nativeWindow.height = 400;
                  stage.nativeWindow.visible = true;
                }
                
                private function openFile():void { 
                  var fileChooser:File;
                  if (currentFile) {
                    fileChooser = currentFile;
                  }
                  else{
                    fileChooser = defaultDirectory;
                  }
                    fileChooser.browseForOpen("Open");
                    fileChooser.addEventListener(Event.SELECT, fileOpenSelected);
                }
                
                private function fileOpenSelected(event:Event):void 
                {
                  currentFile = event.target as File;
                  stream = new FileStream();
                  stream.openAsync(currentFile, FileMode.READ);
                  stream.addEventListener(Event.COMPLETE, fileReadHandler);
                  stream.addEventListener(IOErrorEvent.IO_ERROR, readIOErrorHandler);
                }
                
                private function fileReadHandler(event:Event):void 
                {
                  var str:String = stream.readUTFBytes(stream.bytesAvailable);
                  stream.close();
                  var lineEndPattern:RegExp = new RegExp(File.lineEnding, "g");
                  str = str.replace(lineEndPattern, "\n");
                  stream.close();
                  var arr:Array = str.split("\n");
                  for each(var line:String in arr){
                    var lb:LinkButton = new LinkButton();
                    lb.label = line;
                    lb.addEventListener(MouseEvent.CLICK, clickFunc);
                    vb.addChild(lb);
                  }
                }
                
                private function clickFunc(evt:MouseEvent):void{
                  navigateToURL(new URLRequest(evt.currentTarget.label),"_blank");
                }
                
                private function readIOErrorHandler(event:Event):void 
                {
                  mx.controls.Alert.show("Specified file cannot be opened.", "Error", Alert.OK, this);
                }
              ]]>
            </mx:Script>
            <mx:Button label="Open Links" id="openBtn" click="openFile()"/>
            <mx:VBox id="vb" width="100%" height="100%"/>
          </mx:WindowedApplication>