据我所知,声明#targetengine "myEngineName"将用于InDesign来记住全局变量(在这里找到的信息是:herehttp://incom.org/post/89818)。
然而,这仍然不足以让它记住全局变量,因为它仍然抛出一个关于全局变量imgs的错误:
错误号:由于对象不再存在,因此无法完成另一项错误字符串:“if(imgsi.itemLink != null)”。
无论如何,...or是这样的。它不喜欢我的代码中的特定行,似乎忘记了全局变量imgs被实例化为什么。
所以我实现了一个try-catch语句,并恢复了变量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语句.
发布于 2012-07-12 10:20:40
阅读您的代码,我看到一些可能是真正有问题的东西。将对文档的引用设置为活动文档。但这一提法仍然贯穿整个会议。事实上,如果切换到另一个文档或关闭文档,则引用将丢失。这可能解释了为什么imgs在某个时候可能没有定义,尽管我认为它应该引起一个错误。在函数范围内包装变量,我保证你一切都会好的;)
发布于 2012-07-10 15:26:24
首先,尽量避免全局变量,特别是在使用会话引擎时。
如果您的脚本的目标是一个简单的导出链接/修改链接/替换链接,为什么要有持久变量?
在我看来,我将摆脱引擎,只需做一个定期的功能调用。
//myscript.jsx
mySuperFunction();
function mySuperFunction()
{
var doc;
if ( app.documents.length == 0 ){ return; }
doc = app.activeDocument;
//Do all your stuff
}(这应该是你所需要的;)
洛奇
https://stackoverflow.com/questions/11405987
复制相似问题