title: Run event listener only once after saving InDesign document?
body: <p>I have a script <em>(that I did not write)</em> that has a <code>app.addEventListener('afterOpen', addVariables);</code> event in it (addVariables being the name of the function).</p>
  
  <p>I altered it to be <code>app.addEventListener('afterSave', addVariables);</code> which works how I wanted it to except that it's creating a "never-ending" loop. </p>
  
  <p>Basically what's happening is that the script pulls the user's name (i.e.: Tia) and adds it as variable text onto the pasteboard in InDesign CC. With the "afterOpen" command, the name was updating anytime I opened the document; which defeated the purpose of the script since I want to know who last <em>saved</em> the document, not who last opened the document <em>(which would always be me)</em>. The script works properly with the "afterSave" command in that it shows the name of the person who last saved it; however, I've now created the new problem that the script keeps looping* whenever anyone tries to save the document. </p>
  
  <p><strong>So, my question is this: can I tell the eventListener to run only once per document?</strong> </p>
  
  <p><br />
  I am not familiar with javascript—I do have a fair understanding of applescript. </p>
  
  <p><em>Thanks for any help!</em> <br /><br /></p>
  
  <hr>
  
  <p><em>iMac running osx 10.8.4 • Adobe InDesign CC • Using Adobe ExtendScipt Toolkit to edit</em></p>
  
  <hr>
  
  <p><br />
  *It loops because once someone saves the document, the script runs and changes the text variable, which InDesign (correctly) notes as a change to the document. So now the document is flagged as "unsaved" (it has an asterisk next to the document name). If I hit command+s to save it again to "clear" the asterisk, then the eventListener picks up the afterSave command and updates the document again which in turn InDesign picks up as a change and flags it as unsaved... so you can see what I mean by it loops. You can tell the document to close (command+w) and tell it to save before closing, which it does, and then closes. However, I'd prefer to just save the document once, the variable text updates, and then if any further changes are made on that document <em>while it's still open</em>, the script will not run again. Obviously, I want the script to run once on each new document.</p>