首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >InDesign CS5脚本:如何使用BridgeTalk从Photoshop中保存新图像?

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

Stack Overflow用户
提问于 2012-07-10 01:50:57
回答 2查看 1.4K关注 0票数 0

此脚本试图:

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

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

到目前为止,我几乎可以使用,因为创建了一个新文件夹,而InDesign似乎正在通过BridgeTalk与Photoshop进行通信。但是当Photoshop打开时,什么都不会发生--没有保存新的图像,我也不确定灰度转换是否正在进行.

这是我的代码:

代码语言:javascript
复制
#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的工作,在这里可以找到:herehttp://forums.adobe.com/message/3817782

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-10 16:48:41

似乎您正在传递一个文件夹到Photoshop,而不是一个实际的图像文件。这也许可以解释你的问题;)

洛奇

票数 0
EN

Stack Overflow用户

发布于 2012-07-12 10:27:01

是的,我最近遇到了单行评论的问题,而且确实使用了/* . */完成了这个任务。我认为您可以为url传递一个字符串,但需要转义斜杠字符。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11405590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档