﻿/*
@@@BUILDINFO@@@ contribute-6.1.jsx 1.0.1
*/

/*************************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
*  Copyright 2006 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 Contribute CS5.1</dc:title>
     <dc:description>This script enables other applications to communicate with Adobe Contribute.</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="fr_FR">
     <dc:title>Adobe Contribute CS5.1</dc:title>
     <dc:description>Ce script permet à d'autres applications de communiquer avec Adobe Contribute.</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="ja_JP">
     <dc:title>Adobe Contribute CS5.1</dc:title>
     <dc:description>このスクリプトは、他のアプリケーションと Adobe Contribute との通信を有効にします。</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="de_DE">
     <dc:title>Adobe Contribute CS5.1</dc:title>
     <dc:description>Mithilfe dieses Skripts können andere Anwendungen mit Adobe Contribute kommunizieren.</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="it_IT">
     <dc:title>Adobe Contribute CS5.1</dc:title>
     <dc:description>Questo script consente ad altre applicazioni di comunicare con Adobe Contribute</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="es_ES">
     <dc:title>Adobe Contribute CS5.1</dc:title>
     <dc:description>Este script posibilita que otras aplicaciones se comuniquen con Adobe Contribute</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="nl_NL">
     <dc:title>Adobe Contribute CS5.1</dc:title>
     <dc:description>Dit script laat andere toepassingen toe te communiceren met Adobe Contribute</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="pt_BR">
     <dc:title>Adobe Contribute CS5.1</dc:title>
     <dc:description>Este script permite que outros aplicativos se comuniquem com o Adobe Contribute</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="nb_NO">
     <dc:title>Adobe Contribute CS5.1</dc:title>
     <dc:description>Skriptet gjør at andre programmer kan kommunisere med Adobe Contribute</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="da_DA">
     <dc:title>Adobe Contribute CS5.1</dc:title>
     <dc:description>Dette script betyder, at andre programmer kan kommunikere med Adobe Contribute</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="fi_FI">
     <dc:title>Adobe Contribute CS5.1</dc:title>
     <dc:description>Tämän komentosarjan avulla muut sovellukset ja Adobe Contribute voivat kommunikoida keskenään</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="se_SE">
     <dc:title>Adobe Contribute CS5.1</dc:title>
     <dc:description>Det här skriptet gör det möjligt för andra program att kommunicera med Adobe Contribute</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="zh_TW">
     <dc:title>Adobe Contribute CS5.1</dc:title>
     <dc:description>"此指令碼能讓其他應用程式與 Adobe Contribute 進行通訊。</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="zh_CN">
     <dc:title>Adobe Contribute CS5.1</dc:title>
     <dc:description>"此脚本使其它应用程序能够与 Adobe Contribute 进行通信</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="ko_KO">
     <dc:title>Adobe Contribute CS5.1</dc:title>
     <dc:description>"이 스크립트를 사용하면 다른 응용 프로그램에서 Adobe Contribute 과(와) 통신할 수 있습니다.</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="cs_cz">
     <dc:title>Adobe Contribute CS5.1</dc:title>
     <dc:description>Tento skript umožní jiným aplikacím komunikovat s Adobe Contribute.</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="pl_pl">
     <dc:title>Adobe Contribute CS5.1</dc:title>
     <dc:description>Ten skrypt umożliwia innym aplikacjom komunikowanie się z programem Adobe Contribute.</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="ru_ru">
     <dc:title>Adobe Contribute CS5.1</dc:title>
     <dc:description>Этот сценарий позволяет другим приложениям обмениваться данными с Adobe Contribute.</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="tr_tr">
     <dc:title>Adobe Contribute CS5.1</dc:title>
     <dc:description>Bu komut dosyası, diğer uygulamaların Adobe Contribute ile iletişim kurabilmesini sağlar.</dc:description>
</ScriptInfo>
@@@END_XML@@@
*/

//--------------------------------------------------------------------
// CONTRIBUTE COMMON INTERFACE
//--------------------------------------------------------------------

var Contribute = new Object();
var contribute = Contribute;	// Also support
var ct = Contribute;	// Also support

$.localize = true;

//---------------------------------------------------------
// executeScript ()
//	Lets CT execute the specified JavaScript. 
//	The entire document object model (DOM) of the target application is available to the script.
//
ct.executeScript = function ( script )
{   	
	var bt = new BridgeTalk;
	bt.target = 'contribute';
	bt.body = script;
	bt.send();
	BridgeTalk.bringToFront("contribute");
}

//---------------------------------------------------------
// quit()
//	Performs the equivalent of CT's File > Exit or File > Quit command.
//
ct.quit = function ()
{
	if (BridgeTalk.isRunning('contribute')) // First check if CT is open 
	{
	    BridgeTalk.bringToFront("contribute");
		ct.executeScript("dw.quitApplication(true);");		
	}
}

//---------------------------------------------------------
// openAsNew(fromTemplate)
//	Performs the equivalent of CT's File > New command.
//
ct.openAsNew = function ()
{
	ct.activate();
	ct.executeScript("dw.newCCDocument(true)");
}

//---------------------------------------------------------
// open(files)
//	Performs the equivalent of CT's File > Open command on the specified files.
//
//	Arguments
//	- files:	A File object or array of File objects. (See File and Folder Objects.)
//
ct.open = function open( files )
{
	if (files.reflect.name == "Array")
	{
		ct.activate();
	
		for (var index=0; index < files.length; index++)
		{		 
			var script = "dw.openDocument(dw.convertURIToFilePath('" + files[index].fsName + "'), true);"				
			ct.executeScript(script);
		}
	}
	else if (files.reflect.name == "File")
	{
		ct.activate();		

		var script = "dw.openDocument(dw.convertURIToFilePath('" + files.fsName + "'), true);"				
		ct.executeScript(script);
	}
}

//---------------------------------------------------------
// print(files)
//	Performs the equivalent of CT's File > Print command on the requested files.
//
//	Arguments
//	- files:	A File object or array of File objects. (See File and Folder Objects.)
//
ct.print = function ( files )
{    
	if (files.reflect.name == "Array")
	{
		ct.activate();
	
		for (var index=0; index < files.length; index++)
		{
			var script = "dw.printDocument(dw.convertURIToFilePath('" + files[index].fsName + "'));"
			ct.executeScript(script);
		}
	}
	else if (files.reflect.name == "File")
	{
		ct.activate();
		var script = "dw.printDocument(dw.convertURIToFilePath('" + files.fsName + "'));"
		ct.executeScript(script);
	}
}

//---------------------------------------------------------
// reveal(file)
//	Gives CT the operating-system focus, and, if the specified file is open in CT, brings it to the foreground.
//
//	Arguments
//	- files:	A File object or string specifying a file that can be opened in the target application.
//
ct.reveal = function ( file )
{
	if (file.reflect.name == "File")
	{
		ct.activate();

		var script = "dw.revealDocument(dw.convertURIToFilePath('" + file.fsName + "'));"
		ct.executeScript(script);
	}
	else if (file.reflect.name == "String")
	{
		ct.activate();

		var script = "dw.revealDocument(dw.convertURIToFilePath('" + file + "'));"
		ct.executeScript(script);
	}
}



//---------------------------------------------------------
// place -  insert files into active document or site
//
// Description: Place attempts to insert the specified files
//	into the current page. If no page is open nothing will happen.

ct.place = function ( files )
{    
	ct.activate();		
	
	var err = localize ("$$$/CT/Err/NoDraft/=There is no open draft to place into.");	
	var str = " alert(\"" + err + "\");";
		
	ct.executeScript(
					"var doc = dw.getDocumentDOM();" +										
					"if ( doc!=null && dw.getFocus()!='browser' )" +
						"doc.insertFiles(" + files + ");" +
					"else" +
					    str
					);	
}

//--------------------------------------------------------------------
// BRIDGE UI
//--------------------------------------------------------------------

//---------------------------------------------------------
// Create Bridge Specific UI
//
// Win & MAC
if( BridgeTalk.appName == 'bridge' )
{

  // File -> Place -> In Contribute Menu
  
    // create menu
	var placeInContributeMenu = MenuElement.create( 'command', 
		localize ("$$$/CT/Menu/Place/PlaceInContribute=In Contribute"), 
		'at the end of submenu/Place', 'PlaceInContribute' );
	
	// on select 
	placeInContributeMenu.onSelect = function()
	{
		// Get all selected files (no containers).
		var sel = app.document.getSelection("*");
				
		// Get the files, may be remote/version cue
		app.acquirePhysicalFiles( sel );
		var fileList = Contribute.thumbnailsToFiles(sel);
		ct.place(fileList);
	}
   
    // on display of menu
	placeInContributeMenu.onDisplay = function()
	{
		this.enabled = false;

		if( app.document.selectionLength > 0 )
		{
			// You can put a at least a link to anything onto a HTML-page
			// If there was filtering, this is where it would go
			
            // For performace reasons, we should not get a selection with too many objects for every click 
			// in the menu bar. So if the selection is higher than 200, we optimistically assume that there
			// will be at least one non-container object in the list
			// and then handle the filtering only if the menu item is actually selected.
			if (app.document.selectionLength > 200)
			{
				this.enabled = true;
			}
			else
			{
				// Don't allow containers (volumes or folders)
				// Get all selected files (no containers).
				var sel = app.document.getSelection("*");
			
				if (sel.length > 0)
						this.enabled = true;			
			}
		}
	}
}
	
// Win only UI
if( (BridgeTalk.appName == 'bridge')  && (File.fs == "Windows") )
{
	// create Tools->Contribute menu 	
				
	var location = '';
	
	// we try to find the menu under which we would want our menu
	// to exist, if we do not find it, we look for the next best alternative
	if( MenuElement.find('tools/id')!= null )
		location = "after submenu tools/id";
	else if( MenuElement.find('tools/ai') != null )
		location = "after submenu tools/ai";	
	else if( MenuElement.find('tools/ps') != null )
		location = "after submenu tools/ps";
	else if( MenuElement.find('Tools/PhotoshopServices') != null )
		location = "-after Tools/PhotoshopServices ";	
	else if ( MenuElement.find('submenu/VersionCue') != null )
		location = "-after submenu/VersionCue";
	else 
		location = "-after submenu/Cache";
		
	var ctTools = MenuElement.create( "menu", localize ("$$$/CT/Contribute=Contribute"), location, "tools/ct" );  
	
	// Tools->Contribute->Publish To Website... menu item
	
	ctTools.menuPTW = MenuElement.create( "command", localize ("$$$/CT/Menu/Tools/Contribute/PTW=Publish To Website..."), 
	    "at the end of tools/ct", 'tools/ctTools/PTW');
	
	// on select
	ctTools.menuPTW.onSelect = function()
	{ 
	
	    if( app.document.selectionLength != 1 )	    
	    {
	        alert(localize ("$$$/CT/Err/PTWErr=Publish To Web Error: Please select a single MS Word document(.doc, .docx) or an MS Excel spreadsheet(.xls, .xlsx, .xlsm) .") );
	         
	        return;
	    }
	
		// Get all selected files (no containers).
		var sel = app.document.getSelection("*");
				
		// Get the files, may be remote/version cue
		app.acquirePhysicalFiles( sel );
		var fileName = Contribute.thumbnailsToFiles(sel);		    
		
		// check for the correct file type - .doc/.docx/.xls/.xlsm/.xlsx
		var length = fileName.length; 		
		var ext = fileName.substring(length - 5, length - 1 );						
		var newExt = fileName.substring(length - 6, length - 1);		
		
		if ( ( ext==".doc" || ext==".xls" ) || ( newExt ==".xlsx" || newExt == ".docx" || newExt == ".xlsm" ) )
		{
		    ct.activate();	
		    ct.executeScript("dw.publishToWeb(" + fileName + ");");
		}
		else
		{
		    alert(localize ("$$$/CT/Err/PTWErr=Publish To Web Error: Please select a single MS Word document(.doc, .docx) or an MS Excel spreadsheet(.xls, .xlsx, .xlsm) .") );
		     
		    return;
		}		
   	}


    // Tools->Contribute->Open In Contribute... menu item
    
	ctTools.menuOCT = MenuElement.create( "command", localize ("$$$/CT/Menu/Tools/Contribute/OCT=Open In Contribute..."), 
	    "at the end of tools/ct", "tools/ctTools/OCT");
	    
	ctTools.menuOCT.onSelect = function() 
	{ 
	
	    if( app.document.selectionLength != 1 )	    
	    {
	        alert(localize ("$$$/CT/Err/OCTErr=Open In Contribute Error: Please select a single MS Word document(.doc, .docx) or an MS Excel spreadsheet(.xls, .xlsx, .xlsm) .") );
	         
	        return;
	    }
	
		// Get all selected files (no containers).
		var sel = app.document.getSelection("*");
				
		// Get the files, may be remote/version cue
		app.acquirePhysicalFiles( sel );
		var fileName = Contribute.thumbnailsToFiles(sel);
		
		// check for the correct file type - .doc/.docx/.xls/.xlsm/.xlsx
		var length = fileName.length; 
		var ext = fileName.substring(length - 5, length - 1);
		var newExt = fileName.substring(length - 6, length - 1);		
		
		if ( ( ext==".doc" || ext==".xls" ) || ( newExt ==".xlsx" || newExt == ".docx" || newExt == ".xlsm" ) )
		{
		    ct.activate();	
		    ct.executeScript("dw.openInContribute(" + fileName + ");");
		}
		else
		{
		    alert(localize ("$$$/CT/Err/OCTErr=Open In Contribute Error: Please select a single MS Word document(.doc, .docx) or an MS Excel spreadsheet(.xls, .xlsx, .xlsm) .") );
		     
		    return;
		}		
   	}

} // End of Bridge specific code


//--------------------------------------------------------------------
// UTILITIES
//--------------------------------------------------------------------

//---------------------------------------------------------
// activate()
//	Gives ct the operating-system focus.
//
ct.activate = function ()
{    
	ct.executeScript("dw.activateApplication();");
}

//---------------------------------------------------------
// argsToString -  
//
// Description:
//	This routine create a string for the arguments that we can transmit
//	over BridgeTalk as text.
//

Contribute.argsToString = function ( args )
{
	var outArgs = new Array();
	
	for( var index=0; index<args.length; index++ )
	{
		outArgs.push(args[index].toSource());
	}
		
	return outArgs.join(',');
}


//---------------------------------------------------------
// thumbnailsToFiles -  
//
// Description:
//	Convert a bridge thumbnails list to file references.
//

Contribute.thumbnailsToFiles = function ( thumbList )
{
	var outFileList = new Array();
	
	for( var index=0; index<thumbList.length; index++ )
	{		
		if (thumbList[index].spec != undefined) // Filter out containers (volumes and folders)
			outFileList.push('"'+thumbList[index].spec.fsName+'"');
	}
		
	return outFileList.join(',');
}
