﻿/**************************************************************************
*
*  @@@BUILDINFO@@@ acrobat-8.jsx 1.0.0.0  21-August-2006
*  Copyright 2006-2007 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains the property of
* Adobe Systems Incorporated  and its suppliers,  if any.  The intellectual 
* and technical concepts contained herein are proprietary to  Adobe Systems 
* Incorporated  and its suppliers  and may be  covered by U.S.  and Foreign 
* Patents,patents in process,and are protected by trade secret or copyright 
* law.  Dissemination of this  information or reproduction of this material
* is strictly  forbidden  unless prior written permission is  obtained from 
* Adobe Systems Incorporated.
**************************************************************************/

/*
@@@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>Adobe Acrobat 8.0</dc:title>
     <dc:description>This script enables other applications to communicate with Adobe Acrobat 8.0.</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="fr_FR">
     <dc:title>Adobe Acrobat 8.0</dc:title>
     <dc:description>Ce script permet à d'autres applications de communiquer avec Adobe Acrobat 8.0.</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="ja_JP">
     <dc:title>Adobe Acrobat 8.0</dc:title>
     <dc:description>このスクリプトは、他のアプリケーションと Adobe Acrobat 8.0 との通信を有効にします。</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="de_DE">
     <dc:title>Adobe Acrobat 8.0</dc:title>
     <dc:description>Mithilfe dieses Skripts können andere Anwendungen mit Adobe Acrobat 8.0 kommunizieren.</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="it_IT">
     <dc:title>Adobe Acrobat 8.0</dc:title>
     <dc:description>Questo script consente ad altre applicazioni di comunicare con Adobe Acrobat 8.0</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="es_ES">
     <dc:title>Adobe Acrobat 8.0</dc:title>
     <dc:description>Este script posibilita que otras aplicaciones se comuniquen con Adobe Acrobat 8.0</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="nl_NL">
     <dc:title>Adobe Acrobat 8.0</dc:title>
     <dc:description>Dit script laat andere toepassingen toe te communiceren met Adobe Acrobat 8.0</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="pt_BR">
     <dc:title>Adobe Acrobat 8.0</dc:title>
     <dc:description>Este script permite que outros aplicativos se comuniquem com o Adobe Acrobat 8.0</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="nb_NO">
     <dc:title>Adobe Acrobat 8.0</dc:title>
     <dc:description>Skriptet gjør at andre programmer kan kommunisere med Adobe Acrobat 8.0</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="da_DA">
     <dc:title>Adobe Acrobat 8.0</dc:title>
     <dc:description>Dette script betyder, at andre programmer kan kommunikere med Adobe Acrobat 8.0</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="fi_FI">
     <dc:title>Adobe Acrobat 8.0</dc:title>
     <dc:description>Tämän komentosarjan avulla muut sovellukset ja Adobe Acrobat 8.0 voivat kommunikoida keskenään</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="se_SE">
     <dc:title>Adobe Acrobat 8.0</dc:title>
     <dc:description>Det här skriptet gör det möjligt för andra program att kommunicera med Adobe Acrobat 8.0</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="zh_TW">
     <dc:title>Adobe Acrobat 8.0</dc:title>
     <dc:description>"此指令碼能讓其他應用程式與 Adobe Acrobat 8.0 進行通訊。</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="zh_CN">
     <dc:title>Adobe Acrobat 8.0</dc:title>
     <dc:description>"此脚本使其它应用程序能够与 Adobe Acrobat 8.0 进行通信</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="ko_KO">
     <dc:title>Adobe Acrobat 8.0</dc:title>
     <dc:description>"이 스크립트를 사용하면 다른 응용 프로그램에서 Adobe Acrobat 8.0과(와) 통신할 수 있습니다.</dc:description>
</ScriptInfo>
@@@END_XML@@@
*/

if (typeof acrobat == "undefined")
{
	var acrobat = new Object;
	acrobat.appSpec = "acrobat-8.0";

//-----------------------------------------------------------------
// This routine tries to return an array of File objects created
// from the 'files' argument. It will convert a single File object
// or a single string into an array of File objects, or it will
// convert an array of string and File objects into an array of
// file objects.
//-----------------------------------------------------------------
	acrobat.ExtractFileArray = function (files)
	{
		var fileArray = new Array;
		
		// If it isn't an array, make it a length one array.
		if (!(files instanceof Array))
			files = new Array (files);
		
		// Turn each item in the array into a File, or remove it.
		for (index = 0; index < files.length; ++index)
		{
			var file = files[index];
			
			if (file instanceof File)
				fileArray.push (file);
			else if (typeof file == 'string')
				fileArray.push (File (file));
			else
			{
				// do nothing
			}
		}
		return fileArray;
	}

/**
 * Open a file (or files) in Acrobat.
 * files may be a single file, or a list of files.
 * A file is represented by a JavaScript 'File' object
 * or a string that is the file path
 */
	acrobat.open = function( files )
	{
		var fileArray = acrobat.ExtractFileArray ( files );
			
		var script = "";
		for (index = 0; index < fileArray.length; ++index)
		{
			var file = fileArray[index];
			script += "app.openDoc('" + file.fsName + "'); ";
		}
		acrobat.executeScript(script);
		BridgeTalk.bringToFront (acrobat.appSpec);
	}

/**
 * format the passed in value so we can pass it in a script
 * This means putting quote marks around a string and escaping
 * any single quote the string contains.
 */
	acrobat.format = function(value)
	{
		var valtype = typeof value;
		if(valtype=="string") {
			return "'" + value.replace (/'/g, "\\'") + "'";
		}
		return value;
	}

/**
 * Execute a JavaScript in Acrobat,
 * using Acrobat's EScript engine
 */
	acrobat.executeScript = function(script)
	{
		var bt = new BridgeTalk;
		bt.target = acrobat.appSpec;
		bt.body = script;
	/*	bt.onResult = function(btobj) {
			print("> '" + btobj.body + "'");
		}
		bt.onError = function(errmsg) {
			print("ERROR");
			var errcode = parseInt(errmsg.headers["Error-Code"]);
			print(errcode);
			print(errmsg.body);
		}
	*/	return bt.send();
	}

/**
 * Reveal the active document in Bridge.
 */
	acrobat.revealInBridge = function()
	{
		acrobat.executeScript("app.execMenuItem('ADBE:BridgeMenuName')");
	}

/**
 * Create a new file in Acrobat.
 * Takes two optional parameters--nHeight, nWidth
 * the height and width of the new document in points.
 * To call with optional parameter use the syntax:
 *	acrobat.openAsNew({ nWidth: 600, nHeight: 700 });
 */
	acrobat.openAsNew = function()
	{
		var script = "app.newDoc(";
		var argcnt = 0;
		for(index = 0; index < arguments.length; ++index)	{
			var arglist = arguments[index];
			for(var arg in arglist)	{
				var val = arglist[arg];
				if(arg=="nWidth" || arg=="nHeight") {
					script += (argcnt==0) ? "{ ": ", ";
					script += arg + ": " + acrobat.format(val);
					argcnt++;
				}
			}
		}
		if(0<argcnt) script += "}";
		script += ")";
		
		var ok = acrobat.executeScript(script);
		if (ok)
			BridgeTalk.bringToFront (acrobat.appSpec);
		// Return true - the spec says to return true if a file
		// was opend, but the call is asynchronous, so we can just
		// return the status of the send() call.
		return ok;
	}

/**
 * Takes a file (or array of files)
 * and calls Acrobat's print function on them
 */
	acrobat.print = function( files )
	{
		var fileArray = acrobat.ExtractFileArray ( files );
			
		var script = "";
		for (index = 0; index < fileArray.length; ++index)
		{
			var file = fileArray[index];
			script += "var mydoc = app.openDoc('" + file.fsName + "'); mydoc.print(); ";
		}
		acrobat.executeScript(script);
	}

/**
 * Call Acrobat's quit function.
 * This is just a place holder
 * because Acrobat's security model doesn't allow calling quit.
 */
	acrobat.quit = function()
	{
		// acrobat doesn't allow you to quit
		acrobat.executeScript("app.execMenuItem('Quit');");
	}

/**
 * Bring the specified file (or array of files)
 * to the top.
 * (BridgeTalk.bringToFront doesn't seem to work for Acrobat.)
 */
	acrobat.reveal = function( files )
	{
		var fileArray = acrobat.ExtractFileArray ( files );
			
		var script = "";
		for (index = 0; index < fileArray.length; ++index)
		{
			var file = fileArray[index];
			script += "var mydoc = app.openDoc('" + file.fsName + "'); ";
		}
		acrobat.executeScript(script);
		BridgeTalk.bringToFront (acrobat.appSpec);
	}
}
