3 Replies Latest reply on Jul 11, 2014 3:09 AM by JJMack

    How to delete a document if I press escKey while running a script. Please help...

    ryanvolap

      Hi,

      I hope you guys are well.

      I've been writing a script (Javascript ) for my workflow like importing images, creating new layers and layer adjustment.

      When a dialog appear, all the buttons work correctly. But before that, if I press the ecs key while the script is running (creating layers and adjustment layers), the script stops but it doesn't delete a document like my cancel button does.

      How do I fix that.

      Thank you so much.

      Nico,

        • 1. Re: How to delete a document if I press escKey while running a script. Please help...
          JJMack Most Valuable Participant

          Perhaps if you embed the interactive adjustment process in a Try Catch you can catch you used the esc key, It sounds like the esc key cause an error that cause the script to end.   If you catch the error you can delete the document in the catch process.

           

          If there is no error that esc key just terminates the running script. Don't use the ecs to kill the script. Program in a process to handle keyboard key presses that you can trigger your script to delete the document and end the scrip's executiom

          • 2. Re: How to delete a document if I press escKey while running a script. Please help...
            ryanvolap Level 1

            Hi JJMack!

            Do you have an example of handling keyboard that I can trigger to delete a document?

            I'm sorry for asking to much.

            Thank you so much for your time.

            Nico,

            • 3. Re: How to delete a document if I press escKey while running a script. Please help...
              JJMack Most Valuable Participant

              I relay don't know javascript I mostly search and cut an paste what I need. I did need to handle keys for one of my hacks.  To allow only some keys in a dialog fields.   I do not know if code like that would be useful for what you want to do. For there is no dialog active when you want to cancel your script.  You may need to insert some other type of keyboard monitor or code that you can trigger some way that will signal the script to delete the document and terminate.  The Script I have keyboard handler is is my PasteImageRoll script http://www.mouseprints.net/old/dpr/PasteImageRoll.jsx

               

              If you look at my PasteImageRoll  script you will see that all the main code is within a "try catch"  at any point in the main code I can through an error the catch will be triggered and can act on the error code.  In you case the error code would cayse the catch routine to delete the document and end execution perhaps alert the user the his action deleted the document and ended the script execution. 

               

              My PasteImageRoll catch is quite simple

               

              catch(err){

              app.preferences.rulerUnits = startRulerUnits; // Restore  the app preferences

              if (err=="error1") {alert("Paper width exceeded reduce the cell width");}

              else if (err=="error2") {alert("Paper roll length exceeded reduce cell height");}

              else if (err=="error3") {alert("No Images Selected");}

              else if (err=="error4") {alert("Paper roll length exceeded try selecting fewer images or reducing cell height");}

              // Lot's of things can go wrong, Give a generic alert and see if they want the details

              else if ( confirm("Sorry, something major happened and I can't continue! Would you like to see more info?" ) ) { alert(err + ': on line ' + err.line ); }

              }