17 Replies Latest reply on Apr 17, 2015 11:21 AM by JamesHaney

    event scripting issues

    JamesHaney Level 1

      I am trying to get the following script working as a startup script.

       

      the goal is that when the user chooses to save the document, they are presented with a dialog box asking for a quick summary of the edits that were made. Then this info is written to XMP fields.

       

      The core functionality actually works, the dialog prompts and the data gets saved in the XMP.

       

      The problem is that when the save action is initiated an error dialog pops up announcing "The requested action could not be completed because the object no longer exists." and then the script proceeds and functions properly, then it prompts for input and saves the data again.

       

      I get an error, then the script runs twice.

       

      I am confused.

       

       

       

      #targetengine session

      var myEventListener = app.addEventListener("beforeSave", myPromptForInput);

       

      function myPromptForInput(myEvent){

      var myTimeStamp = myEvent.timeStamp

      var myDialog = app.dialogs.add({name:"Enter a simple description of edits made",canCancel:true});

      with(myDialog){

      //Add a dialog column.

      with(dialogColumns.add()){

      //Create a text edit field.

      var myTextEditField = textEditboxes.add({editContents:"Description of document changes", minWidth:300});

      //Display the dialog box.

      var myResult = myDialog.show();

      if(myResult == true){

      //Get the values from the dialog box controls.

      var myNotes = myTextEditField.editContents;

      //Remove the dialog box from memory.

      myDialog.destroy();

      myAddXMPData(myNotes + " - " + myTimeStamp);

      //alert(myEvent);

      }

      }

      }

      }

       

      function myAddXMPData(myNotes){ 

      //var myDocument = app.documents.item(0);

      var myDocument = app.activeDocument;

      with(myDocument.metadataPreferences){

      author = "James Haney";

      description = myNotes;

      var myNewContainer = createContainerItem("http://ns.adobe.com/xap/1.0/", "LCCRevisionNotes");

      setProperty("http://ns.adobe.com/xap/1.0/", "LCCRevisionNotes/*[1]", myNotes);

      }

      }

        • 1. Re: event scripting issues
          Jump_Over Level 5

          Hi,

           

          try it including

               alert (myEvent.currentTarget.constructor.name);

          somewhere inside myPromptForInput() function.

           

          Looks like event is triggered twice. Which objects? doc? app? window?

           

          Jarek

          • 2. Re: event scripting issues
            JamesHaney Level 1

            I inserted that and both times it reports that "Application" is the current Target.

            • 3. Re: event scripting issues
              Jump_Over Level 5

              Hi,

               

              #targetengine session         

                                                 ==> change it into string "session"


              Jarek

              • 4. Re: event scripting issues
                JamesHaney Level 1

                Tried it with #targetengine "session"


                It still runs twice.

                • 5. Re: event scripting issues
                  Jump_Over Level 5

                  Hi,

                   

                  I am not sure if UI dialog and #targetengine work together.

                  Try to use SUI window perhaps...

                   

                  I run it on my side. Looks like it works.

                   

                  #targetengine 'session'
                  //just to remove if exists
                  var old1 = app.eventListeners.itemByName("mXMP");
                  if (old1.isValid) old1.remove();
                  //
                  var myEventListener = app.addEventListener("beforeSave", function (myEvent) {
                    var myTimeStamp = myEvent.timeStamp;
                    var myDialog = new Window("dialog", "Enter a simple description of edits made", undefined, {closeButton: false});
                    var myStatText = myDialog.add("statictext", undefined, "Description of document changes: ");
                    var myEditText = myDialog.add("edittext", undefined, "");
                    var myButtons = myDialog.add("group");
                    myButtons.add("button", undefined, "OK", {name:"OK"});
                    myButtons.add("button", undefined, "Cancel", {name:"Cancel"});
                  
                    myEditText.characters = 45;
                    myStatText.alignment = "left";
                  
                    //Display the dialog box.
                    var myResult = myDialog.show();
                    if(myResult == true){
                    //Get the values from the dialog box controls.
                    var myNotes = myEditText.text;
                    myAddXMPData(myNotes + " - " + myTimeStamp);
                    //~ //alert(myEvent);
                    }
                  }).name = "mXMP";
                  
                  
                  

                   

                  Including your function myAddXMPData() of course.

                   

                  Jarek

                  • 6. Re: event scripting issues
                    JamesHaney Level 1

                    Great troubleshooting.

                     

                    That was it. Thanks for the help.

                    • 7. Re: event scripting issues
                      JamesHaney Level 1

                      I had a strange problem today.

                       

                      InDesign was running and everything was working fine.

                       

                      Then, when I launched Illustrator I got the following error rapidly in succession three times.

                       

                      Error in /Library/Application Support/Adobe/Startup Scrippts CC/Adobe InDesign/promptForUserInputOnSaveXMP2.jsx

                      Line 5: var old1 = app.eventListeners.itemByName("mXMP");

                      undefined is not an object

                       

                      Line 5 is the first line of the script:

                      var old1 = app.eventListeners.itemByName("mXMP");

                       

                      Is there a way to check what app is generating the event and ignoring it if it isn't "Adobe InDesign"?

                      • 8. Re: event scripting issues
                        Jump_Over Level 5

                        Hi,

                         

                        Place:

                            #target indesign

                        in very first line

                         

                        Where is your script located to be a startup?

                         

                        Jarek

                        • 9. Re: event scripting issues
                          JamesHaney Level 1

                          My script is located here:

                          /Library/Application Support/Adobe/Startup Scripts CC/Adobe InDesign/promptForUserInputOnSaveXMP2.jsx

                           

                          I tried

                          #target indesign

                          as well as

                          #target "Adobe InDesign"

                           

                          but the behavior is the same. I still get the errors when starting up Illustrator.

                          • 10. Re: event scripting issues
                            Jump_Over Level 5

                            Hi,

                             

                            Weird.

                            Is it possible some script version was saved in location where Illustrator startup is connected? User or application path?

                             

                            Jarek

                            • 11. Re: event scripting issues
                              Laubender Adobe Community Professional & MVP

                              @Jarek – I tested your code as well.

                              Even without starting Illustrator I can see one warning message. The script is running fine as is.

                              If I add something to InDesign's XMP data and do a document save your editing window is showing up and I can edit a text field.

                              After hitting ok, a warning message is coming up.

                               

                              1. Unsaved named document.

                               

                              2. After working the keyboard shortcut for save cmd + s your text edit field is coming up:

                              3-SaveIsShowingEditField.png

                               

                              3. Adding some text:

                              4-TextEditField-TextAdded.png

                               

                              4. Warning message is popping up after hitting OK:

                               

                              5-WarningMessage.png

                               

                              This warning message is quite different from James' error message:

                               

                              Warning

                              An attached script is causing the following error:

                               

                              myAddXMPData is not a function

                               

                              Would you like to disable this event handler?

                              [ ] Do not show again

                              [No] [[Yes]]

                               

                               

                              Tested with InDesign CC 2014.2 v10.2.0.69 on Mac OSX 10.7.5.

                              Script is saved in folder:

                               

                              Applications/Adobe InDesign 2014/Scripts/startup scripts/Get-myXMP-atSave-Dummy.jsx

                               

                              Uwe

                              • 12. Re: event scripting issues
                                Laubender Adobe Community Professional & MVP

                                I cannot see a problem with starting Illustrator or saving AI files in tandem with InDesign open and saving documents there.

                                 

                                Uwe

                                • 13. Re: event scripting issues
                                  Jump_Over Level 5

                                  Hi Uwe,

                                   

                                  Right, all that cause I skip one function seems not generating any errors

                                   

                                  function myAddXMPData(myNotes){
                                    //var myDocument = app.documents.item(0);
                                    var myDocument = app.activeDocument;
                                    with(myDocument.metadataPreferences){
                                    author = "James Haney";
                                    description = myNotes;
                                    var myNewContainer = createContainerItem("http://ns.adobe.com/xap/1.0/", "LCCRevisionNotes");
                                    setProperty("http://ns.adobe.com/xap/1.0/", "LCCRevisionNotes/*[1]", myNotes);
                                    }
                                    }
                                  

                                  Notice my adnotation at the bottom of post #5

                                   

                                  Jarek

                                  • 14. Re: event scripting issues
                                    JamesHaney Level 1

                                    You were using a partial script, the entire script is below.

                                     

                                     

                                    After looking at your setup I changed one thing.

                                    I was placing the startup script here:

                                    /Library/Application Support/Adobe/Startup Scripts CC/Adobe InDesign/promptForUserInputOnSaveXMP2.jsx

                                     

                                    I moved it to

                                    /Applications/Adobe InDesign CC 2014/Scripts/startup scripts/promptForUserInputOnSaveXMP2.jsx

                                     

                                    and now everything works fine. No conflict with Illustrator.

                                     

                                     

                                    //code follows

                                     

                                    #targetengine 'session'

                                    //just to remove if exists

                                    //alert(myEvent.currentTarget.name);

                                     

                                     

                                    var old1 = app.eventListeners.itemByName("mXMP");

                                    if (old1.isValid) old1.remove();

                                     

                                     

                                    var myEventListener = app.addEventListener("beforeSave", function (myEvent) {

                                     

                                      var myTimeStamp = myEvent.timeStamp

                                      var myDialog = new Window("dialog", "Enter a simple description of edits made", undefined, {closeButton: false});

                                      var myStatText = myDialog.add("statictext", undefined, "Description of document changes: ");

                                      var myEditText = myDialog.add("edittext", [0, 0, 300, 70], "",{multiline:true, alignment:"top"});

                                      //var myUserNameText = myDialog.add("User Name", undefined, "");

                                      var myButtons = myDialog.add("group");

                                      myButtons.add("button", undefined, "OK", {name:"OK"});

                                      myButtons.add("button", undefined, "Cancel", {name:"Cancel"});

                                      myEditText.characters = 45;

                                      myStatText.alignment = "left";

                                      myEditText.active = true;

                                     

                                     

                                      //Display the dialog box.

                                      var myResult = myDialog.show();

                                      if(myResult == true){

                                      //Get the values from the dialog box controls.

                                      var myNotes = myEditText.text;

                                      //Remove the dialog box from memory.

                                      myAddXMPData("\n" + myNotes + "\n" + myTimeStamp);

                                      //~ //alert(myEvent);

                                      }

                                    }).name = "mXMP";

                                    /*

                                    • 15. Re: event scripting issues
                                      Laubender Adobe Community Professional & MVP

                                      @Jarek – yes, I did not look at the function call after:

                                       

                                      var myNotes = myEditText.text;

                                      myAddXMPData(myNotes + " - " + myTimeStamp);

                                       

                                      And missed your annotation as well…

                                      Oh dear!

                                       

                                      Uwe

                                      • 16. Re: event scripting issues
                                        Laubender Adobe Community Professional & MVP

                                        /Library/Application Support/Adobe/Startup Scripts CC/Adobe InDesign/promptForUserInputOnSaveXMP2.jsx

                                         

                                        @James – when looking at your path where you placed the script the first time when Adobe Illustrator reacted on it, could it be, that the name of the folder is only cosmetic?
                                        Maybe all Adobe apps capable of using ExtendScript can react to start up-scripts in that folder?

                                         

                                        In my system of OSX 10.7.5 the folder "Startup Scripts CC" does not exist at all.

                                         

                                        Instead I would have put the file to as an alternative to the applications folder:

                                        User/[UserName]/Library/Preferences/Adobe InDesign/Version 10.0/Scripts/startup scripts/

                                        (I would have to create the folder "startup scripts" first)

                                         

                                        James, what is your exact OS and InDesign version?

                                         

                                        Uwe

                                        • 17. Re: event scripting issues
                                          JamesHaney Level 1

                                          One last follow up question.

                                           

                                          I have seen a lot of eventListener scripts now and most of them don't have code like you use here in the beginning of the program.

                                           

                                          Why is it needed?

                                           

                                          var old1 = app.eventListeners.itemByName("mXMP");

                                          if (old1.isValid) old1.remove();