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

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



せっかく自動っぽいから、入らない時の長体処理と画像のフィットもやって、少し楽ちんになってみました。
#でもお客様には徹夜してることになってまつ ;-p

on open of theFiles
	tell application "Finder"
		set tmplate_file to (choose file with prompt "流し込むテンプレートドキュメントを選択してください" of type {"InDd"})
		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
			my goXML(i, tmplate_file, save_path, save_PDF_path)
		end repeat
		beep 3
	end tell
end open

to goXML(xml_file, tmplate_file, save_path, save_PDF_path)
	tell application "InDesign 2.0.2J"
		open tmplate_file
		tell document 1
			import XML from xml_file
		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 style "guidebook"
		close document 1
	end tell
end goXML


--テキストオーバーフローしているテキストに長体をかけて解消しまつ
--あたりまえだけど、テキストフレームに1行しか入っていない場合だけ有効
to overflow_resolve()
	tell application "InDesign 2.0.2J"
		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 - 1
					end tell
				end repeat
			end tell
		end repeat
	end tell
end overflow_resolve

--画像をフレームの最大サイズにし、中央にそろえる
to fit_image()
	tell application "InDesign 2.0.2J"
		set image_list to {}
		tell document 1
			repeat with i from 1 to (count every page)
				tell page i
					try
						set image_list to image_list & (every image of all page items)
						set image_list to image_list & (every EPS of all page items)
						set image_list to image_list & (every PDF of all page items)
					end try
				end tell
			end repeat
			
			repeat with ii in image_list
				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