Skip to main content
Inspiring
February 11, 2019
Answered

Rename book with the name of the first chapter.

  • February 11, 2019
  • 4 replies
  • 606 views

var myFolder = Folder.selectDialog( "Selecione a pasta dos arquivos para criação do Book." ); 

if ( myFolder != null ) { 

     var myFiles = []; 

      

     GetSubFolders(myFolder); 

      

     if ( myFiles.length > 0 ) { 

          var myBookFileName = myFolder + "/"+ myFolder.name + ".indb"; 

          myBookFile = new File( myBookFileName ); 

          if ( myBookFile.exists ) { 

               if ( app.books.item(myFolder.displayName + ".indb") == null ) { 

                    myBook = app.open( myBookFile ); 

               } 

          } 

          else { 

                myBook = app.books.add( myBookFile ); 

              

                myBook.automaticPagination = false; 

                myBook.synchronizeBulletNumberingList = false

                myBook.synchronizeCellStyle= false

                myBook.synchronizeCharacterStyle= false

                myBook.synchronizeConditionalText= false

                myBook.synchronizeCrossReferenceFormat= false

                myBook.synchronizeMasterPage= false

                myBook.synchronizeObjectStyle= false

                myBook.synchronizeParagraphStyle= false

                myBook.synchronizeSwatch= false

                myBook.synchronizeTableOfContentStyle= false

                myBook.synchronizeTableStyle= false

                myBook.synchronizeTextVariable= false

                myBook.synchronizeTrapStyle= false

               

                for ( i=0; i < myFiles.length; i++ ) { 

                    myBook.bookContents.add( myFiles ); 

                } 

                myBook.save(); 

           } 

     } 

 

//=================================== FUNCTIONS ========================================= 

function GetSubFolders(theFolder) { 

     var myFileList = theFolder.getFiles(); 

     for (var i = 0; i < myFileList.length; i++) { 

          var myFile = myFileList

          if (myFile instanceof Folder){ 

               GetSubFolders(myFile); 

          } 

          else if (myFile instanceof File && myFile.name.match(/\.indd$/i)) { 

               myFiles.push(myFile); 

          } 

     } 

}

I have this code to create book based on the folder where the file is, but how would you rename the book by the name of the first chapter of the book? Thank you for your help.

This topic has been closed for replies.
Correct answer Sunil Yadav

Try this code for renaming your book,

renameBook(app.books[0]);

function renameBook(myBook){

    var filePath = myBook.filePath;

    if(myBook.bookContents.length > 0){

        var firstDocName = myBook.bookContents[0].name;

        var prevBookName =myBook.name.toString();

        myBook.close(SaveOptions.YES);

        var mybookFiles = filePath.getFiles(prevBookName)[0];

        var newFile = filePath+"\\"+firstDocName.toString().replace(".indd", ".indb");

        mybookFiles.copy(filePath+"\\"+firstDocName.toString().replace(".indd", ".indb"));

        mybookFiles.remove();

        app.open(File(newFile));

        }

    }

Best

Sunil

4 replies

Inspiring
February 12, 2019

Thank you guys, you guys are awesome.

It's lovely to see the willingness to help you here.

Pater Kahrel, I am your fan, here in Brazil you are our inspiration to learn more about it. Thank you.

Peter Kahrel
Community Expert
Community Expert
February 12, 2019

Simply save the book using the book's first document's name, then delete the original book:

book = app.books[0].fullName;

doc1 = app.books[0].bookContents[0].fullName;

newName = (doc1.path+'/'+doc1.name).replace(/\.indd$/, '.indb');

app.books[0].save (File(newName));

book.remove();

P.

spicyDoge
Known Participant
February 12, 2019

idk where your chapter is, but just grab the object/file & swap the file name. you can use book.fullName and set it to the myChapter.fullName and see if that works

Sunil Yadav
Sunil YadavCorrect answer
Legend
February 12, 2019

Try this code for renaming your book,

renameBook(app.books[0]);

function renameBook(myBook){

    var filePath = myBook.filePath;

    if(myBook.bookContents.length > 0){

        var firstDocName = myBook.bookContents[0].name;

        var prevBookName =myBook.name.toString();

        myBook.close(SaveOptions.YES);

        var mybookFiles = filePath.getFiles(prevBookName)[0];

        var newFile = filePath+"\\"+firstDocName.toString().replace(".indd", ".indb");

        mybookFiles.copy(filePath+"\\"+firstDocName.toString().replace(".indd", ".indb"));

        mybookFiles.remove();

        app.open(File(newFile));

        }

    }

Best

Sunil