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

(DTPtechNote:852) Re: xmlの取り込み->PDF



InDesign CS用に書き換えてみた。



on open of theFiles
	tell application "Finder"
		set tmplate_file to (choose file with prompt "流し込むテンプレートドキュメントを選択してください" of type {"IDd3"})
		repeat with i in theFiles
			set my_folder to (folder of i) as Unicode text
			set my_name to (name of i) as Unicode text
			if my_name ends with ".xml" then
				set save_path to my_folder & my replace(my_name, ".xml", ".indd")
				set save_PDF_path to my_folder & my replace(my_name, ".xml", ".pdf")
			else
				set save_path to my_folder & my_name & ".indd"
				set save_PDF_path to my_folder & my_name & ".pdf"
			end if
			set tmplate_file to file {my template_select(my_name, (folder of tmplate_file) as Unicode text, (name of tmplate_file) as Unicode text), "_" as Unicode text} --必要なければコメントする
			if class of tmplate_file is list then --文字生成したテンプレートファイルが存在しない
				my my_error(item 1 of tmplate_file, item 2 of tmplate_file)
			else
				try
					my goXML(i, tmplate_file, save_path, save_PDF_path)
				on error errMsg number errNum
					activate
					beep
					display dialog "誤りが起きました;" & my_name & return & "処理を中断しますか?" giving up after 30
					tell application "InDesign CS_J"
						close document 1 saving no
					end tell
				end try
			end if
		end repeat
		say "Work was finished. " & (length of theFiles) & " files were processed."
	end tell
end open

--正しいテンプレートを選んでいるかどうかチェックして、tmplate_fileを書き換える
(*
カレントXML(xml_name)を特定の文字(xml_name)で分割し、2番目(たぶん真ん中)に来る文字列を
テンプレートと比較し、特定し、置き換え、新しいテンプレート名として生成する。
Ex.
xml_name	xxxx_001_xxx.xml
tmplate_file_name	yyy_002_yyy.indd
tmp_delimiter	_
return	yyy_001_indd
*)
to template_select(xml_name, tmplate_file_folder, tmplate_file_name, tmp_delimiter)
	tell application "Finder"
		if xml_name does not contain tmp_delimiter and tmplate_file_name does not contain tmp_delimiter then --tmp_delimiterが含まれていない
			set error_mess to "xml_name : " & xml_name & return & "tmplate_file_name : " & tmplate_file_name & return & "ファイル名にデリミタ(" & tmp_delimiter & ")が含まれていません"
			return {error_mess, false} --不正ファイルはfalseを返す
		else
			set xml_name_list to my as_split(tmp_delimiter, xml_name)
			set tmplate_file_name_list to my as_split(tmp_delimiter, tmplate_file_name)
			set tmplate_file_name to (item 1 of xml_name_list & tmp_delimiter & item 2 of tmplate_file_name_list & tmp_delimiter & item 3 of xml_name_list) as Unicode text --あたらしいtmplate_file_nameを生成
		end if
		if exists file (tmplate_file_folder & tmplate_file_name) then --そのファイルが存在すれば
			return (tmplate_file_folder & tmplate_file_name)
		else
			set error_mess to tmplate_file_folder & tmplate_file_name & return & "は存在しないファイルです"
			return {error_mess, false} --不正ファイルはfalseを返す
		end if
	end tell
end template_select



to goXML(xml_file, tmplate_file, save_path, save_PDF_path)
	tell application "InDesign CS_J"
		open tmplate_file
		tell document 1
			tell XML import preferences
				set import to selected to false --選択した構造要素にとりこまない
				set import style to merge import --内容を置換
			end tell
			import XML from xml_file --XMLのとりこみ
		end tell
		my overflow_resolve() --オーバーフローの解決
		my fit_image() --画像をフレームの最大サイズにし、中央にそろえる
		save document 1 to save_path
		export document 1 format PDF type to save_PDF_path using PDF export preset "guidebook"
		close document 1 saving yes
	end tell
end goXML


--テキストオーバーフローしているテキストに長体をかけて解消しまつ
--あたりまえだけど、テキストフレームに1行しか入っていない場合だけ有効
to overflow_resolve()
	tell application "InDesign CS_J"
		set obj to a reference to document 1
		set overflows_list to {}
		repeat
			try
				set overflows_list to overflows_list & (text frames of obj whose overflows = true)
			end try
			if not (exists groups of obj) then exit repeat
			set obj to a reference to groups of obj
		end repeat
		
		repeat with i in overflows_list
			tell i
				repeat while overflows
					tell parent story
						set horizontal scale to horizontal scale - 2
					end tell
				end repeat
			end tell
		end repeat
	end tell
end overflow_resolve

--画像をフレームの最大サイズにし、中央にそろえる
to fit_image()
	tell application "InDesign CS_J"
		tell document 1
			repeat with ii in all graphics
				fit parent of ii given proportionally --内容をフレーム内に納める
				fit parent of ii given center content --内容を中央に揃える
			end repeat
		end tell
	end tell
end fit_image


----------------------------------●置換
to 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 replace

----------------------------------●リスト分割
to as_split(thedelimit, theText)
	set oldDelim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to thedelimit
	set tmpList to every text item of theText
	set AppleScript's text item delimiters to oldDelim
	return tmpList
end as_split

----------------------------------●エラー処理
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"
		beep
		set ANS to button returned of (display dialog err_str buttons my_stop with icon stop) --警告ダイアログ出してストップ
		if ANS is "中止" then error number -128
	end tell
end my_error