2 Replies Latest reply on Sep 9, 2015 1:55 PM by Trevorׅ

    beforeSave "Cannot delete the target of an active script event" error

    Trevorׅ Adobe Community Professional

      Hi

       

      Can anyone get the bellow snippet to work?

      I can't get the beforeSave event to place an image, I get the "Cannot delete the target of an active script event" error message.

      The same question was asked and not answered here beforeSave "Cannot delete the target of an active script event" error with an applescript snippet.

       

      Thanks

       

      Trevor

       

      #targetengine placeImage
      before = function (evt) {
          var doc, placeFile, newPage;
          doc = evt.target;
          if ( !(doc instanceof Document) ) return;
          placeFile = new File (File.openDialog());
          if (!placeFile) return;
          try {
            newPage = doc.pages.add();
            placedImage = newPage.place(placeFile)[0];
          } catch (e) {
            app.eventListeners.item("onBeforeSave").isValid && app.eventListeners.item("onBeforeSave").remove();
            alert(e); //Cannot delete the target of an active script event" error
          }
      }
      
      
      function registerEventListener() {
        app.eventListeners.item("onBeforeSave").isValid && app.eventListeners.item("onBeforeSave").remove();
        app.eventListeners.add( "beforeSave", before).name = "onBeforeSave";
      }
      
      registerEventListener();
      
      
        • 1. Re: beforeSave "Cannot delete the target of an active script event" error
          Trevorׅ Adobe Community Professional

          I did the following hack but would be happy to hear if anyone else has a better solution.

          1. create a new doc
          2. place the page in the new doc
          3. duplicate the page to the original doc
          4. close the new doc without saving.

           

          Not very nice but seems to work.

          I'll test things out later and mark my answer as correct if all goes well.

           

          Trevor

          • 2. Re: beforeSave "Cannot delete the target of an active script event" error
            Trevorׅ Adobe Community Professional
            #targetengine placeImage
            before = function (evt) {
                var doc, placeFile, temp, tempDoc;
                doc = evt.target;
                if ( !(doc instanceof Document) ) return;
                placeFile = new File (File.openDialog());
                if (!placeFile) return;
                evt.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
                try {
                  tempDoc = app.documents.add(false);
                  temp = tempDoc.spreads[0];
                  placedImage = temp.place(placeFile)[0];
                  temp.duplicate(LocationOptions.BEFORE, doc.spreads[0]);
                  tempDoc.close(SaveOptions.NO);
                } catch (e) {
                  app.eventListeners.item("onBeforeSave").isValid && app.eventListeners.item("onBeforeSave").remove();
                  alert(e); //Cannot delete the target of an active script event" error
                }
            }
            
            
            function registerEventListener() {
              app.eventListeners.item("onBeforeSave").isValid && app.eventListeners.item("onBeforeSave").remove();
              app.eventListeners.add( "beforeSave", before).name = "onBeforeSave";
            }
            
            registerEventListener();
            
            

             

            Works without throwing an error.