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

(DTPtechNote:1185) Re: [AS InDesignCS] Text Esport



ちょっと書き直してみた。


(*
Text_Export
(c)2003-2005 www.seuzo.jp
ドキュメント上のテキストをすべて書き出します。

●history
2005.07.14	ver.0.1	とりあえず
2005.07.17	ver.0.2	テーブルの中も書き出すようにしてみた
2005.07.18	ver.0.3	ドラッグ&ドロップインタフェイスを追加。ページごと(またはストーリーごと)に書き出すかどうかを追加。
*)

------------------------------------------★Droplet処理
on open (theFiles)
	set my_doc to {}
	tell application "Finder"
		repeat with i in theFiles
			if file type of contents of i = "IDd3" then set end of my_doc to contents of i
		end repeat
		if my_doc = {} then my my_error("処理ファイルがみつかりませんでした", true)
		my control_sub(my_doc) --あとはコントロール部におまかせ
	end tell
end open

------------------------------------------★Applet処理
on run
	tell application "Finder"
		activate
		set my_doc to (choose file with prompt "テキストを書き出したいドキュメントを選んでください" of type {"IDd3"}) as list
		my control_sub(my_doc) --あとはコントロール部におまかせ
	end tell
end run


--------------------------------------------------------●コントロールルーチン
to control_sub(my_doc)
	set contents_txt to "" as Unicode text
	set open_files to {}
	--★起動チェック
	my ver()
	
	--★実行
	tell application "InDesign CS_J"
		activate
		--書き出し方法と保存場所
		set export_way to button returned of (display dialog ("Page単位で書き出しますか? それともStoryごと?" as Unicode text) buttons {"キャンセル", "Page", "Story"} default button 3)
		set my_file to (choose file name with prompt "出力ファイルを保存します" default name "contents.txt")
		--現在開いているドキュメントのフルパス
		if exists document 1 then set open_files to (full name of every document) as Unicode text
		--ドキュメントごとの処理
		repeat with i in my_doc
			if open_files contains (i as Unicode text) then my my_error("処理ドキュメントはすでに開かれています。いったんドキュメントを閉じてから再実行してください", true)
			open i
			if export_way is "Story" then
				set contents_txt to contents_txt & (i as Unicode text) & return & return & my story_export()
			else
				set contents_txt to contents_txt & (i as Unicode text) & return & return & my page_export()
			end if
			close document 1 saving no --保存せずにドキュメントを閉じる
		end repeat
		
		--テキスト書き込み
		my write_file(contents_txt, my_file)
		
		--★終了ダイアログ
		beep
		display dialog "書き出しおわりました"
	end tell
end control_sub

--------------------------------------------------------●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 story_export()
	set tmp_txt to "" as Unicode text
	tell application "InDesign CS_J"
		tell document 1
			convert to text every table of every story --テーブルをテキストに変換
			set every_story to every story
			repeat with i in every_story
				set tmp_i_txt to contents of (object reference of i)
				set tmp_txt to tmp_txt & tmp_i_txt & return & return
			end repeat
		end tell
	end tell
	return tmp_txt
end story_export

--------------------------------------------------------●テキスト抜き出し(ページごと)
to page_export()
	set tmp_txt to "" as Unicode text
	tell application "InDesign CS_J"
		tell document 1
			convert to text every table of every story
			repeat with i from 1 to count pages
				tell page i
					set page_name to name
					set tmp_txt to tmp_txt & "-------------------------- Page : " & page_name & return & return
					--ページ中のテキストフレームの抽出
					set tmp_obj to all page items
					set my_obj to {} --テキストフレームオブジェクトのリストの初期化
					repeat with i in tmp_obj
						if class of i = text frame then
							set end of my_obj to object reference of i
						end if
					end repeat
					
					--各テキストフレームへのアクセス
					repeat with ii in my_obj
						set tmp_txt to tmp_txt & return & return & contents of (object reference of ii) & return
					end repeat
				end tell
			end repeat
		end tell
	end tell
	return tmp_txt
end page_export
------------------------------------------★ファイル書き出し
on write_file(contents_txt, my_file)
	tell application "Finder"
		set FH to open for access my_file with write permission
		try
			write contents_txt to FH
		on error errMsg number errNo
			close access FH
			activate
			display dialog "書き込み時のエラーです" & return & errMsg & errNo
			return
		end try
		close access FH
		update my_file
	end tell
end write_file

------------------------------------------★エラー処理
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"
		--set user interaction level to interact with all --ユーザー操作の禁止を解除します
		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