0

この宣言#targetengine "myEngineName"は、InDesignがグローバル変数を記憶するために使用されることを理解しています(これに関する情報は  http://incom.org/post/89818にあります)。


ただし、グローバル変数に関するエラーがスローされるため、これでもグローバル変数を記憶するには不十分でしたimgs

エラー番号:30476
エラー文字列: "if(imgs [i] .itemLink!= null)"は、オブジェクトが存在しなくなったため、完了できませんでした。

...またはとにかくそのようなもの。私のコードのその特定の行が気に入らず、グローバル変数imgsがどのようにインスタンス化されたかを忘れているようです。


そこで、try-catchステートメントを実装し、変数を元に戻し、imgscatchのイテレーターをデクリメントしました...これで問題#targetengine "myEngineName"解決しましたが、想定どおりに問題が解決 しないのはなぜですか?

これが私のコードです:

#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


これは私がこのエラーに関して見つけることができた唯一の情報です:http:   //forums.adobe.com/thread/748419

編集- このエラーはそうではなかったので

、問題は関数に関係していると確信していますreplaceWithNewImageこの関数なしで発生し、try-catchステートメントは必要ありませんでした...

4

2 に答える 2

4

あなたのコードを読んで、私は本当に問題があるかもしれない何かを見ます。ドキュメントへの参照をアクティブなドキュメントに設定します。ただし、この参照はセッション全体に残ります。事実は、別のドキュメントに切り替えるか、ドキュメントを閉じると、参照が失われるということです。これは、代わりにエラーが発生するはずなのに、ある時点でimgsが未定義になる理由を説明している可能性があります。変数を関数スコープでラップしてください。すべてがうまくいくことを保証します;)

于 2012-07-12T10:20:40.790 に答える
0

まず第一に、特にセッションエンジンを使用している間は、グローバル変数をできるだけ避けてください。

スクリプトの目的が単純なリンクのエクスポート/リンクの変更/リンクの置換である場合、なぜ永続変数が必要なのですか?

私の謙虚な意見では、私はエンジンを取り除き、通常の機能呼び出しを行うだけです

//myscript.jsx
mySuperFunction();
function mySuperFunction()
{
    var doc;
    if ( app.documents.length == 0 ){ return; }
    doc = app.activeDocument;
    //Do all your stuff
}

必要なのはそれだけです;)

Loic

于 2012-07-10T15:26:24.230 に答える