【问题标题】:Run event listener only once after saving InDesign document?保存 InDesign 文档后只运行一次事件侦听器?
【发布时间】:2013-09-21 01:58:37
【问题描述】:

我有一个脚本(不是我写的),其中有一个app.addEventListener('afterOpen', addVariables); 事件(addVariables 是函数的名称)。

我把它改成了app.addEventListener('afterSave', addVariables);,它按照我想要的方式工作,只是它创建了一个“永无止境”的循环。

基本上发生的事情是脚本提取用户名(即:Tia)并将其作为可变文本添加到 InDesign CC 中的粘贴板上。使用“afterOpen”命令,只要我打开文档,名称就会更新;这违背了脚本的目的,因为我想知道谁最后保存文档,而不是最后打开文档的人(永远是我)。该脚本与“afterSave”命令一起正常工作,因为它显示了最后保存它的人的姓名;但是,我现在创建了一个新问题,即每当有人尝试保存文档时,脚本都会不断循环*。

那么,我的问题是:我可以告诉 eventListener 每个文档只运行一次吗?


我不熟悉 javascript——我对 applescript 有相当的了解。

感谢您的帮助!


运行 osx 10.8.4 的 iMac • Adob​​e InDesign CC • 使用 Adob​​e ExtendScipt Toolkit 进行编辑



*它会循环,因为一旦有人保存文档,脚本就会运行并更改文本变量,InDesign(正确地)将其记录为对文档的更改。所以现在文档被标记为“未保存”(文档名称旁边有一个星号)。如果我按 command+s 再次将其保存以“清除”星号,则 eventListener 会选择 afterSave 命令并再次更新文档,而 InDesign 会选择更改并将其标记为未保存...所以您可以看看我所说的循环是什么意思。您可以告诉文档关闭(command+w)并告诉它在关闭之前保存,它会这样做,然后关闭。但是,我更愿意只保存文档一次,变量文本会更新,然后如果对该文档进行任何进一步的更改当它仍然打开时,脚本将不会再次运行。显然,我希望脚本在每个新文档上运行一次。

【问题讨论】:

  • 我认为如果你展示你用来做这件事的代码会有所帮助。

标签: javascript adobe-indesign extendscript


【解决方案1】:

您可以通过跟踪脚本是否更新了特定文档的用户名来解决此问题。如果我们已经更新了它,那么在我们再次更新之前停止。否则,请继续更新。

您的 JavaScript 将如下所示:

#targetengine "session"

(function(){
  // Keep track of the files where we've updated
  // the user's name onto the pasteboard.
  var isUserNameUpdated = {};

  app.addEventListener('afterSave', function(theEvent) {
    var filename = theEvent.fullName;
    if (isUserNameUpdated[filename] === true) {
      return;
    }
    isUserNameUpdated[filename] = true;

    // Put the rest of your code here
  });

  app.addEventListener('afterOpen', function(theEvent) {
    // afterOpen fires twice: once when the document opens
    // and once when the window loads. Only the first event
    // has the fullName property. So don't run the second time,
    // to avoid causing an error.
    //
    // See: http://forums.adobe.com/message/5410190
    if (theEvent.target.constructor.name !== 'Document') {
      return;
    }

    // If we've previously saved the file, closed it,
    // then opened it again, then forget that we had 
    // saved it previously.
    var filename = theEvent.properties.fullName;
    isUserNameUpdated[filename] = false;
  });
})();

【讨论】:

    【解决方案2】:

    问题确实是每个保存操作都会触发侦听器,从而导致需要无休止地保存未保存的文档。 一种解决方案是禁用侦听器,这样您就可以在不触发事件的情况下强制保存并在之后启用它。

    function addVariables(openEvent){
    	
        var doc = openEvent.parent;
        
        while ( doc.constructor.name != "Document" )
        {
    	    if ( doc.constructor.name == "Application" ){ return; }
    	   
    	   doc = doc.parent;
    	}
    	//Adding User name to text variables
    	createTextVariable(doc, "User name", (Folder.myDocuments).parent.displayName );
    	
      //Removing the afterSave listener
    	removeAfterSaveEventListener();
      
      //Saving document without firing event listener
    	doc.save();
      
      //Adding the afterSave listener
    	addAfterSaveEventListener();
    }
    var removeAfterSaveEventListener = function() {
      var el = app.eventListeners.item ( "onAfterSave" );
    	el.isValid && el.remove();
    }
    
    var addAfterSaveEventListener = function() {
      app.addEventListener('afterSave', addVariables).name = "onAfterSave";
    }
    
    //Adding the afterSave listener
    addAfterSaveEventListener();

    【讨论】:

      【解决方案3】:

      至少在 Applescript 中,可以选择让事件监听器对 on beforeSave 做出反应:

      tell application "Adobe InDesign CC 2017"
              make event listener with properties {event type:"beforeSave", handler:myHandler}
          end tell
      

      这应该可以解决问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-09
        • 2018-03-18
        • 2019-01-02
        • 2019-05-22
        • 1970-01-01
        • 2021-01-07
        相关资源
        最近更新 更多