据我所知,声明#targetengine "myEngineName"
将用于InDesign记住全局变量(在这里找到的信息: http://incom.org/post/89818)。
InDesign CS5脚本:为什么`#targetengine`工作不正常?
然而,这仍然是不够的,重要的是记住全局变量,因为它仍然抛出关于全局变量
imgs
错误:
Error Number: 30476
Error String: "if(imgs[i].itemLink != null)" could not be completed because the object no longer exists.
...或者类似的东西反正。它不喜欢我的代码中的特定行,似乎忘记了全局变量imgs
被实例化为。
所以,我实现了一个try-catch语句,并reinstatiated变量
imgs
和减少在catch迭代器......虽然这
做解决的问题,为什么不
#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语句...
有趣...所以使用'app.activeDocument'我尝试了'app.documents [0]',现在这个错误不会发生。所以它看起来似乎变得未定义,因为'app.activeDocument';声明,并且文档将焦点更改为图像文件夹可能导致此问题。感谢@Loic的帮助! – 2012-07-13 16:11:22
这就是为什么如果你可以避免使用全局变量是好的;) – Loic 2012-07-13 21:29:33