InDesign用PDF書き出しスクリプト | おいしいAS3

InDesign用PDF書き出しスクリプト

InDesignCS5で複数のinddファイルの
PDFをまとめて書き出すためのスクリプト




var ddlArr,NG

init()
if(!NG)main()

function init(){
/*──────────
ダイアログを作る
──────────*/

var lstArr=[["開いているドキュメント全部","全ブックのドキュメント全部","選択フォルダ内のindd全部"],"から",
["inddと同じフォルダ","各PDFフォルダ","選択フォルダ内"],"に",
getPDFpresets(),"で書き出す",
"すでにPDFがあったら",["上書きする","書き出さない","番号を付けて保存する","古い方に番号を付けて残す"]]

var bnd=[10,10,210,30]
var bndArr=[bnd.slice(0),nextCol(24),
nextRow(200),nextCol(12),
nextRow(200),nextCol(96),
nextRow(132),nextRow(200)]

var win=new Window ("dialog", "PDF書き出し")
var pnl=win.add("Panel",[0,0,300,230])
ddlArr=new Array()
for(var i=0;i<lstArr.length;i++){
switch(typeof(lstArr[i])){
case "string":
bndArr[i][1]+=5
bndArr[i][3]+=5
pnl.add("StaticText",bndArr[i],lstArr[i])
break;
case "object":
var newDDL=pnl.add("DropDownList",bndArr[i])
for(var j=0;j<lstArr[i].length;j++){
newDDL.add("item",lstArr[i][j])
}
ddlArr.push(newDDL)
break;
}
}
nextRow(10)
nextRow(100)
var btn1=pnl.add("Button",nextCol(75),"キャンセル")
btn1.onClick=function(){win.close();NG=true;}
var btn2=pnl.add("Button",nextCol(75),"OK")
btn2.onClick=function(){win.close();NG=false}
function getPDFpresets(){
var rtn=new Array()
for(var i=0;i<app.pdfExportPresets.length;i++){
rtn.push(app.pdfExportPresets[i].name)
}
return rtn
}
function nextCol(width){
bnd[0]=bnd[2]+10
bnd[2]=bnd[0]+width
return bnd.slice(0)
}
function nextRow(width){
bnd[0]=10
bnd[1]+=30
bnd[2]=bnd[0]+width
bnd[3]+=30
return bnd.slice(0)
}

/*──────────
初期値を読み込む
──────────*/
var xmlFol=new Folder(new File(decodeURI($.fileName)).parent.fullName+"/設定")
if(!xmlFol.exists)xmlFol.create()
var xmlFile=new File(xmlFol.fullName+"/PDF書き出し設定.xml")
xmlFile.open("r")
var xml=new XML(xmlFile.read())
ddlArr[0].selection=ddlArr[0].find(xml.child("target").text())
ddlArr[1].selection=ddlArr[1].find(xml.child("location").text())
ddlArr[2].selection=ddlArr[2].find(xml.child("preset").text())
ddlArr[3].selection=ddlArr[3].find(xml.child("onExists").text())
xmlFile.close()
/*──────────
ダイアログを表示
結果をXMLに反映
──────────*/
win.show()
if(NG)return
var xml=new XML(<exportPreset></exportPreset>)
xml.appendChild(<target>{ddlArr[0].selection.text}</target>)
xml.appendChild(<location>{ddlArr[1].selection.text}</location>)
xml.appendChild(<preset>{ddlArr[2].selection.text}</preset>)
xml.appendChild(<onExists>{ddlArr[3].selection.text}</onExists>)
xmlFile.open("w")
xmlFile.write(xml.toXMLString())
xmlFile.close()
}

function main(){
var selFol
if(ddlArr[1].selection.index==2){
selFol=Folder.selectDialog ("PDFを書き出すフォルダを選択")
}
switch(ddlArr[0].selection.index){
var i
var j
case 0:
for(i=0;i<app.documents.length;i++){
exportPDF(app.documents[i])
}
break;
case 1:
for(i=0;i<app.books.length;i++){
var bookCon=app.books[i].bookContents
for(j=0;j<bookCon.length;j++){
app.open (bookCon.item(j).fullName)
exportPDF(app.documents[0])
app.documents[0].close(SaveOptions.no)
}
}
break;
case 2:
var files=Folder.selectDialog ("inddのフォルダを選択").getFiles("*.indd")
for(i=0;i<files.length;i++){
app.open(files[i])
exportPDF(app.documents[0])
app.documents[0].close(SaveOptions.no)
}
break;
}
function exportPDF(doc){
var file=getFileToExport ()
if(file)doc.exportFile(ExportFormat.PDF_TYPE,file,false,app.pdfExportPresets.itemByName(ddlArr[2].selection.text))
function getFolToExport(){
var rtn
switch(ddlArr[1].selection.index){
case 0:
rtn=new Folder(decodeURI(doc.fullName).replace("/"+decodeURI(doc.properties.name),""))
break;
case 1:
rtn=new Folder(decodeURI(doc.fullName).replace(decodeURI(doc.properties.name),"PDF"))
if(!rtn.exists)rtn.create()
break;
case 2:
rtn=selFol
break;
}
return rtn
}
function getFileToExport(){
var cnt=1
var docName=doc.properties.name
var rtn=new File(getFolToExport().fullName+"/"+doc.properties.name.replace(".indd",".pdf"))
switch(ddlArr[3].selection.index){
case 0:
break;
case 1:
if(rtn.exists)rtn=null
break;
case 2:
if(rtn.exists){
while(File(decodeURI(rtn.fullName).replace(".pdf","-"+cnt+".pdf")).exists)cnt++
rtn=File(decodeURI(rtn.fullName).replace(".pdf","-"+cnt+".pdf"))
}
break;
case 3:
if(rtn.exists){
while(File(decodeURI(rtn.fullName).replace(".pdf","-"+cnt+".pdf")).exists)cnt+
rtn.rename(docName.replace (".indd", "-"+cnt+".pdf"))
rtn=new File(getFolToExport().fullName+"/"+doc.properties.name.replace(".indd",".pdf"))
}
break;
}
return rtn
}
}
}