在InDesign CC 2017 JavaScript中,当使用eventListener“afterOpen”时,如何避免警告“没有文档打开”。

问题描述:

我在Mac OS X El Capitan中使用InDesign CC 2017,并且希望在我的“启动脚本”文件夹中使用脚本,以便每次在该文件的filePath中为某个字符串打开文件时始终执行检查。如果在filePath中找到字符串,我只想向用户显示一条消息。在InDesign CC 2017 JavaScript中,当使用eventListener“afterOpen”时,如何避免警告“没有文档打开”。

选择要打开的文件后,我会在加载文件之前收到警告。 “附加的脚本生成以下错误:没有文档打开,是否要禁用此事件处理程序?”

我用一个名为“afterOpen”的eventListener来表示,直到打开文件之后才会触发脚本,在这种情况下,我认为我不应该得到警告。

我理想的解决方案是通过使用更合适的代码来避免警告(这就是我希望你能帮助我的),但我也愿意让别人告诉我如何添加代码压制警告。

#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() { 
 
\t var ev = app.eventListeners.itemByName ("onAfterOpen"); 
 
\t !ev.isValid && app.eventListeners.add("afterOpen",myfunc).name = "onAfterOpen"; 
 
} 
 

 
function myfunc (myEvent) { 
 
\t 
 
\t var doc = myEvent.parent, sPath; 
 
\t if (!(doc instanceof Document)) return; 
 
\t 
 
\t sPath = decodeURI(doc.properties.filePath); 
 
\t if (!sPath) return; 
 

 
\t alert(/string in path/.test (sPath)? 
 
\t \t "This file is the one mother warned you about." 
 
\t \t : 
 
\t \t "This file is good to go!" 
 
\t); 
 
}

+0

非常感谢!这工作完美。没有错误和脚本功能就像我想要的。 – nollaf126