Skip to content

Instantly share code, notes, and snippets.

@kilica
Last active December 4, 2016 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kilica/6da92320f682b360cbd4ce1aca5b5f68 to your computer and use it in GitHub Desktop.
Save kilica/6da92320f682b360cbd4ce1aca5b5f68 to your computer and use it in GitHub Desktop.
Javascript for To InDesign Text Frame from JSON
function getPowers()
{
// 元データとなる json ファイルを指定
filepath = "/Users/kilica/6-1-Powers.json";
fileObj = new File(filepath);
flag = fileObj.open("r");
if (flag == true) {
powerText = fileObj.read();
// JSON のテキストを eval() でオブジェクト(配列)に変換
powers = eval("("+powerText+")");
fileObj.close();
}else{
alert("ファイルが開けませんでした");
}
return powers;
}
function setData()
{
// 呪文一覧の配列を .json ファイルから取得
powers = getPowers();
// 開いている .indd ファイルのページオブジェクトを取得
docObj = app.activeDocument.pages;
// ドキュメント内のページに対する処理
for (j=0; j<docObj.length; j++) {
// ページ内のグループに対する処理
for (m=0; m<docObj[j].groups.length; m++) {
frameGroup = docObj[j].groups[m];
// グループ内のテキストフレームに対する処理
for (i=0; i< frameGroup.textFrames.length; i++) {
targetFrame = frameGroup.textFrames[i].contents;
// テキストフレームの中のテキストが "Pow_"で始まっていた場合
if (targetFrame.indexOf("Pow_") > -1) {
// テキストフレームの中の _ で区切られているテキストを分割する
powerCode = targetFrame.split('_');
// JSON ファイルから取得した配列の、該当するデータを取得する
powerText = powers[powerCode[1]][powerCode[3]][powerCode[2]]
// 取得したデータで、テキストフレームの中身を置き換える
frameGroup.textFrames[i].contents = powerText;
}
}
}
}
}
setData();
/*
想定しているJSONファイルの構造
{
"Sylph" : [
{
"l" : "L",
"t" : "稲妻",
"r" : "遠隔(10エリア内)",
"d" : "瞬間",
"a" : "3本 / 6本 / 9本",
"c" : "@効果 : 1d6 + 【2】 (雷のダメージ)\n稲妻を放ち対象に電撃のダメージを与えます。稲妻は、異なる対象に向けることも、同じ対象に向けることもできます。",
"act" : "2",
},
{
"l" : "1",
"t" : "風の守り",
"r" : "使役者",
"d" : "2時間",
"a" : "使役者",
"c" : "@効果 : 10\n使役者を取り巻く風により、飛び道具から身を護ります。遠隔攻撃に対する装甲10を持ちます。\n同時に、ガスや煙、霧、虫を吹き飛ばすことができます。",
"act" : "2",
}
]
}
*/
// 使い方 : http://trpg-labo.com/blog/page/1056
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment