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に
export PATH=~/Applications/Adobe\ Flash\ Builder\ 4/sdks/4.0.0/bin

2.ターミナル再起動すると、「adt」コマンドが利用可能に!
3.圧縮するシェルスクリプトを用意
src/archive.sh
#!/bin/sh
ditto -c -k "$@"

4.NativeProcessTest.mxml
< ?xml version="1.0" encoding="utf-8"?>
<mx :WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" horizontalAlign="left" creationComplete="init()">
	</mx><mx :Script>
		< ![CDATA[
			import flash.desktop.*;
			import mx.controls.Alert;
			private var process:NativeProcess;
			
			private function init():void{}
			
			public function dittoCommnd():void{
				if (NativeProcess.isSupported){
					// コマンドライン情報設定
					var info:NativeProcessStartupInfo = new NativeProcessStartupInfo();
					// 実行するプログラムを指定 相対パスの場合、flaのあるフォルダからのパス
					var shFile:File = File.applicationDirectory.resolvePath("archive.sh");
					info.executable = shFile;
					
					// 実行するプログラムに渡す引数
					var args:Vector.<String> = new Vector.<string>();
					//第1引数
					args.push("/Users/test/Pictures/hoge"); //圧縮するファイル達
					//第2引数
					args.push("/Users/test/Desktop/hoge.zip"); // 圧縮後のファイル名
					info.arguments = args;
					
					process = new NativeProcess();
					process.addEventListener(NativeProcessExitEvent.EXIT, onComplateHandler);
					process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
					process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onErrorData);
					
					process.start(info);
				}else{
					Alert.show("NativeProcess not supported.");
				}
			}

			public function onComplateHandler(event:NativeProcessExitEvent):void {
				outPutTextArea.text = "Process exited with " + event.exitCode;
				Alert.show("Archive complete");
			}

			public function onOutputData(event:ProgressEvent):void{
				trace("Got: ", process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable));
				outPutTextArea.text = process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable) + outPutTextArea.text;
			}
			
			private function onErrorData(event:Event):void{
				Alert.show("ERROR");
				var bytes = event.currentTarget.standardOutput;
				outPutTextArea.text = bytes.readUTFBytes(bytes.bytesAvailable).toString();
			}
			
		]]>
	</string></mx>
	<mx :HBox width="100%">
		<mx :Button label="圧縮" click="dittoCommnd()"/>
	</mx>
	<mx :TextInput width="100%" height="100%" editable="false" id="outPutTextArea"/>
 


5.NativeProcessTest-app.xml
< ?xml version="1.0" encoding="utf-8" standalone="no"?>
<application xmlns="http://ns.adobe.com/air/application/2.0beta2">
// 以下のようにextendedDesktop desktopってしないとリリースビルドできません
<supportedprofiles>extendedDesktop desktop</supportedprofiles>
	<id>NativeProcessTest</id>
	<filename>NativeProcessTest</filename>
	<name>NativeProcessTest</name>
	<version>v1</version>
	<initialwindow>
		<!-- The main SWF or HTML file of the application. Required. -->
		<!-- Note: In Flash Builder, the SWF reference is set automatically. -->
		<content>[この値は Flash Builder の出力ファイル app.xml に上書きされます。]</content>
		
		<!-- The title of the main window. Optional. -->
		<!-- <title> -->

		<!-- The type of system chrome to use (either "standard" or "none"). Optional. Default standard. -->
		<!-- <systemChrome> -->

		<!-- Whether the window is transparent. Only applicable when systemChrome is none. Optional. Default false. -->
		<!-- <transparent> -->

		<!-- Whether the window is initially visible. Optional. Default false. -->
		<!-- <visible> -->

		<!-- Whether the user can minimize the window. Optional. Default true. -->
		<!-- <minimizable> -->

		<!-- Whether the user can maximize the window. Optional. Default true. -->
		<!-- <maximizable> -->

		<!-- Whether the user can resize the window. Optional. Default true. -->
		<!-- <resizable> -->

		<!-- The window's initial width in pixels. Optional. -->
		<!-- <width> -->

		<!-- The window's initial height in pixels. Optional. -->
		<!-- <height> -->

		<!-- The window's initial x position. Optional. -->
		<!-- <x> -->

		<!-- The window's initial y position. Optional. -->
		<!-- <y> -->

		<!-- The window's minimum size, specified as a width/height pair in pixels, such as "400 200". Optional. -->
		<!-- <minSize> -->

		<!-- The window's initial maximum size, specified as a width/height pair in pixels, such as "1600 1200". Optional. -->
		<!-- <maxSize> -->
	</initialwindow>

</application>


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

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


NativeProcess.mxml 22・23行目あたり
//第1引数
args.push("/Users/test/Pictures/hoge"); //圧縮するファイル達
//第2引数
args.push("/Users/test/Desktop/hoge.zip"); // 圧縮後のファイル名

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

コメントをどうぞ