2 Replies Latest reply on Oct 11, 2016 3:33 AM by Loic.Aigon

    Script only runs in the Extendscript toolkit

    jakec88782761 Level 1

      I've got a script which works perfectly from the toolkit but throws an error if it's run from the script panel.

       

      The error suggests that it's trying to export the same file again rather than the next file in the loop.

       

      var doc = app.activeDocument,
      pages = doc.pages.everyItem().getElements();

      function translationExport () {
         
                var openDocs = app.documents.everyItem().getElements(); //get an array of all open docs
         
              if ( confirm( "Do you want to export all open documents?", undefined, "Exporter" ) ) { //if the user clicks OK
                 
      //============================================if the user clicks YES========================================================          
                 
                  for ( var i = 0; i<openDocs.length; i++) { //loop through the open docs
                 
                          for ( var j = 0; j<openDocs[i].pages.everyItem().getElements().length; j++ ) { //loop through the pages
         
                                  if ( openDocs[i].pages[j]==openDocs[i].pages[0]||openDocs[i].pages[j]==openDocs[i].pages[-1] ){
             
                                      openDocs[i].pages[j].remove(); //remove the first and last page
                                     
                                     
                                     
                                     
                                      var newDoc1 = new File ( "A:/Work in progress/Jake/SL Translations/"+openDocs[i].name.substr( 0, openDocs[i].name.lastIndexOf(".") ) + ".idml" ); //create a new file for each doc
                
                                      openDocs[i].exportFile(ExportFormat.INDESIGN_MARKUP,newDoc1); //export the new file as idml

                                      var newDoc2 = new File ( "A:/Work in progress/Jake/SL Translations/"+openDocs[i].name.substr( 0, openDocs[i].name.lastIndexOf(".") ) + ".pdf" );

                                      openDocs[i].exportFile( ExportFormat.PDF_TYPE, newDoc2, false, "Jake pdf" ); //export the new file as PDF
                                     
                                     
                         
                       };
             
             
                  };
             
                  openDocs[i].undo(); //Put back the removed pages

                 openDocs[i].undo(); //Put back the removed pages
          
              };
        

       

         
          }

       

       

      Does anyone have any idea why this happens?

       

      Maybe it's my code?

        • 1. Re: Script only runs in the Extendscript toolkit
          Kai Rübsamen Adobe Community Professional

          I did not find the error in your code, but assume, that it will work, if you spent some clean up ;-)

           

          Not tested:

           

          var openDocs = app.documents;
          var nDocs = openDocs.length;
          
          if (confirm( "Do you want to export all open documents?", undefined, "Exporter")) {
          
            for (var i = 0; i < nDocs; i++) {
              var curDoc = openDocs[i];
              var allPages = curDoc.pages;
              try {
                allPages[0].remove();
                allPages[-1].remove();
               }
              catch(e) {}
          
              var newDoc1 = new File( "~/Desktop/"+ curDoc.name.replace(/\.indd$/i,".idml"));
              curDoc.exportFile(ExportFormat.INDESIGN_MARKUP, newDoc1);
          
              var newDoc2 = new File("~/Desktop/"+ curDoc.name.replace(/\.indd$/i,".PDF"));
              curDoc.exportFile( ExportFormat.PDF_TYPE, newDoc2, false, "Jake pdf");
              
              curDoc.undo();
              curDoc.undo();
            }
          }
          
          1 person found this helpful
          • 2. Re: Script only runs in the Extendscript toolkit
            Loic.Aigon Adobe Community Professional

            I don't see why it would work in the ESTK and not within the app but anyway here is another script that intends to make things clearer

             

            var main = function() {
              var docs = app.documents,
              n = docs.length;
            
              while ( n-- ) {
              processDoc ( docs[n] );
              }
            
            }
            
            
            var processDoc = function(doc) {
            
            
              var foPath = "A:\\Work in progress\\Jake\\SL Translations";
              var docName = doc.name.replace (".indd", "");
              var docFile = File ( Folder.temp+"/" +doc.name );
              doc.saveACopy ( docFile );
              var tmpdDoc = app.open ( docFile, false );
              var idmlFile = File ( foPath+"/idml/"+docName+".idml" );
              var pdfFile = File ( foPath+"/pdf/"+docName+".pdf" );
            
              tmpdDoc.pages.length>1 && tmpdDoc.pages[-1].remove();
              tmpdDoc.pages.length>1 && tmpdDoc.pages[0].remove();
            
              tmpdDoc.exportFile ( ExportFormat.INDESIGN_MARKUP, idmlFile );
              tmpdDoc.exportFile ( ExportFormat.PDF_TYPE, pdfFile ); //…set your preset
            
              tmpdDoc.close ( SaveOptions.NO );
            }
            
            
            var u;
            
            
            app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
            
            

             

            HTH

             

            Loic

            www.ozalto.com

            1 person found this helpful