Indesign jsx脚本请求指针

时间:2018-08-14 20:40:07

标签: scripting jsx adobe-indesign

我是不熟悉indesign脚本的人。我想要一些编写jsx脚本的指针。我有一个indesign模板(indt文件),我必须从中创建一个indd文件(只是一个副本)。然后,我必须用从xml中读取的内容替换姓氏/名字和日期等。

到目前为止,我确实喜欢这样:

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");
        }
    }
}

我无法正确打开文件并替换内容。有人可以阐明这一点吗?

1 个答案:

答案 0 :(得分:0)

要打开模板并保存模板,请尝试以下操作:

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));
 }