0

该脚本试图:

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

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

到目前为止,我几乎可以正常工作,因为创建了一个新文件夹,并且 InDesign 似乎确实通过BridgeTalk. 但是当打开 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

4

2 回答 2

1

Yeah I experienced issues recently with single line comments and indeed using /* ... */ did the trick. I think you can pass a string for your url but need to escape the slashes characters.

于 2012-07-12T10:27:01.100 回答
0

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

洛伊克

于 2012-07-10T16:48:41.197 回答