3 Replies Latest reply on Sep 28, 2015 12:29 AM by Ronald63

    Save to PDF option

    evan71922978

      Hi !

       

      I've a script who export all page of my document to PDF format in a specific place of my computer whith a specific name.

       

      But some time the file already exist et when I do my export, the old document is replaced with the new, and some times and do want it

       

      I would like, when the document already exist, put a dialog box or alert with "The document [name of the document] already existe, would you replace it ?" with a Yes/No button, if it's yes, do the script normaly, if it's no, don't save the pages concerned (It can have several pages on a document who already exist.) and save the pages who does'nt already existe.

       

      Thank you !

       

      That is my code :

       

      app.activeDocument.dataMergeProperties.mergeRecords(); 
      var myDocument = app.activeDocument;
      main();
             
      function main(){
      exportPDF();  
      
      }                              
      function exportPDF(){          
      
           var myPageName, myFilePath, myFile;           
      
           var myPDFExportPreset = app.pdfExportPresets.item("[Qualité supérieure]");        
      
      for(var myCounter = 0; myCounter < myDocument.pages.length; myCounter++){ 
      myPageName = myDocument.pages.item(myCounter).name;   
      app.pdfExportPreferences.pageRange = myPageName;       
      
           var myRegExp = /:/gi;          
      myPageName = myPageName.replace(myRegExp, "_");    
      myFilePath = Folder.desktop + "/" + extractPAFname(myCounter) + ".pdf";
       myFile = new File(myFilePath);    
      myDocument.exportFile(ExportFormat.pdfType, myFile, false, myPDFExportPreset);
      
      }   
          
      }    
      
      function extractPAFname(AllPages){  
      
           var myStyle = myDocument.paragraphStyles.itemByName('nom_fichier');         
      if (myStyle.isValid){  
      app.findGrepPreferences = app.changeGrepPreferences = null;   
      app.findGrepPreferences.appliedParagraphStyle = myStyle;  
      
           var mySearch = myDocument.findGrep();  
      if (mySearch.length >0){       
      
      return mySearch[AllPages].contents        
      
      }else {  
      
      alert('Erreur - Pas de texte');  
      exit();             
      }         
      
      }else {  
      alert('Erreur - Pas de style');  
      exit();              
      
      }    
                
      }  
      
        • 1. Re: Save to PDF option
          Ronald63 Level 4

          Hi,

           

          Try this

          if(!myFile.exists){
           myDocument.exportFile(ExportFormat.pdfType, myFile, false, myPDFExportPreset);
          }else{
              var mConfirm= confirm ('The document ' + extractPAFname(myCounter) + ' already exist, would you replace it ?', 'Title of window');
              if (mConfirm){
                  myDocument.exportFile(ExportFormat.pdfType, myFile, false, myPDFExportPreset);
              }
          }
          

           

          Regards

          • 2. Re: Save to PDF option
            evan71922978 Level 1

            Hi !

             

            Problem, no Confirm alert and, that replace the file :/

             

            It's possible I've not placed it at the right place ?

             

            app.activeDocument.dataMergeProperties.mergeRecords(); 
            var myDocument = app.activeDocument;
            main();        
            function main(){
            exportPDF();    
            }                              
            function exportPDF(){          
            var myPageName, myFilePath, myFile;           
            var myPDFExportPreset = app.pdfExportPresets.item("[Qualité supérieure]");        
            for(var myCounter = 0; myCounter < myDocument.pages.length; myCounter++){ 
            myPageName = myDocument.pages.item(myCounter).name;   
            app.pdfExportPreferences.pageRange = myPageName;       
            var myRegExp = /:/gi;          
            myPageName = myPageName.replace(myRegExp, "_");    
            myFilePath = Folder.desktop + "/" + extractPAFname(myCounter) + ".pdf";
             myFile = new File(myFilePath);    
            myDocument.exportFile(ExportFormat.pdfType, myFile, false, myPDFExportPreset);  
            }         
            }      
            function extractPAFname(AllPages){  
            var myStyle = myDocument.paragraphStyles.itemByName('nom_fichier');         
            if (myStyle.isValid){  
            app.findGrepPreferences = app.changeGrepPreferences = null;   
            app.findGrepPreferences.appliedParagraphStyle = myStyle;  
            var mySearch = myDocument.findGrep();  
            if (mySearch.length >0){       
            return mySearch[AllPages].contents       
            }else {  
            alert('Erreur - Pas de texte');  
            exit();             
            }         
            }else {  
            alert('Erreur - Pas de style');  
            exit();              
            }   
                   
            }  
            if(!myFile.exists){  
             myDocument.exportFile(ExportFormat.pdfType, myFile, false, myPDFExportPreset);  
            }else{  
                var mConfirm= confirm ('The document ' + extractPAFname(myCounter) + ' already exist, would you replace it ?', 'Title of window');  
                if (mConfirm){  
                    myDocument.exportFile(ExportFormat.pdfType, myFile, false, myPDFExportPreset);  
                }  
            }  
            

             

            Error line 38.

            • 3. Re: Save to PDF option
              Ronald63 Level 4

              Hi,

               

              Paste the snippet like this ...

              app.activeDocument.dataMergeProperties.mergeRecords();   
              var myDocument = app.activeDocument;  
              main();  
              function main(){  
                  exportPDF();  
              }
              
              function exportPDF(){
                  var myPageName, myFilePath, myFile; 
                  var myPDFExportPreset = app.pdfExportPresets.item("[Qualité supérieure]");  
                  for(var myCounter = 0; myCounter < myDocument.pages.length; myCounter++){   
                      myPageName = myDocument.pages.item(myCounter).name; 
                      app.pdfExportPreferences.pageRange = myPageName; 
                      var myRegExp = /:/gi;
                      myPageName = myPageName.replace(myRegExp, "_");  
                      myFilePath = Folder.desktop + "/" + extractPAFname(myCounter) + ".pdf";  
                      myFile = new File(myFilePath);  
                      if(!myFile.exists){
                          myDocument.exportFile(ExportFormat.pdfType, myFile, false, myPDFExportPreset);
                      }else{
                          var mConfirm= confirm ('The document ' + extractPAFname(myCounter) + ' already exist, would you replace it ?', 'Title of window');
                          if (mConfirm){
                              myDocument.exportFile(ExportFormat.pdfType, myFile, false, myPDFExportPreset);
                          }
                      }
                  } 
              }
              
              
              function extractPAFname(AllPages){
                  var myStyle = myDocument.paragraphStyles.itemByName('nom_fichier');   
                  if (myStyle.isValid){
                      app.findGrepPreferences = app.changeGrepPreferences = null; 
                      app.findGrepPreferences.appliedParagraphStyle = myStyle;
                      var mySearch = myDocument.findGrep();
                      if (mySearch.length >0){ 
                          return mySearch[AllPages].contents 
                      }else {
                          alert('Erreur - Pas de texte');
                          exit();   
                      }   
                  }else {
                      alert('Erreur - Pas de style');
                      exit();
                  } 
              }