8 Replies Latest reply on Apr 4, 2013 8:21 PM by Gustavo Del Vechio

    call a function when the active document is closed

    Gustavo Del Vechio Level 3

      Hi friends

       

      I have a portion of my script (at true a specific function) that I´d need to be executed every time the user close the app.activeDocument in Photoshop.

       

      I know there´s a Photoshop script called "Script Event Manager" where we can associate actions and scripts with events (like closing the document). I´d not like to use this script because..as I told...it´s not my entire script to be runned when closing a file..but only a function.

       

      Tried to search in the Script Event Manager for any event called "onClose" () but sure there´s nothing.

       

      How could I insert an event in the body of my own script that calls a function every time user close the active document?

       

      Thank you a lot for the help

      Best Regards

       

      Gustavo.

        • 1. Re: call a function when the active document is closed
          Michael L Hale Level 5

          The event codes are listed in the scripting guide and are the same as the charID/stringID for the action in the scriptlistener output.

           

          But the event handler runs after the event and the document will have already been closed. While you can have a close event handler it is not as useful as it could be if it was run before the document closed.

          • 2. Re: call a function when the active document is closed
            JJMack Most Valuable Participant

            I think many things in Photoshop can not be nested. It would not surprise me to find out thing like "Script Event Manager" events only get triggered when the user is in control of the document in Photoshop outside of any action script or plug-in.  For example an action can play an other action however it can not play itself recursion is not possible.  Recursion is hard to handle there needs to be a way out else it would be never ending.  If an action, script or plug-in can triggered an event how can Photoshop know this is not infinite recursion.

             

            And as mike point out if you get control after document is close if there is an active document it has nothing to do with the file the was closed. If  a document is not changed to you want to run the process or do you only want to run the process when documents get changed with a save.  There are many types of saves perhaps a event can be created for all save events.  However I feel that saves done within actions, scripts and plugins may not trigger such an event.

            • 3. Re: call a function when the active document is closed
              Gustavo Del Vechio Level 3

              Hi Michael

               

              Thank you a lot for the answer. Well..if the event is runned after the own document is closed (and not before closing), then I think it wont work in my case.

               

              My intention would be...when user invokes a close...script would read the keywords included in file´s metadata (if any) and make a copy of the file to a specific folder if finding the correct keyword.

               

              But if it closes firt..then I could not read keywords any longer.

               

              Have you done such thing??

               

              Best Regards

              Gustavo.

              • 4. Re: call a function when the active document is closed
                Michael L Hale Level 5

                I can think of two things that might be helpful.

                 

                If you are using a CS-SDK panel I understand that some of the events have both a before and after handler. So you could ask about that in the CS-SDK forum if you are using a panel.

                 

                With a standard app.notifer the close event descriptor has the path to the saved document. You could use the xmplib to read the keywords from the saved document and copy to another folder as needed without having to reopen the closed document in Photoshop.

                • 5. Re: call a function when the active document is closed
                  Gustavo Del Vechio Level 3

                  Thank you Michael

                   

                  No..I do not use CS-SDK...it´s the standard!

                   

                  I´ll try to do this

                   

                  Best Regards

                  Gustavo.

                  • 6. Re: call a function when the active document is closed
                    Gustavo Del Vechio Level 3

                    Hi Michael

                     

                    After a time I return to the project which involves this script.

                     

                    Bad news are I have not been able to work with the app.notifiers. Perhaps I´m doing things wrong since I never worked with these events before.

                     

                    But, any way, as you mentioned, if you associate any script to CLOSE in the Script Event Manager directlly, then it runs after document is already closed. And it´s the problem.

                     

                    If...after closing the document, I was able to just catch the name of the closed document, would be sufficient! But it´s not possible.

                     

                    Would you have any other idea on how to do it? Or any simple example??

                     

                    Thank you a lot for your help.

                    Best Regards

                    • 7. Re: call a function when the active document is closed
                      Michael L Hale Level 5

                      As I said before the close event is really not that useful. In addition to the document already being closed the event descriptor will only have the path to the closed file if it was saved as part of the close event.

                       

                      But if you want to play around with this to see if it will be helpful to you here is one way. You will need to add the notifer. This only needs to be done once unless it is removed.

                       

                      app.notifiersEnabled = true;
                      app.notifiers.add( "Cls ",new File('~/desktop/closeEventHandler.jsx') );
                      

                       

                       

                      Then for the event handler script, something like this.

                       

                      try {
                          if (arguments.length >= 2) {
                          var filePath;
                          var desc = arguments[0];
                          if( desc.hasKey( stringIDToTypeID('in') ) ) filePath = desc.getPath(stringIDToTypeID('in')); 
                          if( filePath != undefined ) alert( decodeURI( filePath ) );
                        }
                      } catch (e) {}
                      
                      • 8. Re: call a function when the active document is closed
                        Gustavo Del Vechio Level 3

                        Hi Michael

                         

                        Thank you a lot for the help

                         

                        I´ll try this

                         

                        Thank you!

                         

                        Best Regards

                        Gustavo.