InDesign CS5脚本:如何使用BridgeTalk从Photoshop保存新图像?

时间:2012-07-10 01:50:58

标签: ajax adobe adobe-indesign extendscript

此脚本正在尝试:

  • 创建新文件夹
  • 扫描所有图像的InDesign文档
  • 在Photoshop中将所有图像转换为灰度
  • 将新的灰度图像保存在Photoshop的新文件夹中

在Photoshop中将图像转换为灰度需要使用BridgeTalk对象,这允许InDesign和Photoshop之间的通信(BridgeTalk对象的使用似乎是Ajax的一种形式) 。

到目前为止,我已经几乎正在工作,因为创建了一个新文件夹,InDesign似乎确实通过BridgeTalk与Photoshop进行通信。但是当打开Photoshop时,没有任何反应 - 没有保存新图像,我不确定是否正在进行灰度转换......

这是我的代码:

#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


这是基于Kasyan Servetsky的工作,可在此处找到:http://forums.adobe.com/message/3817782

2 个答案:

答案 0 :(得分:1)

是的,我最近遇到过单行注释问题,确实使用/ * ... * /做了诀窍。我认为你可以为你的网址传递一个字符串,但需要转义斜杠字符。

答案 1 :(得分:0)

您似乎正在将文件夹传递给Photoshop而不是实际的图像文件。这可能解释了你的问题;)

卢瓦克