3 Replies Latest reply: Apr 23, 2015 10:26 AM by MadMax4025 RSS

    Renaming files in a book

    heatherb555 Community Member

      Hi everyone,

       

      I need to rename files in a book, but from within the book. A Save as... won't update the cross refrences.

       

      I see an fcode for this (FCodes.KBD_BOOKRENAMEFILE), but I've been advised not to rely on these types of shortcuts.

       

      I don't see anything else documented in the Scripting Guide. Does anyone else have any other suggestions?

       

      Thanks so much!

       

      Heather A

        • 1. Re: Renaming files in a book
          MadMax4025 Community Member

          Sadly I don't have an answer to this question and I'm hoping to bump this up to see if anybody out there now has an answer.

           

          I've been looking into it and I have found examples in ExtendScript suggesting (for illustrator):

           

          var myfile = File.openDialog( 'select your AI file…' );

          myfile.rename( 'NEW.ai' );

           

          However I want this to be done automatically without opening a dialog.

          I can programmatically cycle through book components and can skip over folders and groups. I'm hoping to rename files to a naming structure based upon the top level element name and it's attributes (all of which I have already done). Now all I need to do is to take the name that has been calculated and replace the old name with it (Similar to how you can simply click on a file in the book and rename it). Any thoughts or suggestions would be greatly appreciated.

           

          The following is what I have been trying so far:

           

          function SaveFM(Item) {

               var NewName,

                    Filetype;


               Filetype = Item.Name.split(".").pop();

           

               //NameEncoder returns a string containing the new file name (no path or file type);

               NewName = NameEncoder(Item, AbbrList) + "." + Filetype;

              

               //If the component being checked is a folder or a group the name returned is "Skip." Files such as TOC or Indexes will return starting with "XXX" to allow them to be skipped as well.

               if (NewName.substring(0, 4) !== "Skip" && NewName.substring(0, 3) !== "XXX") {

                    //I have tried the following, using .Save with the full file path, retProps, and saveProps (though these would probably create a new file which is not the end goal). All of them are throwing an error and I'm not sure what I am doing wrong.

                    Item.rename(NewName);

               }

               return;

          }

           

          ---Corey---

          • 2. Re: Renaming files in a book
            frameexpert Community Member

            Hi Heather,

             

            A book component has a Name property that you can use to rename the file. The name will show in the book window as a relative path from the book, but when setting the Name property, you use an absolute path.

             

            Of course this only changes the name in the book window. To rename the file on disk you can use ExtendScript's File object methods. Then, you will have to open all of the other components in the book and resolve any cross-references that used to point to the renamed document. Basically, you are doing what the built-in command does, but via scripting. If you need help with the individual steps, please let me know.

             

            -Rick

            • 3. Re: Renaming files in a book
              MadMax4025 Community Member

              Hi Rick,

               

              Thanks for the info. I've gotten some code that creates an array of all the files in my book, their old names, and their new names.

               

              However, the "Name" quality of the files are grayed out and for some reason don't change. I can easily change the "Label" and the "PrintFileName" (both of their icon's are blue). Is there some other way I should be doing this?

               

              I've attached the section of my code that is doing the renaming:

               

                  for (var i = 0, len = FileArray.length; i < len; i++) {

                      ThisFile = FileArray[i][0];

                      OldName = FileArray[i][1];

                      NewName = FileArray[i][2];

                      ThisFile.Name = NewName;

                      ThisFile.Label = NewName.split(".").shift(); //since the label does not contain a file type

                      ThisFile.PrintFileName = NewName.split(".").shift() + ".ps" //since file type is .ps

                  }