5 Replies Latest reply on Jun 2, 2015 3:09 PM by MadMac55

    Closing library panels at beginQuit event

    thor2112 Level 1

      Hello,

       

      I'm trying to have a javascript run when the user quits InDesign (CS5), and close all the Library Panels.

       

      The code below (that only closes the Library Panels) works perfect if ran from ExtendScript.  But once I add the EventListener code and then place it in the startup scripts folder, it will loop but never close any panel.  I added the 'n' character so as not to get caught in the infinite loop.

       

      I've also experimented trying to do this when InDesign is first launched, but it doesn't seem to work either.  I'm wondering if it has something to do with how events work in InDesign, and maybe the Library Panels aren't accessible at these points?

       

      Any insight would be appreciated.  Thanks!

       

      ------------------------------------------------------------------------------------------ -

      app.addEventListener('beforeQuit', save_library, false);

       

      function save_library(theEvent) {

          var n = 0;

          while(app.libraries.length > 0) {

              var lib = app.libraries[0];

              alert(lib.name);

       

              //lib.close();

              app.libraries[0].close();

       

              n++;

              if (n > 7) { break; }

          }

      }

      ------------------------------------------------------------------------------------------ -

        • 1. Re: Closing library panels at beginQuit event
          Loic.Aigon Adobe Community Professional

          Hi,

           

          Why not use this line:

           

          function save_library(theEvent) {

               app.libraries.everyItem().close();

          }

           

           

          But maybe the issue is that event may not allow application tweaks as it's about to close :S A 2cts thought.

           

          Loic

          1 person found this helpful
          • 2. Re: Closing library panels at beginQuit event
            thor2112 Level 1

            Hi Loic,

             

            Thank you for your reply.  I gave your code a try, but when quitting the app, the Libraries still wouldn't close - which makes me think it's something with how the event works.

             

            However, if I reverse things a bit, and run your code when the app first starts up, it works great!  I tried this originally with the code I had, but my code wouldn't work.  Running the code at startup (instead of at quit) will totally work for what I need, so thank you very much for sharing your code and thoughts.  I appreciate it!  :-)

             

            Jay

            • 3. Re: Closing library panels at beginQuit event
              Matthew.Fry Level 1

              Thor2112,

               

              What does your final version of this script look like?

               

              Thanks.

               

              Matt

              • 4. Re: Closing library panels at beginQuit event
                thor2112 Level 1

                Hi Matt,

                 

                Man, this was awhile back, and from what I recall I never found a good solution.

                 

                I've included the code below I came up with at that time, which was AppleScript.  The department who needed this was wanting to be able to have the latest libraries, which were stored on the server, and have them stored to their folder on the desktop if appropriate.

                 

                I was never happy with this, but it was the best I could figure out at that time.

                 

                Jay

                 

                -----

                 

                property pServer : "work"

                property pFromTemplatePath : "work:TEMPLATES:image library:"

                 

                property pToTemplatePath : "image library"

                 

                ------------------------------------------------------------------------------------------ --------------------

                -- Main

                 

                try

                  set errList to ""

                  -------

                  --tell application "Adobe InDesign CS5"

                  --          set libraryList to libraries's name -- Get Library names that are open

                  --          tell libraries to close

                  --end tell

                  -------

                  if pServer is not in (list disks) then mountServer()

                  -------

                  set desktopPath to (path to desktop as string)

                  set toTemplatePath to (desktopPath & pToTemplatePath & ":")

                  -------

                  if pToTemplatePath is not in listFolder(desktopPath) then

                    tell application "Finder"

                      make new folder at folder desktopPath with properties {name:pToTemplatePath}

                    end tell

                  end if

                  -------

                  set desktopTemplateList to listFolder(toTemplatePath)

                  set libraryList to {}

                  -------

                  repeat with i in listFolder(pFromTemplatePath)

                    set i to (i as string)

                    -------

                    if i is in desktopTemplateList then

                      tell application "System Events"

                        set toMod to file (toTemplatePath & i)'s modification date

                        set fromMod to file (pFromTemplatePath & i)'s modification date

                      end tell

                      -------

                      if toMod < fromMod then set libraryList's end to i

                    else -- Library file doesn't exist in Desktop folder

                      set libraryList's end to i

                    end if

                  end repeat

                  -------

                  repeat with i in libraryList

                    set i to (i as string)

                    -------

                    try

                      dupeItem((pFromTemplatePath & i), toTemplatePath)

                    on error number n

                      if n is -54 then -- Permissions error

                        set errList to (errList & return & i)

                      else

                        try

                          tell application "Finder" to delete alias (toTemplatePath & i) -- Move to Trash

                          -------

                          delay 1 -- Give system time to move file to Trash

                          -------

                          dupeItem((pFromTemplatePath & i), toTemplatePath)

                          -------

                          try

                            tell application "Adobe InDesign CS5" to tell library i to close

                          end try

                        on error

                          set errList to (errList & return & i)

                        end try

                      end if

                    end try

                  end repeat

                  -------

                  --tell application "Adobe InDesign CS5"

                  --          try

                  --                    repeat with i in libraryList

                  --                              open alias (toTemplatePath & (i as string))

                  --                    end repeat

                  --          end try

                  --end tell

                  -------

                  if (count errList) > 0 then

                    error ("Can't copy the following Libraries:" & return & errList)

                  else

                    if (count libraryList) > 0 then displayDialog("Templates have been updated!", {"OK"}, 1, 2)

                  end if

                on error Err

                  displayDialog(Err, {"OK"}, 1, 0)

                end try

                 

                ------------------------------------------------------------------------------------------ --------------------

                -- Bring up sign on window for server

                 

                on mountServer()

                  try

                    with timeout of 9999999 seconds

                      mount volume ("afp://:@TEST/" & pServer)

                    end timeout

                  on error

                    error ("Can't connect to " & pServer)

                  end try

                end mountServer

                 

                ------------------------------------------------------------------------------------------ --------------------

                -- Duplicate item

                 

                on dupeItem(itemPath, folderPath)

                  with timeout of 1200 seconds -- 20 minutes

                    tell application "Finder" to duplicate alias itemPath to folder folderPath with replacing

                  end timeout

                end dupeItem

                 

                ------------------------------------------------------------------------------------------ --------------------

                -- Return folder contents

                 

                on listFolder(folderPath)

                  try

                    tell application "System Events" to return (list folder alias folderPath without invisibles)

                  on error

                    error ("Can't get file(s) from folder:  " & folderPath)

                  end try

                end listFolder

                 

                ------------------------------------------------------------------------------------------ --------------------

                -- Display a dialog

                 

                on displayDialog(txt, btn, def, giv)

                  activate

                  -------

                  if giv > 0 then

                    display dialog txt buttons btn default button def giving up after giv

                  else

                    with timeout of 9999999 seconds

                      display dialog txt buttons btn default button def

                    end timeout

                  end if

                end displayDialog

                • 5. Re: Closing library panels at beginQuit event
                  MadMac55 Level 1

                  I have run similar code from js to try and close Libraries.  My simple finding is that palettes that are expanded will close; palettes that are collapsed will not.  It could be a case of trying to find the right language that 1) first expands the palette, then closes the libraries.

                   

                  In this sample, collapsed palettes stay open, expanded will close.

                   

                  function CloseLibraries(myEvent) {

                      var myLibraries = app.libraries; // all open libraries

                      if(myLibraries.length > 0) {

                          var libraryNames = (myLibraries.everyItem(0).name).toString(); // string containing all names

                              for (var i = myLibraries.length - 1; i > -1; i--) { // looping backwards because closing libraries changes the indexes

                                  if (myLibraries[i].associatedPanel != null && myLibraries[i].associatedPanel.visible == true) {

                                      myLibraries[i].associatedPanel.visible = false;

                                  }

                                  myLibraries[i].close(); // file closed

                              }