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

(DTPtechNote:1390) [AS_Indesign CS2]Save PDF 0.4



Indesign CS2用に少し書き直しました。
あー、ASひさしぶりだなぁ。。。やっぱりScriptDebugger欲しいなぁ。。
http://www.latenightsw.com/sd4/index.html
#もう痛いんだか便利なんだかよくわからない^^



on open of theFiles
	my ver()
	--書き出しPDFスタイルを選択する
	set pdf_export_presets to my pdf_style()
	
	--ファイルのフィルタリング
	set my_files to my file_kind({"ind", "indd"}, {"IDd4"}, theFiles)
	
	--ファイルを開いて、コントロールルーチンへ
	repeat with i in my_files
		tell application "Adobe InDesign CS2_J"
			set user interaction level of script preferences to never interact --ユーザー操作を禁止します
			try
				open i
			on error errMsg number errNum
				set user interaction level of script preferences to interact with all --ユーザー操作の再開
				my my_error("誤りが起きました;" & i & return & "処理を中断しますか?" & return & errMsg & return & errNum, false)
			end try
			set user interaction level of script preferences to interact with all --ユーザー操作の再開
			my control_routin(pdf_export_presets)
			close document 1 saving no
		end tell
	end repeat
	
	--終了の合図
	say "Work was finished. " & (length of my_files) & " files were processed."
end open

on run
	my ver()
	my doc_exists()
	--書き出しPDFスタイルを選択する
	set pdf_export_presets to my pdf_style()
	
	my control_routin(pdf_export_presets)
	--終了の合図
	say "Work was finished. "
end run


--------------------------------------------------------●コントロールルーチン
to control_routin(pdf_export_presets)
	
	set save_PDF_path to my make_PDF_path()
	
	try
		my save_PDF(save_PDF_path, pdf_export_presets)
	on error errMsg number errNum
		my my_error("誤りが起きました;" & save_PDF_path & return & "処理を中断しますか?" & return & errMsg & return & errNum, false)
	end try
end control_routin


--------------------------------------------------------●InDesignバージョン確認ルーチン
to ver()
	tell application "Adobe InDesign CS2_J"
		set my_version to (version of script preferences) as real
		if my_version < 4 or my_version > 4.9 then
			activate
			my my_error("このプログラムはInDesign CS2以外では動作しません", true)
		end if
		my_version
	end tell
end ver


--------------------------------------------------------●書き出しPDFスタイルを選択する
to pdf_style()
	tell application "Adobe InDesign CS2_J"
		set pdf_export_presets to name of PDF export presets
		set pdf_export_presets to choose from list pdf_export_presets with prompt ("PDF書き出しプリセットを選んでください") as Unicode text
	end tell
	if pdf_export_presets = false then my my_error("処理を中止します", true)
	return pdf_export_presets as Unicode text
end pdf_style


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



----------------------------------------------●必要なファイルだけをフィルタして返します
to file_kind(extention_list, type_list, theFiles)
	set my_files to {}
	set extention_list to my conv_unicode(extention_list)
	set type_list to my conv_unicode(type_list)
	
	ignoring case
		tell application "Finder"
			repeat with i in theFiles
				if extention_list contains ((name extension of i) as Unicode text) then
					set end of my_files to contents of i
				else if (kind of i) is "フォルダ" as Unicode text then
					activate
					display dialog "フォルダ「" & (name of i) & "」の中の全ファイルを処理します" buttons {"キャンセル", "OK"} default button 2 with icon 0
					set my_files to my_files & my file_kind(extention_list, type_list, every file in folder i)
				else if type_list contains ((file type of i) as Unicode text) then
					set end of my_files to contents of i
				else
					activate
					display dialog "ファイル「" & (name of i) & "」は処理ファイルとして不適当です" buttons {"キャンセル", "OK"} default button 2 with icon 0
				end if
			end repeat
		end tell
	end ignoring
	return my_files
end file_kind


--------------------------------------------------------●PDFのパスを生成する
to make_PDF_path()
	tell application "Adobe InDesign CS2_J"
		tell document 1
			set my_name to (name) as Unicode text
			try
				set my_folder to (file path) as Unicode text
			on error errMsg number errNum
				activate
				set save_PDF_path to choose file name with prompt ("どこにPDFを保存しますか?") as Unicode text default name my_name & ".pdf"
				return save_PDF_path
			end try
			
			--拡張子の変換
			if my_name ends with ".indd" then
				set save_PDF_path to my_folder & my as_replace(my_name, ".indd", ".pdf")
			else
				set save_PDF_path to my_folder & my_name & ".pdf"
			end if
			
		end tell
	end tell
	set save_PDF_path to my filepath_test(save_PDF_path, 1)
	return save_PDF_path
end make_PDF_path


--------------------------------------------------------●ファイルパスの検証と自動生成
--引数に与えられたファイルが存在する場合は、新たに生成し、一意であればそれを返す。
--cnt変数はループ回数、これをファイル名にくっつけていく。最初は必ず1から始めること。
to filepath_test(file_path, cnt)
	tell application "Finder"
		if exists file file_path then --ファイルパスは既に存在する
			--初めてではない時は、cnt識別子を削除する
			if cnt > 1 and cnt < 10 then
				set file_path to (text 1 thru -3 of file_path) as Unicode text
			else if cnt > 9 then
				set file_path to (text 1 thru -4 of file_path) as Unicode text
			end if
			--とりあえず100回チャレンジ!
			if cnt < 100 then
				my filepath_test((file_path & "_" & cnt) as Unicode text, (cnt + 1))
			else --100回以上のトライしてもダメ(一意にならない)でした
				set file_path to choose file name with prompt ("どこに保存しますか?") as Unicode text
				return (file_path as Unicode text)
				--my my_error("ファイル名エラーです", true)
			end if
		else
			return (file_path as Unicode text)
		end if
	end tell
end filepath_test


--------------------------------------------------------●PDFをsaveする
to save_PDF(save_PDF_path, pdf_export_presets)
	tell application "Adobe InDesign CS2_J"
		set page range of PDF export preferences to all pages --すべてのページを書き出す
		with timeout of 3600 seconds
			export document 1 format PDF type to save_PDF_path using PDF export preset pdf_export_presets
		end timeout
	end tell
end save_PDF




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

----------------------------------●リストの項目をunicode textにする
to conv_unicode(tmp_list)
	repeat with i in tmp_list
		set contents of i to (contents of i) as Unicode text
	end repeat
	return tmp_list
end conv_unicode

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