2017-06-13 66 views
2

我在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!"); 
    } 
} 

在此先感謝您的幫助。 :)

回答

2

由於事件冒泡通過對象層次,你需要得到肯定該事件的父對象實際上是文檔:

#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

相關問題