我将 InDesign CC 2017 与 Mac OS X El Capitan 结合使用,并且希望使用“启动脚本”文件夹中的脚本,以便每次打开文件时始终执行检查该文件的 filePath 中的特定字符串。如果在 filePath 中找到该字符串,我只想向用户显示一条消息。
选择要打开的文件后,我在加载文件之前收到警告。 “附加的脚本生成了以下错误:没有打开任何文档。是否要禁用此事件处理程序?”
我认为使用名为“afterOpen”的事件监听器,直到文件打开后才会触发脚本,在这种情况下我认为我不应该收到警告。
我理想的解决方案是通过使用更合适的代码来避免警告(这就是我希望你能帮助我的),但我也愿意让有人向我展示如何添加代码来简单地抑制警告。
#targetengine "onAfterOpen"
main();
function main() {
var myApplicationEventListener = app.eventListeners.add("afterOpen",myfunc);
}
function myfunc (myEvent) {
var sPath = Folder.decode(app.activeDocument.filePath);
if(sPath.indexOf("string in path") >= 0){
alert("This file is the one mother warned you about.");
} else {
alert("This file is good to go!");
}
}
预先感谢您的帮助。 :)
最佳答案
当事件在对象层次结构中冒泡时,您需要确保事件父对象实际上是文档:
#targetengine "onAfterOpen"
main();
function main() {
var ev = app.eventListeners.itemByName ( "onAfterOpen" );
!ev.isValid && app.eventListeners.add("afterOpen",myfunc).name = "onAfterOpen";
}
function myfunc (myEvent) {
var doc = myEvent.parent, sPath;
if ( !( doc instanceof Document ) ) return;
sPath = decodeURI(doc.properties.filePath);
if ( !sPath ) return;
alert( /string in path/.test ( sPath )?
"This file is the one mother warned you about."
:
"This file is good to go!"
);
}
关于javascript - 在 InDesign CC 2017 javascript 中,使用 eventListener "afterOpen"时,如何避免警告 "No documents are open."?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44530888/