我是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");
}
}
}
我无法正确打开文件并替换内容。有人可以对此有所了解吗?
要打开模板并保存它,请尝试以下操作:
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));
}