Copy link to clipboard
Copied
Hi all,
I have a script which needs some work, any pointers or resources would be great!
It works for short files but anything larger and the script stops as it starts adding the pages to the IDMl file instead of the "doc".
I'm not sure why this is happening.
// Opens a folder browser dialog box, where you can choose a folder with the file you want to import.
// The chosen folder is stored inside 'targetFolder'.
// Note: In this script, files will be imported in the order they appear in the folder!
// Stores a reference to the currently open document into which we will import the files (this should be the English master).
importTranslations ();
// Stores the IDML files inside 'fileList'
function importTranslations () { // Create a function which will import the files.
var doc = app.activeDocument,
targetFolder = Folder.selectDialog ("Select a folder with idml translations \r*files must be in the order you want them to be imported*\rFR\rDE\rIT\rES\rNO\rRU\rTR",undefined, "Select a folder"),
TL,
TLpages,
fileList = [];
if ( targetFolder!= null ) {
var fileList = targetFolder.getFiles("*.idml");
} else {
exit(0);
}
for ( var i = 0; i < fileList.length; i++ ) { // Loop through the IDML files inside 'fileList'.
TL = fileList; // Store each file in a variable called 'TL'.
app.open( TL ); // open each file
TLpages = app.activeDocument.pages.everyItem().getElements(); // Store the open IDML file pages inside 'TLpages'.
for ( var j = 0; j <TLpages.length; j++ ) { // Loop through the pages
switch ( fileList ) {
case fileList[0]:
TLpages
break;
case fileList[1]:
TLpages
break;
case fileList[2]:
TLpages
break;
case fileList[3]:
TLpages
break;
case fileList[4]:
TLpages
break;
case fileList[5]:
TLpages
break;
case fileList[6]:
TLpages
break;
}
TLpages
if ( doc.pages.length % 2 == 1 ) { // Add a spread if the number of pages in the main doc is odd
doc.spreads.add( { allowPageShuffle: true } ); //Allow the pages to shuffle
TLpages
} else { TLpages
};
if ( doc.sections.length > 1 ) { // If there are more than 1 section then remove the excess sections.
doc.sections.itemByRange(1, -1 ).remove();
};
}
app.activeDocument.close(SaveOptions.NO); // Close the IDML and don't save.
}
var pages = app.activeDocument.pages.everyItem().getElements();
for ( i = 0; i<pages.length; i++ ) {
if (pages.pageItems.length==0) {
pages.remove();
}
} //delete empty pages
alert ("Files successfully imported", "Confirmation message");
//=================================================END MAIN FUNCTION=================
var pageChecker = (function () {
var myDoc=app.activeDocument;
mylength = myDoc.pages.length,
err=0,
mydiv = ( mylength % 4 ),
myadd = ( 4 - mydiv );
if ( mydiv == 0 ) {
alert( "No issue with Page count. Press OK to go next step." )
};
if ( myadd != 4 ) {
var myconfirmation=confirm ( "Need to add " + myadd + " pages. Shall I add them?" );
if ( myconfirmation ) {
if ( mydiv !=0 ) {
for ( i = 0; i < myadd; i++ ) {
myDoc.pages.add( LocationOptions.BEFORE, myDoc.spreads[-1] );
}
}
}
}
}());
//======================insert an empty page to make the document pages a multiple of 4 (for booklets)
var bm = (function () {
var newDoc = app.activeDocument;
var frame = newDoc.textFrames.everyItem().getElements();
var f = frame.length;
for ( var c = 0; c < f; c++ ) {
var paras = frame
for ( p = 0; p < paras.length; p++ ) {
var langArray = [ "EN", "FR", "DE", "IT", "ES", "NO", "RU", "TR" ];
for ( var i = 0; i < langArray.length; i++ ) {
if ( paras
.contents === langArray ) {
var a = app.activeDocument.hyperlinkTextDestinations.add( paras
);
var bm = app.activeDocument.bookmarks.add( a );
bm.name = paras
.contents;
}
}
}
}
}());
};
Hi Jake.
It is hard to read your code. Without it being formatted it looks a real mess. Please use the advanced editor so you can apply a syntax to your code. I could well be that even after the formatting the code will look a real mess.
I really hardly saw the code but I noticed that you are referring to:
1) doc
2) activeDocument
3) myDoc
Could be more I didn't look for more than 5 seconds.
I don't know if it will make a difference but use only 2 references.
doc and idmlDoc or whatever you want to call
...Copy link to clipboard
Copied
Hi Jake.
It is hard to read your code. Without it being formatted it looks a real mess. Please use the advanced editor so you can apply a syntax to your code. I could well be that even after the formatting the code will look a real mess.
I really hardly saw the code but I noticed that you are referring to:
1) doc
2) activeDocument
3) myDoc
Could be more I didn't look for more than 5 seconds.
I don't know if it will make a difference but use only 2 references.
doc and idmlDoc or whatever you want to call them. Do not make any references to activeDocument by itself, i.e. if doc is the activeDocument refer to the document as doc and not activeDocument.
To define doc try using:
doc = app.documents.itemByID(app.activeDocument.id);
Instead of:
doc = app.activeDocument;
It could be that the activeDocument is switching and when you refer to activeDocument during the execution of the script the activeDocument is the IDML document, by having doc referring to the ID you are keeping it fixed.
Not too sure it will solve your problem but you should give it a go.
HTH
Trevor
Copy link to clipboard
Copied
Thanks Trevor, that was a really useful explanation, it definitely works better using the id property!
However, I think it is messing up on any document that gets a dialog pop up to tell me that links are missing.
When this happens the pages get moved to the end of the IDML and not the activeDocument.
I've tried using userInteractionLevel but it still pops up.
Copy link to clipboard
Copied
Hi,
Supporting Trevor I suggest to set a var mDoc = app.open(TL) ==> to be sure you are reffering to a proper doc.
Looks like another case is switch...case part. You may need to change it alike:
switch(i) {
case 0:
....
case 1:
....
}
Jarek
Copy link to clipboard
Copied
Additionally, it looks like you're looping forward and duplicating. The loop should probably be reversed.