• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Script only runs in the Extendscript toolkit

Engaged ,
Oct 11, 2016 Oct 11, 2016

Copy link to clipboard

Copied

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.pages.everyItem().getElements().length; j++ ) { //loop through the pages
   
                            if ( openDocs.pages==openDocs.pages[0]||openDocs.pages==openDocs.pages[-1] ){
       
                                openDocs.pages.remove(); //remove the first and last page
                               
                               
                               
                               
                                var newDoc1 = new File ( "A:/Work in progress/Jake/SL Translations/"+openDocs.name.substr( 0, openDocs.name.lastIndexOf(".") ) + ".idml" ); //create a new file for each doc
          
                                openDocs.exportFile(ExportFormat.INDESIGN_MARKUP,newDoc1); //export the new file as idml

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

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

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

   
    }

Does anyone have any idea why this happens?

Maybe it's my code?

TOPICS
Scripting

Views

381

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

People's Champ , Oct 11, 2016 Oct 11, 2016

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 );

  }

}

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,

...

Votes

Translate

Translate
Enthusiast ,
Oct 11, 2016 Oct 11, 2016

Copy link to clipboard

Copied

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;

    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();

  }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Oct 11, 2016 Oct 11, 2016

Copy link to clipboard

Copied

LATEST

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 );

  }

}

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines