2 Replies Latest reply on Jan 15, 2012 3:44 PM by dylw

    Passing variables from one event listener to another.

    dylw Level 1

      I have written an Applescript that exports JPEGs from the active InDesign document and I have set it to run on the beforeClose event handler. Which is great and works well, but I don't want it to run on BeforeClose if the document was never saved because there would be no point remaking an unchanged JPEG and the process of exporting to JPEG is time consuming (well only a little but that is besides the point). I had the Applescript, at one point, run on the beforeSave event handler, but it got tedious constantly exporting JPEGs every time I saved when I only wanted the JPEG based on my last save.

       

      So I figured, what if I stored a variable in the Startup Script that I created for the Event Listeners that said there were no documents open and nothing had been saved:

      property SaveStatus : {DocID:false, DocSaved:false}

       

      Then using a beforeSave Event Listener to adjust that value when saving a document:

      set thisDoc to the active document

      set SaveStatus to {DocID:thisDoc, DocSaved:true}


      And when running the beforeClose Event Listener, check to see if the active document has been saved and if so run the script, afterwards resetting SaveStatus to false.

       

      But with my intermediate to amateur knowledge of Applescript I am struggling with the concept of sharing the variable between different Applescripts. The only way I can think of is to store some kind of preference/text file on my computer with the variable's properties and then retrieving them with the other script. Surely there is a way to store it in the memory during the InDesign session, or is there a much better way of doing this, or have I completely overlooked a more obvious and easier solution entirely?

       

      --Gosh I hope I haven't sounded like a buffoon and this all makes sense.

        • 1. Re: Passing variables from one event listener to another.
          absqua Level 4

          You can run your startup script in a persistent engine (#targetengine "whatever" at the top of your script) and the listeners—in whichever scripts are running in the same engine—will have access to its variables across the session. I don't see any reason to use multiple scripts for what you're doing here, though; you can put all your listeners in one (but you'll still need to run the startup script in a persistent engine).

           

          And I would think you would need to keep one hash or record for each document you open, maybe using the full path to the file as the id, to deal with having more than one document open at a time.

           

          Jeff

          1 person found this helpful
          • 2. Re: Passing variables from one event listener to another.
            dylw Level 1

            Thanks for your help Jeff, but I think a lot of what you are talking about is only relevant for Javascripts. Anyway, your comments "I don't see any reason to use multiple scripts for what you're doing here" was most helpful, I don't know what I was thinking last Friday. I have now registered only one event listener on startup which runs a before save event. This before save event registers a close event that runs the time consuming part of the script, then when the before close is finished the save event removes the close event. Works a charm.