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
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?
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,
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();
}
}
Copy link to clipboard
Copied
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