Adobe AIR2.0 NativeProcess使ってみました for Mac

2010年5月13日 木曜日

こんにちは、矢野です。

AIR2.0からローカルシェルスクリプトを実行できるってことで、
Macの「ditto」コマンドで、ファイルを圧縮できるか試してみました。
※ dittoコマンドでファイルを圧縮すると、Macのファイル情報であるリソースフォークを含んだ形で圧縮できるんです。
 

以下を参考に
AIR2.0 Bata 2 でNativeProcessつかってみる。Flash CS4つかって。

ハマったこと リリースビルドでエラー続出
application.xmlの追記と、ネイティブインストーラーが必須なことを知らず、
普通にリリースビルドしても全く動かずで、かなり格闘しました。
ネイティブインストーラーはWinならexe Macならdmgでインストーラーを作成しないと駄目なんですねぇ。

とりあえず、adtコマンドをターミナルから実行できるよう設定します。

1.AIR2.0のsdk内のbinにコマンドが格納されているので、シェルにパスを通します。
私は、zshなんで、.zsrenvに

CODE:
  1. export PATH=~/Applications/Adobe\ Flash\ Builder\ 4/sdks/4.0.0/bin

2.ターミナル再起動すると、「adt」コマンドが利用可能に!

3.圧縮するシェルスクリプトを用意
src/archive.sh

CODE:
  1. #!/bin/sh
  2. ditto -c -k "$@"

4.NativeProcessTest.mxml

CODE:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx :WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" horizontalAlign="left" creationComplete="init()">
  3.     </mx><mx :Script>
  4.         <![CDATA[
  5.             import flash.desktop.*;
  6.             import mx.controls.Alert;
  7.             private var process:NativeProcess;
  8.            
  9.             private function init():void{}
  10.            
  11.             public function dittoCommnd():void{
  12.                 if (NativeProcess.isSupported){
  13.                     // コマンドライン情報設定
  14.                     var info:NativeProcessStartupInfo = new NativeProcessStartupInfo();
  15.                     // 実行するプログラムを指定 相対パスの場合、flaのあるフォルダからのパス
  16.                     var shFile:File = File.applicationDirectory.resolvePath("archive.sh");
  17.                     info.executable = shFile;
  18.                    
  19.                     // 実行するプログラムに渡す引数
  20.                     var args:Vector.<String> = new Vector.<string>();
  21.                     //第1引数
  22.                     args.push("/Users/test/Pictures/hoge"); //圧縮するファイル達
  23.                     //第2引数
  24.                     args.push("/Users/test/Desktop/hoge.zip"); // 圧縮後のファイル名
  25.                     info.arguments = args;
  26.                    
  27.                     process = new NativeProcess();
  28.                     process.addEventListener(NativeProcessExitEvent.EXIT, onComplateHandler);
  29.                     process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
  30.                     process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onErrorData);
  31.                    
  32.                     process.start(info);
  33.                 }else{
  34.                     Alert.show("NativeProcess not supported.");
  35.                 }
  36.             }
  37.  
  38.             public function onComplateHandler(event:NativeProcessExitEvent):void {
  39.                 outPutTextArea.text = "Process exited with " + event.exitCode;
  40.                 Alert.show("Archive complete");
  41.             }
  42.  
  43.             public function onOutputData(event:ProgressEvent):void{
  44.                 trace("Got: ", process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable));
  45.                 outPutTextArea.text = process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable) + outPutTextArea.text;
  46.             }
  47.            
  48.             private function onErrorData(event:Event):void{
  49.                 Alert.show("ERROR");
  50.                 var bytes = event.currentTarget.standardOutput;
  51.                 outPutTextArea.text = bytes.readUTFBytes(bytes.bytesAvailable).toString();
  52.             }
  53.            
  54.         ]]>
  55.     </string></mx>
  56.     <mx :HBox width="100%">
  57.         <mx :Button label="圧縮" click="dittoCommnd()"/>
  58.     </mx>
  59.     <mx :TextInput width="100%" height="100%" editable="false" id="outPutTextArea"/>

5.NativeProcessTest-app.xml

CODE:
  1. <?xml version="1.0" encoding="utf-8" standalone="no"?>
  2. <application xmlns="http://ns.adobe.com/air/application/2.0beta2">
  3. // 以下のようにextendedDesktop desktopってしないとリリースビルドできません
  4. <supportedprofiles>extendedDesktop desktop</supportedprofiles>
  5.     <id>NativeProcessTest</id>
  6.     <filename>NativeProcessTest</filename>
  7.     <name>NativeProcessTest</name>
  8.     <version>v1</version>
  9.     <initialwindow>
  10.         <!-- The main SWF or HTML file of the application. Required. -->
  11.         <!-- Note: In Flash Builder, the SWF reference is set automatically. -->
  12.         <content>[この値は Flash Builder の出力ファイル app.xml に上書きされます。]</content>
  13.        
  14.         <!-- The title of the main window. Optional. -->
  15.         <!-- <title> -->
  16.  
  17.         <!-- The type of system chrome to use (either "standard" or "none"). Optional. Default standard. -->
  18.         <!-- <systemChrome> -->
  19.  
  20.         <!-- Whether the window is transparent. Only applicable when systemChrome is none. Optional. Default false. -->
  21.         <!-- <transparent> -->
  22.  
  23.         <!-- Whether the window is initially visible. Optional. Default false. -->
  24.         <!-- <visible> -->
  25.  
  26.         <!-- Whether the user can minimize the window. Optional. Default true. -->
  27.         <!-- <minimizable> -->
  28.  
  29.         <!-- Whether the user can maximize the window. Optional. Default true. -->
  30.         <!-- <maximizable> -->
  31.  
  32.         <!-- Whether the user can resize the window. Optional. Default true. -->
  33.         <!-- <resizable> -->
  34.  
  35.         <!-- The window's initial width in pixels. Optional. -->
  36.         <!-- <width> -->
  37.         <!-- The window's initial height in pixels. Optional. -->
  38.         <!-- <height> -->
  39.  
  40.         <!-- The window's initial x position. Optional. -->
  41.         <!-- <x> -->
  42.         <!-- The window's initial y position. Optional. -->
  43.         <!-- <y> -->
  44.  
  45.         <!-- The window's minimum size, specified as a width/height pair in pixels, such as "400 200". Optional. -->
  46.         <!-- <minSize> -->
  47.         <!-- The window's initial maximum size, specified as a width/height pair in pixels, such as "1600 1200". Optional. -->
  48.         <!-- <maxSize> -->
  49.     </initialwindow>
  50.  
  51. </application>

6.リリースビルド
普通にキーストアを仕様してリリースビルドするだけでOKです。

7.インストーラーパッケージ化(dmg作成)
ターミナルより、リリースビルドファイルの場所まで移動し、以下、adtコマンド実行

CODE:
  1. adt -package -target native NativeProcessTest.dmg NativeProcessTest.air

NativeProcess.mxml 22・23行目あたり

CODE:
  1. //第1引数
  2. args.push("/Users/test/Pictures/hoge"); //圧縮するファイル達
  3. //第2引数
  4. args.push("/Users/test/Desktop/hoge.zip"); // 圧縮後のファイル名

ドラッグ&ドロップでローカルのパスをとるとを圧縮ソフトができそうな感じです!

コメントをどうぞ