﻿/*
@@@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 Encore CS6</dc:title>
     <dc:description>This script enables other applications to communicate with Adobe Encore.</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="fr_FR">
     <dc:title>Adobe Encore CS6</dc:title>
     <dc:description>Ce script permet à d'autres applications de communiquer avec Adobe Encore.</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="ja_JP">
     <dc:title>Adobe Encore CS6</dc:title>
     <dc:description>このスクリプトは、他のアプリケーションと Adobe Encore との通信を有効にします。</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="de_DE">
     <dc:title>Adobe Encore CS6</dc:title>
     <dc:description>Mithilfe dieses Skripts können andere Anwendungen mit Adobe Encore kommunizieren.</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="it_IT">
     <dc:title>Adobe Encore CS6</dc:title>
     <dc:description>Questo script consente ad altre applicazioni di comunicare con Adobe Encore</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="es_ES">
     <dc:title>Adobe Encore CS6</dc:title>
     <dc:description>Este script posibilita que otras aplicaciones se comuniquen con Adobe Encore</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="ko_KR">
     <dc:title>Adobe Encore CS6</dc:title>
     <dc:description>이 스크립트를 사용하면 다른 응용 프로그램이 Adobe Encore와 통신할 수 있게 됩니다.</dc:description>
</ScriptInfo>
@@@END_XML@@@
*/

encore6 = {};

encore6.encoreTargetName = "encore-6.0";
encore6.encoreAppName = "encore";
encore6.encoreVersion = "6.0";
encore6.BridgeTargetName = "bridge";


/**
**	Open Event
**
**	The event is:
**		type - document
**		location - document
**		object - document
**
**	To get the selection use object.selections.
**
*/
encore6.eventHandler = function(event)
{
	var retval = {};
	retval.handled = false;

	if ( event.type == "open" &&
		event.location == "document" &&
		event.object.constructor.name == "Document" &&
		app.document.owner == encore6.encoreAppName)
	{
		var sels = event.object.selections;
		var files = [];
		for (var i = 0; i < sels.length; i++)
		{
			files[i] = sels[i].spec;
		}
		// hmm, even though open project works ok now via
		// scripting, when the welcome dialog is up, scripting is not
		// called -- so changing this to suss out if trying to open a
		// project and then not handle it.  Really shouldn't do this code
		// here so it is redundant in open
		//	getextension of first file
		var	ext = encore6.getExtension(files[0].name);
		var	isNCOR = (ext == ".ncor");

		if ((files.length > 1) || !isNCOR) {
			BridgeTalk.bringToFront(encore6.encoreAppName);
			encore6.open(files);
			retval.handled = true;
		}
	}

	return retval;
}


encore6.getExtension = function(filename)
{
	var	dot_index = filename.lastIndexOf('.');
	
	return filename.substring(dot_index);
}


/**
**	open
**
**	Open the passed in files. This may be one or many files.
**  This doesn't really follow the approved form, but since we're not
**  actually supporting scripting Encore this version, and it works well
**  from bridge, excluding the bits to support executing scripts in Encore
**  allows me to test all the scripts here, and feels a little more correct to
**  me.  This will need to be changed for future versions of Encore to support
**  scripting of Encore.
**
*/	

encore6.open = function(target)
{
	if (BridgeTalk.appName == encore6.encoreAppName && 
	    BridgeTalk.appVersion == encore6.encoreVersion) {
		alert("Should never get here");
	}
	else {
		var btMessage = new BridgeTalk;
		btMessage.target = encore6.encoreTargetName;

		//	getextension of first file
		var	ext = encore6.getExtension(target[0].name).toLowerCase();

		var	isNCOR = (ext == ".ncor");
		var	isPSD = true;
		
		// if they're all psds, treat as menus
		for (var index = 0; index < target.length; index++)
    	{
			ext = encore6.getExtension(target[index].name).toLowerCase();
  			if (ext != ".psd")
  				isPSD = false;
  		}

		// if more than one file not all or psds, or a single non-ncor, non-psd
		if ( ((target.length > 1) && !isPSD) || (!isNCOR && !isPSD)) {
			// treat as open files
			var arrayString = encore6.fileArrayToString (target);
			btMessage.body = "app.project.importFile(" + arrayString + ")";
			btMessage.send();
		}
		else if (isNCOR) {
			// single ncor file, open project
			// passing as File handles spaces in names well, passing as string doesn't
			btMessage.body = "app.open(File('" + target[0].absoluteURI.toString() + "'));";
			btMessage.send();
		}
		else { 
			// one or more psds, open as menus
			var arrayString = encore6.fileArrayToString (target);
			btMessage.body = "app.project.importMenu(" + arrayString + ")";
			btMessage.send();
		}
	}
}

/**
**	quit
**
**	Performs the equivalent of File>Exit.
*/
encore6.quit = function()
{
	if (BridgeTalk.appName == encore6.encoreTargetName)
	{
		app.quit();
	}
	else
	{
		encore6.executeScript("app.quit();");
	}
}

/**
**	fileArrayToString
**
**	This routine create a string for the files array that we can transmit
**	over BridgeTalk as text, like this:
**
**	Array (File ('path1'), File ('path2'), File ('path3'))
*/
encore6.fileArrayToString = function (files)
{
    var filesString = new String ("Array (");
    
    for (var index = 0; index < files.length; index++)
    {
        if (index > 0)
        {
			filesString += ", ";
	}
    
        filesString += "File ('" + files[index].absoluteURI.toString() + "')";
    }
    
    // close the Array
    filesString += ")";
    
    return filesString;
}



if (BridgeTalk.appName == encore6.BridgeTargetName)
{
	// install open handler, only does something if we are the daddy
	app.eventHandlers.push( { handler:encore6.eventHandler } );

	// install our menu options, if we're installed 
	//	if (app.document.owner == encore6.encoreAppName)
	{
    	var openString = "Open in Adobe Encore As";
		var assetString = "Asset";
		var menuString = "Menu";
		var timelineString = "Timeline";
		var slideshowString = "Slideshow";
		
		var noSelection = "No files selected";
		if (app.locale == "ja_JP")
		{
			openString = "Adobe Encore で読み込む";
			assetString = "アセット";
			menuString = "メニュー";
			timelineString = "タイムライン";
			slideshowString = "スライドショー";
		}
		else if (app.locale == "de_DE")
		{
			openString = "In Adobe Encore öffnen als";
			assetString = "Asset";
			menuString = "Menü";
			timelineString = "Schnittfenster";
			slideshowString = "Bildschirmpräsentation";
		}
		else if (app.locale == "es_ES")
		{
			openString = "Abrir en Adobe Encore como";
			assetString = "Recurso";
			menuString = "Menú";
			timelineString = "Línea de tiempo";
			slideshowString = "Presentación";
		}
		else if (app.locale == "fr_FR")
		{
			openString = "Ouvrir dans Adobe Encore comme";
			assetString = "Elément";
			menuString = "Menu";
			timelineString = "Montage";
			slideshowString = "Diaporama";
		}
		else if (app.locale == "it_IT")
		{
			openString = "Apri in Adobe Encore in formato";
			assetString = "Risorsa";
			menuString = "Menu";
			timelineString = "Timeline";
			slideshowString = "Presentazione";
		}
		else if (app.locale == "ko_KR")
		{
			openString = "Adobe Encore에서 열기";
			assetString = "에셋";
			menuString = "메뉴";
			timelineString = "타임라인";
			slideshowString = "슬라이드 쇼";
		}
		
		MenuElement.create ('menu', openString, 'after Open', 'submenu/openinencore' );

		var assetCommand = MenuElement.create ('command', assetString, 'at the end of submenu/openinencore', 'encoreasset' );

		assetCommand.onSelect = function () 
		{ 
			BridgeTalk.bringToFront(encore6.encoreTargetName);
			var btMessage = new BridgeTalk;
			btMessage.target = encore6.encoreTargetName;

			var sels = app.document.selections;
			var files = [];

			if (sels.length > 0) {
			
				for (var i = 0; i < sels.length; i++)
				{
					files[i] = sels[i].spec;
 				}

				var arrayString = encore6.fileArrayToString (files);

				btMessage.body = "app.project.importFile(" + arrayString + ")";
				btMessage.send();
			}
			else // should never get here since only enables with selection
				alert(noSelection);
		}
		assetCommand.onDisplay = function()
		{
			var sels = app.document.selections;
			var files = [];

			if (sels.length > 0) {
				assetCommand.enabled = true;
			}
			else
				assetCommand.enabled = false;
		}


		var menuCommand = MenuElement.create ('command', menuString, 'after encoreasset', 'encoremenu' );
		menuCommand.onSelect = function () 
		{ 
			BridgeTalk.bringToFront(encore6.encoreTargetName);
			var btMessage = new BridgeTalk;
			btMessage.target = encore6.encoreTargetName;

			var sels = app.document.selections;
			var files = [];

			if (sels.length > 0) {
			
				for (var i = 0; i < sels.length; i++)
				{
					files[i] = sels[i].spec;
 				}

				var arrayString = encore6.fileArrayToString (files);

				btMessage.body = "app.project.importMenu(" + arrayString + ")";
				btMessage.send();
			}
			else  // should never get here since only enables with selection
				alert(noSelection);
		}
		menuCommand.onDisplay = function()
		{
			var sels = app.document.selections;
			var files = [];

			if (sels.length > 0) {
				menuCommand.enabled = true;
			}
			else
				menuCommand.enabled = false;
		}

		var tlCommand = MenuElement.create ('command',timelineString, 'after encoremenu', 'encoretimeline' );
		tlCommand.onSelect = function () 
		{ 
			BridgeTalk.bringToFront(encore6.encoreTargetName);
			var btMessage = new BridgeTalk;
			btMessage.target = encore6.encoreTargetName;

			var sels = app.document.selections;
			var files = [];

			if (sels.length > 0) {
			
				for (var i = 0; i < sels.length; i++)
				{
					files[i] = sels[i].spec;
 				}

				var arrayString = encore6.fileArrayToString (files);

				btMessage.body = "app.project.importTimeline(" + arrayString + ")";
				btMessage.send();
			}
			else // should never get here since only enables with selection
				alert(noSelection);
		}
		tlCommand.onDisplay = function()
		{
			var sels = app.document.selections;
			var files = [];

			if (sels.length > 0) {
				tlCommand.enabled = true;
			}
			else
				tlCommand.enabled = false;
		}

		var ssCommand = MenuElement.create ('command', slideshowString, 'after encoretimeline', 'encoreslideshow' );
		ssCommand.onSelect = function () 
		{ 
			BridgeTalk.bringToFront(encore6.encoreTargetName);
			var btMessage = new BridgeTalk;
			btMessage.target = encore6.encoreTargetName;

			var sels = app.document.selections;
			var files = [];

			if (sels.length > 0) {
			
				for (var i = 0; i < sels.length; i++)
				{
					files[i] = sels[i].spec;
 				}

				var arrayString = encore6.fileArrayToString (files);

				btMessage.body = "app.project.importSlideshow(" + arrayString + ")";
				btMessage.send();
			}
			else // should never get here since only enables with selection
				alert(noSelection);
		}
		ssCommand.onDisplay = function()
		{
			var sels = app.document.selections;
			var files = [];

			if (sels.length > 0) {
				ssCommand.enabled = true;
			}
			else
				ssCommand.enabled = false;
		}

	}
}

