アルファサード株式会社 代表取締役 野田 純生のブログ


TextWrangler(Mac)用追加メニュー。


公開日 : 2011-01-22 16:39:43


文字実体参照に変換したりURLエンコードしたりquotemetaしたり何かと面倒なので久しぶりにAppleScriptで書いてみた。

TextWranglerのスクリプトメニュー

とりあえずは下記の5種類。

  • 文字実体参照変換
  • 文字実体参照から戻す
  • URLエンコード
  • URLデコード
  • quotemeta(Perl)

↓TextWranglerユーザーの方はよろしければどうぞ。

# 直接PerlやPythonとかでも書けるみたい(今回は選択範囲とかの受け渡し方がちょっとわからんかったのでAppleScriptで書いた)。

~/Library/Application Support/TextWrangler/AppleScriptディレクトリを作成。

最初に定番?の文字列置換モジュールを書いてAppleScriptディレクトリに設置。

(*
TextReplacer.scpt
*)
on replace_all(theText, serchStr, replaceStr)
	set theDelim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to serchStr
	set theList to every text item of theText
	set AppleScript's text item delimiters to replaceStr
	set theText to theList as string
	set AppleScript's text item delimiters to theDelim
	return theText
end replace_all

メニュースクリプトは ~/Library/Application Support/TextWrangler/Scripts 以下に置く

簡単な文字実体参照変換とか...

(*
To Character Entity Reference.scpt
*)
set libpath to path to library folder from user domain
set libpath to (libpath as Unicode text) & "Application Support:TextWrangler:AppleScript:TextReplacer.scpt"
set TextReplacer to load script file libpath

tell application "TextWrangler"
	set targetWin to window 1
	set selectionText to selection of targetWin as Unicode text
	set selectionText to TextReplacer's replace_all(selectionText, "&", "&")
	set selectionText to TextReplacer's replace_all(selectionText, "<", "&lt;")
	set selectionText to TextReplacer's replace_all(selectionText, ">", "&gt;")
	set selectionText to TextReplacer's replace_all(selectionText, "\"", "&quot;")
	set selection of targetWin to selectionText
end tell

Perlのワンライナーを実行させたり...

(*
Perl Quotemeta.scpt
*)
tell application "TextWrangler"
	set targetWin to window 1
	set selectionText to selection of targetWin as Unicode text
	set selectionText to quoted form of selectionText
	set command to "perl -e"
	set perlScript to quoted form of "print quotemeta($ARGV[0])"
	set selectionText to do shell script command & " " & perlScript & " " & selectionText
	set selection of targetWin to selectionText
end tell

正規表現も...

(*
Encode URL.scpt
*)
tell application "TextWrangler"
	set targetWin to window 1
	set selectionText to selection of targetWin as Unicode text
	set selectionText to quoted form of selectionText
	set command to "perl -e"
	set perlScript to quoted form of "$s=$ARGV[0];$s=~s!([^a-zA-Z0-9_.~-])!uc sprintf \"%%%02x\", ord($1)!eg;print $s"
	set selectionText to do shell script command & " " & perlScript & " " & selectionText
	set selection of targetWin to selectionText
end tell

こんな配布の仕方もできるのね...

カテゴリ


このブログを書いている人
野田純生の写真
野田 純生 (のだ すみお)

大阪府出身。ウェブアクセシビリティエバンジェリスト。 アルファサード株式会社の代表取締役社長であり、現役のプログラマ。経営理念は「テクノロジーによって顧客とパートナーに寄り添い、ウェブを良くする」。 プロフィール詳細へ