AppleScriptでURLエンコード

AppleScriptでJavaScriptを使わずにURLエンコードする。Perl, Ruby
set theText to "http://ja.wikipedia.org/wiki/アップルスクリプト"
--Perl
set theURL to do shell script "perl -MURI::Escape -wle 'print uri_escape(\"" & theText & "\")'"
log "2=" & theURL
(*2=http%3A%2F%2Fja.wikipedia.org%2Fwiki%2F%E3%82%A2%E3%83%83%E3%83%97%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88*)
--perl
set theURL to do shell script "perl -MEncode -MURI::Escape -e 'print uri_escape(encode('utf8', decode('utf8', \"" & theText & "\")))'"
log "3=" & theURL
(*3=http%3A%2F%2Fja.wikipedia.org%2Fwiki%2F%E3%82%A2%E3%83%83%E3%83%97%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88*)
--ruby
--AppleScript : URLエンコード http://blog.goo.ne.jp/vallie/e/26f5d6333f44e071863c173300865306
set theURL to do shell script "ruby -r cgi -e 'puts CGI.escape(\"" & theText & "\")'"
log "4=" & theURL
(*4=http%3A%2F%2Fja.wikipedia.org%2Fwiki%2F%E3%82%A2%E3%83%83%E3%83%97%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88*)
--URL全体をエンコードしたいときは、上記のエンコード方法では有効なURLとならないので、有効なURLとしてエンコードしておきたいときは、次をかませる。
--有効なURLへ
set theURL to do shell script "echo " & quoted form of theURL & " | sed -e 's/%23/#/g' -e 's/%24/$/g' -e 's/%26/&/g' -e 's/%2B/+/g' -e 's/%2C/,/g' -e 's/%2F/\\//g' -e 's/%3A/:/g' -e 's/%3B/;/g' -e 's/%3D/=/g' -e 's/%3F/?/g' -e 's/%40/@/g'"
logtheURL
(*http://ja.wikipedia.org/wiki/%E3%82%A2%E3%83%83%E3%83%97%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88*)
--デコード
set theURL to do shell script "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -e 'print(decodeURI(\"" & theURL & "\"))'"
logtheURL
(*http://ja.wikipedia.org/wiki/アップルスクリプト*)