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

(DTPtechNote:1053) [AS-InDesign CS] distance of item



(*
distance of item 0.1
(c) 2005市川せうぞー http://www.seuzo.jp/
ふたつのページアイテムの距離を表示します。
History;
ver.0.1	2005.02.28	とりあえず版
*)

my ver()
my doc_exists()
my item_check(2)
set conv_unit to true --ptとmmで単位換算するかどうか
my do_it(conv_unit)



--------------------------------------------------------●InDesignバージョン確認ルーチン
to ver()
	tell application "InDesign CS_J"
		set my_version to version
		if my_version < 3 and my_version > 3.9 then
			my my_error("このプログラムはInDesign CS以外では動作しません", true)
		end if
	end tell
end ver

--------------------------------------------------------●ドキュメントが開かれているかどうか
to doc_exists()
	tell application "InDesign CS_J"
		if not (exists document 1) then
			my my_error("ドキュメントが開かれていません", true)
		end if
	end tell
end doc_exists

--------------------------------------------------------●n個のページオブジェクトが選択されているかどうか
to item_check(n)
	tell application "InDesign CS_J"
		set mySelection to selection
		--選択ページアイテムが2つかどうか
		if (count mySelection) is not equal to n then
			my my_error("必ず" & n & "つのページアイテムを選択してください", true)
		end if
		--選択アイテムのクラスがテキスト以外かどうか
		set myTextObjectList to {insertion point, character, word, line, paragraph, text column, text, story}
		set myClass to class of item 1 of mySelection
		if myClass is in myTextObjectList then
			my my_error("テキスト以外を選択してやりなおしてください", true)
		end if
	end tell
end item_check


--------------------------------------------------------●計算と結果表示
to do_it(conv_unit)
	tell application "InDesign CS_J"
		activate
		set ANS to button returned of (display dialog ("線幅を考慮しますか?" as Unicode text) buttons {"キャンセル", "線幅を考慮する", "線幅を無視"} default button 3)
		set ANS to ANS as Unicode text
		set mySelection to selection
		
		--単位をゲットする
		set v_unit to vertical measurement units of view preferences of document 1
		set h_unit to horizontal measurement units of view preferences of document 1
		
		--原点の移動
		set zero_pooint to zero point of document 1 --現在の原点を記憶
		set zero point of document 1 to {0, 0} --原点の移動
		
		--大きさをゲットする{y1, x1, y2,x2}
		if ANS is "線幅を考慮する" then
			set bounds_1 to visible bounds of item 1 of mySelection
			set bounds_2 to visible bounds of item 2 of mySelection
		else
			set bounds_1 to geometric bounds of item 1 of mySelection
			set bounds_2 to geometric bounds of item 2 of mySelection
		end if
		
		--原点の復帰
		set zero point of document 1 to zero_pooint
		
		
		--位置の比較(y方向)
		if item 1 of bounds_1 = item 1 of bounds_2 then --y1が同じ位置
			if item 3 of bounds_1 = item 3 of bounds_2 then --さらに高さが同じだった
				set y to (item 1 of bounds_1) - (item 3 of bounds_1) --単純に高さを開き具合とする
			else if item 3 of bounds_1 > item 3 of bounds_2 then --bounds_1のほうが大きいオブジェクトだった
				set y to (item 1 of bounds_1) - (item 3 of bounds_1) --bounds_1の高さを開き具合とする		
			else ---bounds_2のほうが大きいオブジェクトだった
				set y to (item 1 of bounds_2) - (item 3 of bounds_2) --bounds_2の高さを開き具合とする		
			end if
		else if item 1 of bounds_1 > item 1 of bounds_2 then --bounds_1のほうが下にある
			set y to (item 1 of bounds_1) - (item 3 of bounds_2)
		else --bounds_2のほうが下にある
			set y to (item 1 of bounds_2) - (item 3 of bounds_1)
		end if
		
		--位置の比較(x方向)
		if item 2 of bounds_1 = item 2 of bounds_2 then --x1が同じ位置
			if item 4 of bounds_1 = item 4 of bounds_2 then --さらに幅が同じだった
				set x to (item 2 of bounds_1) - (item 4 of bounds_1) --単純に幅を開き具合とする
			else if item 4 of bounds_1 > item 4 of bounds_2 then --bounds_1のほうが大きいオブジェクトだった
				set x to (item 2 of bounds_1) - (item 4 of bounds_1) --bounds_1の幅を開き具合とする		
			else ---bounds_2のほうが大きいオブジェクトだった
				set x to (item 2 of bounds_2) - (item 4 of bounds_2) --bounds_2の幅を開き具合とする		
			end if
		else if item 2 of bounds_1 > item 2 of bounds_2 then --bounds_1のほうが右にある
			set x to (item 2 of bounds_1) - (item 4 of bounds_2)
		else --bounds_2のほうが下にある
			set x to (item 2 of bounds_2) - (item 4 of bounds_1)
		end if
		
		--単位換算と返事の生成
		set ans_x to "ヨコ間隔:" as Unicode text
		set ans_y to "タテ間隔:" as Unicode text
		if conv_unit then
			if (h_unit as Unicode text) = (points as Unicode text) then
				set ans_x to ans_x & x & " points ≒ " & my point2mm(x) & " millimeters"
			else if (h_unit as Unicode text) = (millimeters as Unicode text) then
				set ans_x to ans_x & my mm2point(x) & " points ≒ " & x & " millimeters"
			else
				set ans_x to ans_x & x & (h_unit as Unicode text)
			end if
			if (v_unit as Unicode text) = (points as Unicode text) then
				set ans_y to ans_y & y & " points ≒ " & my point2mm(y) & " millimeters"
			else if (v_unit as Unicode text) = (millimeters as Unicode text) then
				set ans_y to ans_y & my mm2point(y) & " points ≒ " & y & " millimeters"
			else
				set ans_y to ans_y & y & (v_unit as Unicode text)
			end if
		else --単位換算をしない
			set ans_x to ans_x & x & (h_unit as Unicode text)
			set ans_y to ans_y & y & (v_unit as Unicode text)
		end if
		
		--結果の表示
		display dialog ans_x & return & ans_y
		
	end tell
end do_it


--------------------------------------------------------●nポイントをmミリに変換
to point2mm(n)
	return n * 0.352778
end point2mm

--------------------------------------------------------●mミリをnポイントに変換
to mm2point(m)
	return m * 2.834644
end mm2point



----------------------------------●エラー処理
on my_error(err_str, my_stop)
	set err_str to err_str as Unicode text
	if my_stop then
		set my_stop to {"中止"}
	else
		set my_stop to {"中止", "続行"}
	end if
	tell application "InDesign CS_J"
		activate
		beep
		set ANS to button returned of (display dialog err_str buttons my_stop default button 1 with icon stop) --警告ダイアログ出してストップ
		if ANS is "中止" then
			error number -128
		end if
	end tell
end my_error