いてづきブログ

情シスでやってみたことの備忘録

AIRからexeファイルを実行する

タイトルのとおり。
以下、サンプルコード。

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">


<fx:Script>
<![CDATA[
private var process:NativeProcess = new NativeProcess();

//メモ帳で開くtxtファイルのパスを取得
protected function getTxtPath(event:MouseEvent):void{
var f:File = new File();

f.addEventListener(Event.SELECT,function(event:Event):void{
txtPath.text = (event.target as File).nativePath;
});

f.browseForOpen("txtファイルを選択",[new FileFilter("txtファイル","*.txt")]);

}

//メモ帳を開く
protected function executeNotepad(event:MouseEvent):void{
if(!process.running){

var info:NativeProcessStartupInfo = new NativeProcessStartupInfo();
info.executable = File.applicationDirectory.resolvePath("C:\\WINDOWS\\system32\\notepad.exe");

if(txtPath.text != null && txtPath.text != ""){
info.arguments.push(txtPath.text);
}

process.start(info);
}
}

//メモ帳を終了する
protected function exitNotepad(event:MouseEvent):void{
process.exit(true);
}

]]>

</fx:Script>

<s:BorderContainer>
<s:layout>
<s:VerticalLayout gap="5"/>
</s:layout>

<s:HGroup gap="5">
<s:TextInput id="txtPath"/>
<s:Button label="txtファイル選択" click="getTxtPath(event)"/>
</s:HGroup>

<s:Button label="メモ帳起動" click="executeNotepad(event)"/>

<s:Button label="メモ帳終了" click="exitNotepad(event)"/>

</s:BorderContainer>


</s:WindowedApplication>


txtファイルを指定しない場合、新規ファイルが開かれる。

また、NativeProcessを有効にするために、app.xmlに以下の記述を追加する。

<supportedProfiles>extendedDesktop desktop</supportedProfiles>


以下を参考にさせていただきました。
http://d.hatena.ne.jp/qkrhn081/20100723/1279850681