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

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



すこし変えてみました。思いのほか便利ねえ、とおもう(自画自賛^^


/*
snap_caption.jsx
(c)2007 www.seuzo.jp
キャプションを画像の横または下にスナップさせます。
簡単な使い方説明:http://jp.youtube.com/watch?v=a8ar4Ydq0ww

2007-12-16	ver.0.1	とりあえず横組キャプションしか考えてません。
2007-12-17	ver.0.2	「設定」にmy_text_align_topを追加
2007-12-18	ver.0.3	画像がグループ化されていても動作するようにした。グラフィックフレームが楕円または多角形でも動作するようにした。「設定」にmy_text_fitを追加。テキストフレームの上辺がグラフィックフレームの底辺のわずかに(my_distance値の2倍分)上にあっても、下付きのキャプションと判定することにした。
*/


////////////////////////////////////////////設定
const my_distance = 1; //画像とキャプションの間隔単位は環境設定に依存
const my_width_adjustment = true; //キャプションが画像の下にあるとき、テキストフレームの幅を画像にあわせるかどうか
const my_text_align_bottom = true; //キャプションが画像の右(左)にあり、かつ画像の下側に近いとき、テキストフレームのそろえを下にするかどうか
const my_text_align_top = true; //キャプションを画像の下、または画像の上辺にフィットさせるとき、テキストフレームのそろえを強制的に上揃えにするかどうか
const my_text_fit = 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 ("Rectangle, Group, Oval, Polygon".match(my_selection[0].reflect.name)) {
	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 ("Rectangle, Group, Oval, Polygon".match(my_selection[1].reflect.name)) {
		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 - my_distance * 2) { //キャプションが画像の下にある
	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];
	}
	if (my_text_align_top) { //テキストフレームを上揃え設定
		my_text_obj.textFramePreferences.verticalJustification = VerticalJustification.topAlign;//TOP_ALIGN;
	}
} 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];
		if (my_text_align_top) { //テキストフレームを上揃え設定
			my_text_obj.textFramePreferences.verticalJustification = VerticalJustification.topAlign;//TOP_ALIGN;
		}
	} 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];
		if (my_text_align_top) { //テキストフレームを上揃え設定
			my_text_obj.textFramePreferences.verticalJustification = VerticalJustification.topAlign;//TOP_ALIGN;
		}
	} 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("テキストフレームの位置を特定できませんでした");
}

//テキストフレームに対して「フレームを内容にあわせる」を実行する
if (my_text_fit) {
	my_text_obj.fit (FitOptions.frameToContent);
}