[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

(DTPtechNote:1704) [InDesign CS3][JavaScript]snap_caption.jsx



グラフィックフレームの適当な位置にテキストフレームを移動させます。
ありそうでなかった? 探せばあるんだろうけれど、探すのが手間だった(w


使い方はカンタンだけど、わからない人はこれを見てください。
http://www.youtube.com/v/a8ar4Ydq0ww
画像とキャプションの間隔とかは「設定」の所に直に入れています。適宜変えてください。

キャプションの位置の判定は一応以下のとおり
1)テキストフレームのY1座標がグラフィックフレームのY2座標より下なら、すべて下付きのキャプション
2)テキストフレームのX2座標が画像フレームのX2座標より右なら、右付きのキャプション
3)テキストフレームのX1座標が画像フレームのX1座標より左なら、左付きのキャプション
4)2または3の時、テキストフレームの中心がグラフィックフレームの中心より下にあるなら、それぞれの下にそろえます。

たぶん、CS2でも動くんじゃないかなあ。。。試してないけれど。


/*
snap_caption.jsx
(c)2007 www.seuzo.jp
キャプションを画像に寄せます。
2007.12.16        ver.0.1        とりあえず横組キャプションしか考えてません。
*/


////////////////////////////////////////////設定
const my_distance = 1; //画像とキャプションの間隔単位は環境設定に依存
const my_width_adjustment = true; //キャプションが画像の下にあるとき、テキストフレームの幅を画像にあわせるかどうか
const my_text_align_bottom = true; //キャプションが画像の右(左)にあり、かつ画像の下側に近いとき、テキストフレームのそろえを下にするかどうか

////////////////////////////////////////////エラー処理 
function myerror(mess) { 
  if (arguments.length > 0) { alert(mess); }
  exit();
}


////////////////////////////////////////////以下実行ルーチン
if (app.documents.length == 0) {myerror("ドキュメントが開かれていません")}
var my_document = app.activeDocument;
var my_selection = my_document.selection;
if (my_selection.length != 2) {myerror("2つのオブジェクトを選択してください")}

//オブジェクト種類の確定
if (my_selection[0].reflect.name == "Rectangle") {
	var my_image_obj = my_selection[0];
	if (my_selection[1].reflect.name == "TextFrame") {
		var my_text_obj = my_selection[1];
	} else {
		myerror("グラフィックフレームとテキストフレームを選択してください");
	}
} else if (my_selection[0].reflect.name == "TextFrame") {
	var my_text_obj = my_selection[0];
	if (my_selection[1].reflect.name == "Rectangle") {
		var my_image_obj = my_selection[1];
	} else {
		myerror("グラフィックフレームとテキストフレームを選択してください");
	}
} else {
	myerror("グラフィックフレームとテキストフレームを選択してください");
}

//縦組はサポートしない
if (my_text_obj.parentStory.storyPreferences.storyOrientation == 1986359924) {myerror("縦組はサポートしていません。ごめんなさい")}


//オブジェクトの大きさ(線幅を含む)
var my_image_bounds = my_image_obj.visibleBounds;
var my_text_bounds = my_text_obj.visibleBounds;
var i_y1 = my_image_bounds[0];
var i_x1 = my_image_bounds[1];
var i_y2 = my_image_bounds[2];
var i_x2 = my_image_bounds[3];
var i_w = i_x2 - i_x1; //グラフィックフレームの幅
var i_h = i_y2 - i_y1; //グラフィックフレームの高さ
var t_y1 = my_text_bounds[0];
var t_x1 = my_text_bounds[1];
var t_y2 = my_text_bounds[2];
var t_x2 = my_text_bounds[3];
var t_w = t_x2 - t_x1; //テキストフレームの幅
var t_h = t_y2 - t_y1; //テキストフレームの高さ


//キャプションの場所の移動
if (t_y1 >= i_y2) { //キャプションが画像の下にある
	if (my_width_adjustment) { //キャプションの幅を画像にあわせる
		my_text_obj.visibleBounds = [i_y2 + my_distance, i_x1, i_y2 + my_distance + t_h, i_x2];
	} else {
		my_text_obj.visibleBounds = [i_y2 + my_distance, i_x1, i_y2 + my_distance + t_h, i_x1 + t_w];
	}
} else if (t_x2 >= i_x2) { //キャプションが画像の右側にある(テキストフレームのx2がグラフィックフレームのx2よりも右にある)
	if (i_y1 + i_h / 2 >= t_y1 + t_h / 2) { //テキストフレームの中心がグラフィックフレームの中心より上にある
		my_text_obj.visibleBounds = [i_y1, i_x2 + my_distance, i_y1 + t_h, i_x2 + my_distance + t_w];
	} else { //下にある
		my_text_obj.visibleBounds = [i_y2 - t_h, i_x2 + my_distance, i_y2, i_x2 + my_distance + t_w];
		if (my_text_align_bottom) { //テキストフレームを下揃え設定
			my_text_obj.textFramePreferences.verticalJustification = VerticalJustification.bottomAlign;//BOTTOM_ALIGN;
		}
	}
} else if (i_x1 >= t_x1) { //キャプションが画像の左側にある(テキストフレームのx1がグラフィックフレームのx1よりも左にある)
	if (i_y1 + i_h / 2 >= t_y1 + t_h / 2) { //テキストフレームの中心がグラフィックフレームの中心より上にある
		my_text_obj.visibleBounds = [i_y1, i_x1 - my_distance - t_w, i_y1 + t_h, i_x1 - my_distance];
	} else { //下にある
		my_text_obj.visibleBounds = [i_y2 - t_h, i_x1 - my_distance - t_w, i_y2, i_x1 - my_distance];
		if (my_text_align_bottom) { //テキストフレームを下揃え設定
			my_text_obj.textFramePreferences.verticalJustification = VerticalJustification.bottomAlign;//BOTTOM_ALIGN;
		}
	}
} else {
	myerror("テキストフレームの位置を特定できませんでした");
}