0

I am new to indesign scripting. I would like some pointers in writing jsx scripts. I have an indesign template( indt file )and I have to create a indd file out of it (just a copy). Then I have to replace first/last name and date etc with a content I read from an xml.

I did like this so far:

function buildIndesignDocument(templatePath, templateFileName, targetPath, xmlFile, orderNumber, jobNumber) {
    DEBUGG("buildIndesignDocument execution start");

    //check wheher target path is writable.  If not exit from further processing
    var canSaveToServerFlag = checkServerWritable(targetPath.toString());
    if(!canSaveToServerFlag) {
        DEBUGG("Exiting from the process");
        return;
    }else{
        var originalTemplateFilePath = templatePath+templateFileName;
        DEBUGG("Original Template Path is " + originalTemplateFilePath);
        var originalTemplate = new File(originalTemplateFilePath);
        var filePrefix =jobNumber.toString()+"_"+orderNumber.toString()+ "_";

        if (originalTemplate.exists) {
            //create a copy of the template in target folder and name it as indd
            targetFileName= filePrefix+templateFileName.replace(".indt", ".indd");
            DEBUGG("target FileName is " + targetFileName);
            var targetFile =targetPath.toString() + targetFileName ;
            DEBUGG("target FilePath and name is " + targetFile);
            var targetFileCreated=originalTemplate.copy(File(targetFile));
            //  if(targetFileCreated.exists){
                // TODO####hardcoded true need to change
                if(true) {
                    DEBUGG("targetFile Created Successfully" + targetFileCreated);

                    var xmlFormFile =getGenericXmlFile(xmlFile);
                    DEBUGG("xmlFormFile is -----------" +xmlFormFile);
                    var firstName = xmlFormFile.xpath("/formData/firstName");
                    var lastName = xmlFormFile.xpath("/formData/lastName");
                    DEBUGG("firstName----------"+firstName  + "  and last name is ----------" +lastName );

                    //Open the indd file and replace the content with form data preserving format and style
                    var inddFile = new File(targetFile);
                    try {
                        DEBUGG("inside try");

                        //open file for write -mode w is write
                        inddFile.open ("w", null, null);
                        var myDoc=inddFile.read ();
                        DEBUGG("after opening"+myDoc.toString());

                        inddFile.close;
                    } catch(e) {
                        DEBUGG("Exception in opening " + e.description);
                        inddFile.close;
                    }
                } else {
                    DEBUGG("targetFile did not create successfully");
                }
            } else {
                DEBUGG("Template not Found");   
                logError("Missing template file with name =="  +originalTemplateFilePath);
            }
            DEBUGG("buildIndesignDocument execution end");
        }
    }
}

I am unable to open file properly and replace the content. Can someone shed some light on this.

Nic3500
  • 8,144
  • 10
  • 29
  • 40
  • Have you tried opening the duplicated/renamed file manually? If yes did that work? What if you use `var myDocument = app.open(File(targetFile))`? – cybernetic.nomad Aug 15 '18 at 04:09
  • I did but it says app.open is not recognized. I am using ExtendedToolKit to run the script. Should I do something else also – DesignJoe Aug 15 '18 at 17:18
  • in ExtendedToolKit make sure you select Indesign in the top left menu or add #target InDesign as the first line of your script – Nicolai Kant Aug 17 '18 at 09:45

1 Answers1

0

For opening the template and saving it, try the following:

function buildIndesignDocument(templatePath, templateFileName, targetPath, xmlFile, orderNumber, jobNumber) {
        var myDocument = app.open(File(templatePath+"/"+templateFileName));

// do what you want with the document here

        var targetFileName= targetPath+"/"+templateFileName.replace(".indt", ".indd");
        myDocument.save(new File(targetFileName));
 }
cybernetic.nomad
  • 6,100
  • 3
  • 18
  • 31