2 Replies Latest reply on Jan 17, 2011 3:06 PM by PedroCortezMarques

    User window variable verification / NaN and File.exists

    Pedro Cortez Marques Level 3

      I have a script window to collect the number of a file that must be written by hand and put it inside the name of the jpg image I have to save:

       

      This code is already doing the job well, but I need 2 things:

       

      1)  when the user presses OK button, the script must verify the input string "myEditText.text" is a Number (1, 2, 3, ...., n)

           Not a letter / not a blank space

       

      2) Then it must verify if the File() name of the new image to save already exists.

           I had already success on this verification but I only have manage to do it AFTER the OK button is pressed (I need it in the moment the user writes the number on the window or, if is not possible, after the OK button (opening repeatedly the input window)

           If the new file already exists, I need also to ask the user if he wants to over save the file or, if not, retype the new number to build the new variable of the named image

       

       

      Here is the part of the code I am using:

       

      (...)

       

      var myDialog = new Window('dialog', 'Gravar Número:'); 
      myDialog.size = {width:600, height:200};
      var myEditText = myDialog.add('edittext');
      myEditText.active = true;
      myEditText.preferredSize = {width:40, height:20};
      var myNewName = myEditText.text;
      var myGroup = myDialog.add('group');
      myGroup.orientation = 'row';
      var myOkBtn = myGroup.add('button', undefined, 'OK', {name:'ok'});
      var myCancelBtn = myGroup.add('button', undefined, 'Cancelar', {name:'cancelar'});
      //
      if (myDialog.show() == 1) {
             
          var myNewName = myEditText.text; // reads the number inserted
          if (myNewName != "") {
          // the new name of the File to save:
          myNewName = myDocName.match(/^\d{8}/) +"_v"+myNewName + ".jpg";
          // verify if it exists
          verificaFile = File(outFolderPress +"/"+myNewName);
          if (verificaFile.exists) {
              alert("That image already exists.");
          }

       

       

      Can someone help me or at least give some advice?

        • 1. Re: User window variable verification / NaN and File.exists
          Paul Riggott Level 6

          Please try this...

          var myDialog = new Window('dialog', 'Gravar Número:');  
          myDialog.size = {width:600, height:200};
          var myEditText = myDialog.add('edittext');
          myEditText.active = true;
          myEditText.preferredSize = {width:40, height:20};
          var myNewName = myEditText.text;
          var myGroup = myDialog.add('group');
          myGroup.orientation = 'row';
          var myOkBtn = myGroup.add('button', undefined, 'Process');
          var myCancelBtn = myGroup.add('button', undefined, 'Cancelar', {name:'cancelar'});
          //
          myEditText.onChanging = function() { 
            if (this.text.match(/[^\d]/)) { 
              this.text = this.text.replace(/[^\d]/g, ''); 
            } 
          }
          myCancelBtn.onClick= function(){
               myDialog.close(2);
              }
          myOkBtn.onClick= function(){
             if(myEditText.text == ''){
                 alert("No number has been entered!");
                 return;
                 }
           // outFolderPress="/c/temp";
           //  myDocName="123472454145_hdghd78.jpg";
          var  verificaFile = File(outFolderPress +"/"+myDocName.match(/^\d{8}/) +"_v"+myNewName + ".jpg");
              if (!verificaFile.exists) {
                 var overwrite = confirm("That image already exists,\n Do you want to overwrite the file?.");
                  if(!overwrite) return;
              }
           myDialog.close(1);
           // do the rest of your processing here...
          }
          myDialog.show();
                  
          
          
          
          • 2. Re: User window variable verification / NaN and File.exists
            PedroCortezMarques

            Thats it Paul!

             

            Thanks.

             

            I had a Number verification much more complex. This will do.

            The .onChanging is great.