ImageMagickで-trimして、長辺が最大ピクセル値を超えたら-resizeするドロップレット

(*
convert-trim-resize.app
ImageMagickで
convert -trim orginal-file new-file
を実行する。
ピクセル数の最大値my_maxpixelを書き換えると、長辺がこの値を超えた時だけresizeします。
オプション変えたい人は ドロップレットのdo shell scriptの部分を直接書き換えてくだしあ。


2012-06-10	とりあえず
*)


--ドラッグ&ドロップで実行
on open of drop_files
	--設定
	set my_maxpixel to 1536 --長辺のピクセル最大値
	
	--ファイルのフィルタリング
	set my_files to my file_kind({"jpg", "jpeg", "png", "gif", "tif", "tiff", "bmp"}, {""}, drop_files) --拡張子のリスト、ファイルタイプのリスト、処理ファイル	
	
	repeat with i in my_files
		my violation_name(i as alias)
		set path_info to my split_path(i as alias)
		set org_path to POSIX path of (i as alias)
		set new_path to (dir of path_info) & (f_name of path_info) & "_new" & (ext of path_info)
		my alert_same_name(new_path)
		try
			do shell script "/usr/local/bin/convert -trim " & (quoted form of org_path) & " " & (quoted form of new_path) --空白除去
			set new_w to (do shell script "/usr/local/bin/identify -format '%w' " & (quoted form of new_path)) as integer
			set new_h to (do shell script "/usr/local/bin/identify -format '%h' " & (quoted form of new_path)) as integer
			if (new_w > my_maxpixel) or (new_h > my_maxpixel) then --幅または高さがピクセル最大値を超えていた
				do shell script "/usr/local/bin/mogrify -resize " & my_maxpixel & "x" & my_maxpixel & " " & (quoted form of new_path) --上書きでリサイズ
			end if
		on error errMsg number errNo
			display dialog "実行エラーです" & return & errMsg & return & errNo buttons {"Cancel"} default button 1 with icon 1 --キャンセルで終了
		end try
	end repeat
end open


----------------------------------------------●必要なファイルだけをフィルタして返します
to file_kind(extention_list, type_list, theFiles)
	set my_files to {}
	
	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

----------------------------------------------●
--フルパス名を、コンテナディレクトリ・ファイル名+拡張子・ファイル名・拡張子に分割する
--返値はPOSIX pathのレコード {dir:"", name_ext:"", f_name:"", ext:""}
to split_path(f)
	set f to quoted form of ((POSIX path of f) as Unicode text) --まずPOSIX pathにする
	set d to do shell script "echo " & f & " | perl -pe 's/^(.+\\/).+$/$1/;'" --コンテナディレクトリ
	set ne to do shell script "echo " & f & " | perl -pe 's/^.+\\/(.)/$1/;'" --ファイル名+拡張子
	if f ends with "/'" then --フォルダの場合
		set n to text 1 thru -2 of ne
		set e to ""
	else
		set e to do shell script "echo '" & ne & "' | perl -pe 's/^.+(\\..+)$/$1/;'"
		if e is not "" then
			set n to do shell script "echo '" & ne & "' | perl -pe 's/" & e & "$//;'"
		end if
	end if
	
	return {dir:d, name_ext:ne, f_name:n, ext:e}
end split_path

----------------------------------------------●
--同名ファイルの警告
to alert_same_name(file_path)
	tell application "Finder"
		if (exists (file_path as POSIX file)) then
			display dialog file_path & " は同名のファイルがあります" & return & "上書きしますか?" buttons {"Cancel", "上書き"} default button 1 with icon 1 --キャンセルで終了
		end if
	end tell
end alert_same_name

----------------------------------------------●
--ファイル名の警告
to violation_name(file_path)
	set file_path to quoted form of ((POSIX path of file_path) as Unicode text) --まずPOSIX pathにする
	set file_path_test to do shell script "echo " & file_path & " | perl -pe 's/[\\/\\.0-9A-Za-z _-]+//;'"
	if (file_path_test is not "") then
		display dialog file_path & " のファイル名には英数字以外の文字が使われています。" & return & "処理トラブルになるかもしれません。続行しますか?" buttons {"Cancel", "続行"} default button 1 with icon 1 --キャンセルで終了
	end if
end violation_name