的InDesign CS5脚本:为什么`#targetengine`不能正常工作? [英] InDesign CS5 Script: Why is `#targetengine` not working correctly?

查看:268
本文介绍了的InDesign CS5脚本:为什么`#targetengine`不能正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,申报 #targetenginemyEngineName将用于InDesign的记忆全局变量(发现有关此问题在这里:  的 http://incom.org/post/89818 )。


然而,这仍然是不够的,一定要记住全局变量,因为它仍然抛出关于全局变量 IMGS 错误:

I understand that the declaration #targetengine "myEngineName" would be used for InDesign to remember global variables (found information on this here:  http://incom.org/post/89818).


However, this was still not enough for it to remember global variables as it still throws an error regarding the global variable imgs:

错误号:   30476   
  错误字符串:  如果(!IMGS [I] .itemLink = NULL)不能   完成,因为该对象已不存在。

Error Number:  30476
Error String:  "if(imgs[i].itemLink != null)" could not be completed because the object no longer exists.

...或者类似的东西反正。它不喜欢我的code特定的路线,似乎忘记了什么是全局变量 IMGS 被实例化。


所以,我实现了一个try-catch语句,并reinstatiated变量 IMGS 和减少在catch迭代器......虽然这个确实解决问题为什么不 #targetenginemyEngineName解决像它的问题咋办?

这是我的code:

...or something like that anyway. It doesn't like that particular line in my code, and seems to be forgetting what the global variable imgs was instantiated as.


So I implemented a try-catch statement, and reinstatiated the variable imgs and decremented the iterator in the catch... Though this did solve the problem, why doesn't #targetengine "myEngineName" solve the problem like it is supposed to?

Here is my code:

#target "InDesign" // this solves the "Error Number: 29446" problem
#targetengine "session" // this solves the "Error Number: 30476" problem

var imgs; // global variable for the #targetengine "session" to keep track of
var document = app.activeDocument;
var newFolder = createFolder(document); // if subdirectory images DNE, create this folder with the function below

saveAllImages(document, newFolder); // with the function below

alert("The file conversion is complete!\n\nAll black & white images have been copied to:\n" + newFolder +
        "\.\n\nAll color images have been replaced with the new black & white images in the current InDesign document.");

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

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 + "\n - reload the script, and it should work.");
        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 saveAllImages(doc, folder)
{
    imgs = document.allGraphics; // this is a global variable, for the #targetengine "session" to keep track of
    var fileName = "";
    var img = "";
    var imgType = "";

    for(var i = 0; i < imgs.length; i++)
    {
        try
        {
            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."); // Note: escape characters

               /*
                     * array for image options, instantiated from the function below;
                     * options[0] is the "MAXIMUM" image quality property, &
                     * options[1] is the "GRAY" image conversion property;
                     */
                var options = convertToBlackAndWhite(imgType);

                // 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 + " !"); // Note: escape characters
                        break;

                    case "Adobe PDF":
                        break;                            
                    case "EPS":
                        break;
                    case "Windows Bitmap":
                        break;
                    case "JPEG":
                        break;
                    case "PNG":
                        break;
                    case "TIFF":
                        options[0]; // maximum image quality
                        options[1]; // black & white conversion

                        imgs[i].exportFile(ExportFormat.JPG, img, false);
                        replaceWithNewImage(doc, fileName, img); // with the function below
                        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 try statement
        catch(e)
        {
            /*
                * in case the #targetengine is overloaded, this solves the "Error Number: 30476" problem:
                *           - "The requested action could not be completed because the object no longer exists."
                *               (the second statement #targetengine "session" is also in place to solve this error)
                */
            imgs = document.allGraphics; // global variable reinstantiated in case of error
            i--; // retry the same iteration again, in case of error (the variable " i " is the iterator in the for loop)
        }
    } // end of for loop

} // end of function saveAllImages

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

        function convertToBlackAndWhite(fileType)
        {
            // array for image-quality and color-conversion values
            var settings = [];

            // each image exported to the new folder here, by file type
            switch(fileType)
            {                    
                    case "Windows Bitmap":
                        break;
                    case "JPEG":
                        break;
                    case "PNG":
                        break;
                    case "TIFF":
                    settings[0] = "app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MAXIMUM"; // maximum image quality
                    settings[1] = "app.jpegExportPreferences.jpegColorSpace = JpegColorSpaceEnum.GRAY"; // black & white conversion
                    break;

                default:
                    break;
            } // end of switch statement

            return settings; // for instantiation outside of this function

        } // end of function convertToBlackAndWhite

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

function replaceWithNewImage(doc, imageName, newImage)
{
    var links = doc.links;
    var link = "";
    for(var i = 0; i < links.length; i++)
    {
        link = links[i];
        if ( (link.status == LinkStatus.NORMAL) && (link.name == imageName) )
        {
            try
            {
                link.relink(newImage);
                link.update();
            }
            catch(e)
            {
            }
        } // end of if statement
    } // end of for loop

} // end of function replaceWithNewImage


这是唯一的信息,在那里我能找到关于此错误:&NBSP;&NBSP; HTTP://forums.adobe .COM /线程/ 748419

修改 -

我是pretty的确定问题有事情做与功能 replaceWithNewImage ,因为没有发生这个错误没有这个功能,而且当时没有必要的try-catch语句...


This is the only information out there I could find regarding this error:  http://forums.adobe.com/thread/748419

EDIT --

I'm pretty sure the problem has something to do with the function replaceWithNewImage, because this error did not occur without this function, and there was then no need for the try-catch statement...

推荐答案

读了你的code,我看到的东西,可能是真正的问题。您可以设置引用文档活动文档。但是,这仍然是参考各地的会议。事实是,如果你切换到另一个文档或关闭文档,然后将基准丢失。这也许可以解释为什么IMGS可以在某些时候是不确定的,虽然我认为它应该引发一个错误。 不要包装在一个函数范围,我保证你的变量,你就万事大吉了;)

Reading over your code, I see something that may be really problematic. You set the reference to the document to the active document. But this reference remains all over the session. Fact is if you switch to another document or close the document, then the reference is lost. This may explain why imgs can be undefined at some point although I think it should raise an error instead. Do wrap your variables in a function scope and I warranty you everything will be fine ;)

这篇关于的InDesign CS5脚本:为什么`#targetengine`不能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆