The following example shows how you can detect the line ending character sequence used by the host operating system in Adobe AIR by using the static File.lineEnding property.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://airexamples.com/2010/03/13/detecting-the-line-ending-character-sequence-used-by-the-host-operating-system-in-adobe-air/ -->
<mx:WindowedApplication name="File_lineEnding_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        status="{Capabilities.os}"
        height="200">
 
    <mx:Script>
        <![CDATA[
            private function init():void {
                var arr:Array = File.lineEnding.split("");
                arr.forEach(charCodeMe);
                lbl.text = arr.join(",");
            }
 
            private function charCodeMe(item:*, index:int, array:Array):void {
                array[index] = "0x" + item.charCodeAt(0).toString();
            }
        ]]>
    </mx:Script>
 
    <mx:VBox>
        <mx:Label text="File.lineEnding:" />
        <mx:Text id="lbl"
                selectable="true"
                fontSize="48"
                creationComplete="init();" />
    </mx:VBox>
 
</mx:WindowedApplication>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

<?xml version="1.0" encoding="utf-8"?>
<!--  -->
<mx:WindowedApplication name="File_lineEnding_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        status="{Capabilities.os}"
        height="200"
        initialize="init();">
 
    <mx:Script>
        <![CDATA[
            import mx.containers.VBox;
            import mx.controls.Label;
 
            private function init():void {
                var tmpLbl:Label = new Label();
                tmpLbl.text = "File.lineEnding:";
 
                var lbl:Label = new Label();
                lbl.selectable = true;
                lbl.setStyle("fontSize", 48);
 
                var vBox:VBox = new VBox();
                vBox.addChild(tmpLbl);
                vBox.addChild(lbl);
                addChild(vBox);
 
                var arr:Array = File.lineEnding.split("");
                arr.forEach(charCodeMe);
                lbl.text = arr.join(",");
            }
 
            private function charCodeMe(item:*, index:int, array:Array):void {
                array[index] = "0x" + item.charCodeAt(0).toString();
            }
        ]]>
    </mx:Script>
 
</mx:WindowedApplication>
Tagged with:
 

Leave a Reply