FilemakerとInDesignでプチ自動組版(名刺作成)

プチ自動組版なので
__∧ノ_
(゚д゚)  < 期待しすぎると痛い目にあいます。
゚( )−
/ >
てな感じで始めます。よろしくお願いします。_(._.)_
まず、出来なかった事から先に書いておきます。
出来れば設定いらずで運用できるようにしたかったのですが、外部アプリからVBSを実行させるとカレントディレクトリが取得できなかったので、VBSファイルに設定が必要になりました。(カレントディレクトリとビューワ設定)


ということで、はじまり はじまり〜

1.レシピと構造

まず、必要なのは
Windows環境
Filemaker 9とInDesign CS3インストール済
Filemakerファイル
・VBSファイル
・JSXファイル
・イメージファイルを置いておくフォルダ
・PDFファイルが保存されるフォルダ
XMLファイルが保存されるフォルダ
・これらファイル、フォルダを同じ階層に置くためのルートフォルダ
以上

こんな感じです。


サンプルで作ったファイルをここにおいておきます。
http://cid-5bba1198e90d990a.skydrive.live.com/self.aspx/public/namecard.zip
動かしてみる場合はVBSファイルに設定を書き込む必要があります。

2.各ファイルの役割

Filemakerファイルの役割>
Filemakerの役割はデータベース(当たり前)とXMLファイルの書き出し、VBSを実行するEventの送信まで。

<VBSファイルの役割>
Filemakerから直接JSXのファイルの実行が出来ない為、VBSからDoScriptでJSXファイルを実行させています。こういうのにラッパーというのでしょうか?
最後に組版結果(PDF)を開くを実行しています。

<JSXファイルの役割>
正直、JSXファイルは無くても作れるのですが、クロスプラットフォーム環境(サーバ運用)を視野にいれるなら共通言語のJSXはちょっと外せないので、組版部分はJSXで作りました。役割はXML読み込みからPDF作成までのほぼ全域。

3.操作

ユーザー側の操作としてはFilemakerで任意のレコードを選択し、作成ボタンをおすと組版結果を指定したViewerで表示する。というものです。

4.データベース

データベースはごくごく単純に必要なフィールドを作成とスクリプトを1つ作成するのみです。




データを入力し、ボタンを押すとXML書き出しと、VBSが実行されます。
詳しくはファイル参照


書き出されるXMLファイルは以下の様になります。(FMDSORESULT)


5.VBSファイル


'カレントディレクトリ設定
'CurrentDIR = "C:\Documents and Settings\Owner\デスクトップ\namecard"
CurrentDIR = "C:\namecard"

'ビューワ設定
Viewer = "chrome.exe" 'クローム(おすすめ)
'Viewer = "AcroRd32.exe" 'アクロバットリーダー
'Viewer = "iexplore.exe" 'インターネットエクスプローラ
'Viewer = "firefox.exe" 'ファイアーフォックス

Set ws = CreateObject("WScript.Shell")
Set app = CreateObject("InDesign.Application")

app.DoScript CurrentDIR & "\namecard.jsx",1246973031,Array(Wsh.arguments(0),CurrentDIR)
ws.run Viewer & " """ & CurrentDIR & "\pdf\" & Wsh.arguments(0) & ".pdf"""

Set ws = Nothing
Set app = Nothing

■カレントディレクトリ設定
エクスプローラでルートディレクトリのアドレスをコピペすれば簡単に設定できます。本来ならこの設定は省きたかったんだけども外部のアプリから実行するとなんかの拍子でマイドキュメントを指したりするので止めました。
■ビューワ設定
ロームにおすすめと書いたのは理由がありまして、クローム以外は排他的にPDFファイルを開いていてFilemakerから再度同じファイル名でPDF作成すると「誰かがファイルを開いてて保存出来ねーぞー」みたいなエラーが返ってきます。


後は見ての通り、DoScriptでjsxファイルを実行して、シェルでPDFを開くのみの簡単スクリプトです。
マックの方はここをAppleScriptで書くと動きそうだなーと思います。

6.JSXファイル


var FileNo = arguments[0];
var CurrentDIR = arguments[1].replace("\\","/");

for(var i=0;app.documents.length>i;i++){
 app.documents.item(0).close(SaveOptions.no);
}

myDoc = app.documents.add();
myDoc.documentPreferences.pageWidth = "91mm";
myDoc.documentPreferences.pageHeight = "55mm";

myDoc.importXML(File(CurrentDIR + "/xml/" + FileNo + ".xml"));
var myXML = myDoc.xmlElements.item(0).xmlElements.item("ROW").xmlElements;
var rs = new Object;
for(var i=0;myXML.length>i;i++){
if(myXML.item(i).contents.replace(RegExp("[  ]","g"), "")!=""){
rs[myXML.item(i).markupTag.name] = myXML.item(i).contents;
}else{
rs[myXML.item(i).markupTag.name] = "";
}
}

frame = myDoc.textFrames.add();
frame.geometricBounds = ["2mm","2mm","55mm","91mm"];
frame.label = "logo";
try{
frame.place(CurrentDIR + "/img/" + rs["logo"]);
}catch(e){}

frame = myDoc.textFrames.add();
frame.geometricBounds = ["18mm","0mm","21.25mm","91mm"];
frame.label = "position";
frame.parentStory.pointSize = "13q";
frame.parentStory.leading = "20ha";
frame.parentStory.justification = Justification.centerAlign;
frame.parentStory.appliedFont = app.fonts.item("小塚ゴシック Pro L");
frame.parentStory.contents = rs["position"];

frame = myDoc.textFrames.add();
frame.geometricBounds = ["23mm","0mm","28mm","91mm"];
frame.label = "name";
frame.parentStory.pointSize = "20q";
frame.parentStory.leading = "30ha";
frame.parentStory.justification = Justification.centerAlign;
frame.parentStory.appliedFont = app.fonts.item("小塚明朝 Pro B");
frame.parentStory.contents = rs["name"];

frame = myDoc.textFrames.add();
frame.geometricBounds = ["35mm","10mm","40mm","81mm"];
frame.label = "company";
frame.parentStory.pointSize = "16q";
frame.parentStory.leading = "20ha";
frame.parentStory.justification = Justification.leftAlign;
frame.parentStory.appliedFont = app.fonts.item("小塚明朝 Pro B");
frame.parentStory.contents = rs["company"];

frame = myDoc.textFrames.add();
frame.geometricBounds = ["40mm","10mm","55mm","81mm"];
frame.label = "others";
frame.parentStory.pointSize = "11q";
frame.parentStory.leading = "14ha";
frame.parentStory.justification = Justification.leftAlign;
frame.parentStory.appliedFont = app.fonts.item("小塚ゴシック Pro L");
var Others = "〒" + rs["zip"] + " " + rs["address"] + "\r";
Others += "TEL/" + rs["phone"] + " " + "FAX/" + rs["fax"] + "\r";
Others += rs["mail"];
frame.parentStory.contents = Others;

var myPDFExportPreset = app.pdfExportPresets.item("[PDFX1a 2001 JPN]");
myDoc.exportFile(ExportFormat.pdfType, Folder(CurrentDIR + "/pdf/" + FileNo + ".pdf"),false,myPDFExportPreset);

myDoc.close(SaveOptions.no);

今回のこのサンプルはスクリプトでドキュメント作成から行っていますが、雛形を用意した方が明らかに作業効率、メンテナンス性はいいです。コードの量も減るし、会社ごとにレイアウト変更することも簡単です。



続きはまた今度・・・