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

duplicate pages and move to another file

Engaged ,
Jan 06, 2017 Jan 06, 2017

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.pageColor=UIColors.BLUE; // Apply page color attributes depending on the order of the files in the folder.
                                                                       
                                                                       
                                                                        break;
                                                                       
                                                                    case fileList[1]:
                                                                   
                                                                        TLpages.pageColor=UIColors.YELLOW;
                                                                       
                                                                        break;
                                                                       
                                                                    case fileList[2]:
                                                                   
                                                                        TLpages.pageColor=UIColors.VIOLET;
                                                                       
                                                                        break;
                                                                       
                                                                    case fileList[3]:
                                                                   
                                                                        TLpages.pageColor=UIColors.ORANGE;
                                                                       
                                                                        break;
                                                                       
                                                                    case fileList[4]:
                                                                   
                                                                        TLpages.pageColor=UIColors.WHITE;
                                                                       
                                                                        break;
                                                                       
                                                                    case fileList[5]:
                                                                   
                                                                        TLpages.pageColor=UIColors.GREEN;
                                                                       
                                                                        break;
                                                                       
                                                                     case fileList[6]:
                                                                   
                                                                        TLpages.pageColor=UIColors.RED;
                                                                       
                                                                        break;
                                                                       
                                                               }
                                                                   
                                                                                    
                    TLpages.duplicate(); //  Duplicate the pages
                                                   
                                              
                    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.move( LocationOptions.AFTER, doc.pages[-1] );
                       
                                                   
        
                     } else { TLpages.move( LocationOptions.AFTER, doc.pages[-1] );  // If the number of pages is even then just move the pages to the end of the main doc.
                                                   
             };
       
                                         
                   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.paragraphs.everyItem().getElements();
        
                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;
                   
                                }
                           
                            }
                        }
                    }
                }());

};

TOPICS
Scripting

Views

973

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

Guru , Jan 08, 2017 Jan 08, 2017

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

...

Votes

Translate

Translate
Guru ,
Jan 08, 2017 Jan 08, 2017

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

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
Engaged ,
Jan 10, 2017 Jan 10, 2017

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.

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
Mentor ,
Jan 09, 2017 Jan 09, 2017

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

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
LEGEND ,
Jan 10, 2017 Jan 10, 2017

Copy link to clipboard

Copied

LATEST

Additionally, it looks like you're looping forward and duplicating. The loop should probably be reversed.

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