1 Reply Latest reply on Oct 27, 2015 11:22 AM by S Hopkins

    How do i write a javascript code to open InDesign and import template paragraphs & character styles

    karthiks94217840 Level 2

      Dear Friends,

       

      I am leaning stage in the java script code friends.

       

      My Requirements:

       

      I want to open Indesign (Previous_edition_InDesign_file), than import the selected template - all paragraphs & character styles. That styles all delete and replaced with old  Previous_edition_InDesign styles.

       

      Advanced thanks friends,


      Thanks,

      Karthik S

        • 1. Re: How do i write a javascript code to open InDesign and import template paragraphs & character styles
          S Hopkins Adobe Community Professional

          Karthik,

          Hope the following discussion and script helps:

          First: define location of template. I suggest (if you have admin privileges) to use a folder named Templates inside the application folder.

          If the name of the template is not hard-coded in the script, you will need to have the user supply the name. Here we will assume the name of the template is hard-coded in the script.

          var fileName = "NameOfTemplate.indt"

          var folderPath = app.filePath + "/Templates";

          try {

             var fileRef = getFileRef (folderPath, fileName);

          } catch (e) {

             alert (e);

          }

          Next: make sure the document you want updated has been saved with possibly a backup file

          var docRef = app.documents.item(0);

           

           

          Here's the script:

          //assumes target document is open. Change name for template name for variable fileName.

          var fileName = "MyPublication.indt"

          var folderPath = app.filePath + "/Templates";

          try {

              var fileRef = getFileRef (folderPath, fileName);

              } catch (e) {

                  alert (e);

              }

          var docRef = app.documents.item(0);

          //by default, globalClashResolutionStrategy is LOAD_ALL_WITH_OVERWRITE

          docRef.importStyles(ImportFormat.TEXT_STYLES_FORMAT, fileRef, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE);

           

              //function returns reference to file if both folder and file are found

              function getFileRef (folderPath, fileName) {

                  if (Folder(folderPath).exists == false) {

                      throw ("Folder referenced not found");

                  }

                 var filePath = folderPath + "/" + fileName;

                 if (File (filePath).exists == false) {

                     throw ("File referenced not found");

                 }

                 return File(filePath);

             }