//#!open -a /Applications/Utilities/Adobe\ Utilities.localized/ExtendScript\ Toolkit\ 2/ExtendScript\ Toolkit\ 2.app
// vim:ft=javascript:

#target bridge

/*
@@@START_XML@@@
<?xml version="1.0" encoding="UTF-8"?>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="en_US">
	 <dc:title>CS3 Shell</dc:title>
	 <dc:description>This script is for CS3 interactive shell.</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="ja_JP">
	<dc:title>CS3 Shell</dc:title>
	<dc:description>このスクリプトは、インタラクティブシェル用のものです。
	外部スクリプトとあわせて使用します。</dc:description>
</ScriptInfo>
@@@END_XML@@@
*/

global = this;

BridgeTalk.onReceive = function (bt) {
	return (function () { return eval(bt.body) }).apply(global);
};

try {

if (BridgeTalk.appName == "bridge") {

	var fileIn  = new File("/tmp/cs3-command-in");
	var fileOut = new File("/tmp/cs3-command-out");

	var target = "photoshop";

	function response(res) {
		if (res === "") res = '""';
		fileOut.open("w");
		fileOut.write(res);
		fileOut.close();
	}

	function request() {
		fileIn.open("r");
		var ret = fileIn.read();
		fileIn.close();
		fileIn.open("w");
		fileIn.close();
		return ret;
	}

	function evaluate () {
		try {
			if (code = request()) {
				var targetApp = BridgeTalk.getSpecifier(target);
				var bt = new BridgeTalk();
				bt.target = targetApp;
				// bt.headers.Command = "Eval";
				bt.body = code;
				bt.onResult = function (e) {
					response(e.body);
				};
				bt.onError = function (e) {
					response(e.body);
				};
				bt.send();
			}
		} catch (e) {
			response(e);
		}
		app.scheduleTask("evaluate();", 10, false);
	}

	evaluate();
}

} catch (e) {
	alert(e);
}

