Search code examples
ajaxadobeadobe-indesignextendscript

InDesign CS5 Script: How do I use BridgeTalk to save new images from Photoshop?


This script is attempting to:

  • create a new folder
  • scan an InDesign document for all images
  • convert all images to grayscale in Photoshop
  • save new grayscale images in the new folder from Photoshop

Converting the images to grayscale in Photoshop requires the use of the BridgeTalk object, which allows for the communication between InDesign and Photoshop (the usage of the BridgeTalk object seems to be a form of Ajax).

What I have so far is almost working, being that a new folder is created, and InDesign does seem to be communicating with Photoshop via BridgeTalk. But when Photoshop is opened, nothing happens -- no new images are saved, and I'm not sure if the grayscale conversion is taking place or not...

Here is my code:

#target "InDesign"

var inDesignDocument = app.activeDocument;
var newFolder = createFolder(inDesignDocument); // if subdirectory images DNE, create this folder with the function below

sendImagesToPhotoshop(inDesignDocument, newFolder);

//---------------------------------------------------------------------------------------------------------------

function createFolder(doc)
{
    try
    {
    /*
         * type-casting the filePath property (of type object) into a String type;
         * must be a String type to concatenate with the subdirectory variable (also of type String)
         */
        var docPath = String(doc.filePath);
        var subdirectory = "/BLACK AND WHITE IMAGES";
    }
    catch(e)
    {
        alert(e.message);
        exit();
    }

    var imagesFolder = docPath + subdirectory; // concatenating the two variables
    if(!Folder(imagesFolder).exists)
    {
        Folder(imagesFolder).create();
    }

    return imagesFolder; // for instantiation outside of this function

} // end of function createFolder

//---------------------------------------------------------------------------------------------------------------

function sendImagesToPhotoshop(doc, folder)
{
    var imgs = doc.allGraphics;
    var fileName = "";
    var img = "";
    var imgType = "";

    for(var i = 0; i < imgs.length; i++)
    {
        if(imgs[i].itemLink != null)
        {
            fileName = imgs[i].itemLink.name;
            img = new File(folder + "/" + fileName); // each image instantiated here
            imgType = imgs[i].imageTypeName; // image type is determined here (.tif, .jpg, .png, etc..)

            alert("The file \"" + fileName + "\"\n\tis a " + imgType + " file.");

            // each image exported to the new folder here, by file type
            switch(imgType)
            {
                case "GIF":
                    alert("This script cannot convert and export the GIF file:\n\t" + fileName + " !");
                    break;

            case "JPG":
            case "EPS":
                case "PNG":
                case "TIFF":

            createBridgeTalkMessage(folder);
                    break;

                default:
                    alert("\tUnlisted image type: " + imgType + "!\nAdd this type to the switch statement.");
                    break;

            } // end of switch statement

        } // end of if statement
    } // end of for loop

} // end of function sendImagesToPhotoshop

//---------------------------------------------------------------------------------------------------------------

function createBridgeTalkMessage(imagesFolder)
{
    var bt = new BridgeTalk();
    bt.target = "photoshop";
    bt.body = saveNewImageInPhotoshop.toSource() + "(" + imagesFolder.toSource() + ");";

    bt.onError = function(e)
    {
        alert("Error: " + e.body);
    }

    bt.onResult = function(resObj){};

    bt.send();

}// end of function createBridgeTalkMessage

//---------------------------------------------------------------------------------------------------------------

    // called from function createBridgeTalkMessage
    function saveNewImageInPhotoshop(imagePath)
    {
        var photoshopDoc = "";
        app.displayDialogs = DialogModes.NO; // Photoshop statement, prevents status alerts from interrupting
        photoshopDoc.changeMode(ChangeMode.GRAYSCALE); // convert image to GRAYSCALE

        photoshopDoc.saveAs(new File(imagePath) );
        photoshopDoc.close(SaveOptions.DONOTSAVECHANGES);

    } // end of function saveNewImageInPhotoshop


This is based on the work of Kasyan Servetsky, found here:  http://forums.adobe.com/message/3817782


Solution

  • It seems you are passing a folder to Photoshop instead of an actual image file. That may probably explain your issue ;)

    Loic