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

(DTPtechNote:1106) [AS_InDesign CS] section_add_paraStyle

(c)2005 www.seuzo.jp
ver.0.1	とりあえず版。まだバグがあるかも。

my ver()
my doc_exists()

tell application "InDesign CS_J"
	set user interaction level to never interact --ユーザー操作を禁止します
	set my_para_styeles to my get_para_style() ---段落スタイルのgetしておく
	set my_para_styeles to (choose from list my_para_styeles with prompt "セクションのトリガとなる段落スタイルを選択してください" OK button name "選択" without multiple selections allowed and empty selection allowed) as Unicode text
	set text_obj to my style_search(my_para_styeles)
	set {page_obj, marker_list} to my conv_obj(text_obj)
	set my_cnt to my make_section(page_obj, marker_list)
	set user interaction level to interact with all --ユーザー操作の禁止を解除します
	display dialog "セクションを" & my_cnt & "箇所追加しました。" with icon 1 giving up after 10
end tell

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

to get_para_style()
	tell application "InDesign CS_J"
		set my_para_styeles to {}
		tell document 1
			repeat with i from 1 to count (paragraph styles)
				tell paragraph style i
					if not (name = "[No paragraph style]") then
						set end of my_para_styeles to name as Unicode text
					end if
				end tell
			end repeat
		end tell
		if my_para_styeles = {} then
			my my_error("段落スタイルがひとつも設定されていません", true)
			return my_para_styeles
		end if
	end tell
end get_para_style

to style_search(my_para_styeles)
	tell application "InDesign CS_J"
		set find preferences to nothing -- 既存の検索環境設定を初期化します。
		set change preferences to nothing -- 変更環境設定を初期化します。
			set hit_object to search document 1 with find attributes {applied paragraph style:my_para_styeles}
		end try
		if hit_object = {} then my my_error("段落スタイル「" & my_para_styeles & "」が適用されている段落はありませんでした", true)
	end tell
	return hit_object
end style_search

------------------------------------------★text オブジェクトを受け取って、ページオブジェクトと文字列を返す
to conv_obj(text_obj)
	set page_obj to {}
	set marker_list to {}
	tell application "InDesign CS_J"
		repeat with i in text_obj
				set end of page_obj to parent of parent text frame of (contents of i)
				set end of marker_list to my chomp(contents of i)
			on error
				my my_error("段落がテキストフレームにまたがっています。このバージョンではサポートしません" & return & (contents of i), false)
			end try
		end repeat
	end tell
	return {page_obj, marker_list}
end conv_obj

to make_section(page_obj, marker_list)
	tell application "InDesign CS_J"
		set my_cnt to 1
		repeat with i in page_obj
				make section at document 1 with properties {continue numbering:true, marker:item my_cnt of marker_list, page start:contents of i}
			on error
				my my_error("セクションの作成中にエラーが置きました。", false)
			end try
			set my_cnt to my_cnt + 1
		end repeat
		return (my_cnt - 1)
	end tell
end make_section

to chomp(str)
	set str to str as Unicode text
	if str = return then
		return ""
	else if str ends with return then
		return text 1 thru -2 of str
		return str
	end if
end chomp

to as_replace(theText, orgStr, newStr)
	set oldDelim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to orgStr
	set tmpList to every text item of theText
	set AppleScript's text item delimiters to newStr
	set tmpStr to tmpList as text
	set AppleScript's text item delimiters to oldDelim
	return tmpStr
end as_replace

on my_error(err_str, my_stop)
	set err_str to err_str as Unicode text
	if my_stop then
		set my_stop to {"中止"}
		set my_stop to {"中止", "続行"}
	end if
	tell application "InDesign CS_J"
		set user interaction level to interact with all --ユーザー操作の禁止を解除します
		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