Skip to content

Instantly share code, notes, and snippets.

@kanemu
Created May 25, 2010 01:13
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 kanemu/412639 to your computer and use it in GitHub Desktop.
Save kanemu/412639 to your computer and use it in GitHub Desktop.
[indesign][extendscript]ガイドに合わせてテーブルを作成。
#target "InDesign"
(function(){
//なにも開いてなければ抜ける
if(!app.activeDocument) return;
var page=app.activeWindow.activePage;
//ガイドが4本以下なら抜ける
if(page.guides.length<4) return;
//ガイドの位置を収集する。
var wLocate=[];
var hLocate=[];
for(var i=page.guides.length-1;i>-1;i--){
var guide=page.guides[i];
if(guide.orientation==1986359924){
wLocate.push(Number(guide.location))
}else{
hLocate.push(Number(guide.location))
}
//ロックされてないガイドは削除
if(!guide.locked) guide.remove();
}
//縦ガイド、横ガイドが2本以下なら抜ける
if(wLocate.length<2||hLocate.length<2) return;
//配列をソート
wLocate.sort(function(a,b){return a>b});
hLocate.sort(function(a,b){return a>b});
//画像があったら回り込みを解除
var gra=page.allGraphics;
for(var i=0;i<gra.length;i++){
gra[i].parent.textWrapPreferences.textWrapType=1852796517
}
//テキストフレーム作成
var frame= page.textFrames.add();
frame.geometricBounds=[
hLocate[0],
wLocate[0],
hLocate[hLocate.length-1],
wLocate[wLocate.length-1]
];
//表を挿入
var story=frame.parentStory;
var table=story.tables.add(LocationOptions.AT_END);
//行を増やす
while(table.rows.length<hLocate.length+1){
table.rows.add(LocationOptions.AT_END)
}do{
table.rows[0].remove()
}while(table.rows.length>=hLocate.length);
//行のサイズを調節
for(var i=0;i<hLocate.length-1;i++){
var h=hLocate[i+1]-hLocate[i];
table.rows[i].autoGrow=false;
table.rows[i].height=h
};
//列を増やす
while(table.columns.length<wLocate.length+1){
table.columns.add(LocationOptions.AT_END)
}do{
table.columns[0].remove()
}while(table.columns.length>=wLocate.length);
//列のサイズを調節
for(var i=0;i<wLocate.length-1;i++){
var w=wLocate[i+1]-wLocate[i];
table.columns[i].width=w;
};
//フレームを内容に合わせる
frame.fit(FitOptions.FRAME_TO_CONTENT);
//フレームをちょっと移動
frame.move(
"to",
[(table.columns[0].leftEdgeStrokeWeight*-0.5),
(table.rows[0].topEdgeStrokeWeight*-0.5)]
);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment