AppleScriptとターミナルでJavaScriptを使う

AppleScriptでJavaScriptを使いたい
applescript jsc javascriptcore
具体には、URLをエンコード(url encode)したい時、
set theText to "http://ja.wikipedia.org/wiki/アップルスクリプト"
--JavaScript
tell application "Safari"
set theURL to (do JavaScript "encodeURI(\"" & theText & "\")" in document 1) as text
log "0=" & theURL
end tell
(*0=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*)
と、Safariを経由して結果を得る方法が考えられるが、Safariを経由せず、ApplescriptでJavaScriptを使えないか探ってみた。
Safariで使われているJavaScriptCoreを直接操作する方法があった。
set theText to "http://ja.wikipedia.org/wiki/アップルスクリプト"
--JavaScript
set theURL to do shell script "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -e 'print(escape(\"" & theText & "\"))'"
log "1=" & theURL
(*1=http%3A//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*)
キモは
print([native code])
ターミナルでPATHを通すことで下記とできる。自分のMacだけで使う時はこれでOK。
do shell script "jsc -e 'print(eccape(\"" & theText & "\"))'"
ln /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /usr/bin
AppleScriptでJavaScriptを使わずにURLエンコードする。Perl, Ruby
--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*)
ターミナルでJavaScriptを使いたい
tarminal jsc
ターミナルでURLをエンコードしてみる
/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -e 'print(escape("http://ja.wikipedia.org/wiki/アップルスクリプト"))'
又は
/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc encodeurl.js
ホームディレクトリに「encodeurl.js」を新規作成し下記を記述。
// encodeurl.js
(function(){
print(escape("http://ja.wikipedia.org/wiki/アップルスクリプト")); //標準出力は print 関数
}()); // コードは即時関数で実行させる
JavaScriptCoreへPATHを通す
1 - .bash_profileに記述する方法
ホームディレクトリの「.bash_profile」というファイルにJavaScriptCoreへのPATHを記述します。
alias jsc="/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc"
ターミナルで.bash_profileを開き上記を追記して保存。.bash_profileがない時は新規作成される。
vim ~/.bash_profile
変更した.bash_profileを反映させる
source ~/.bash_profile
2 - ターミナルでハードリンクまたはシンボリックリンクする方法
ターミナルでPATHを通す
ln /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /usr/local/bin
.bash_profileへの操作、vimのコマンドについての参考サイト
- MacのターミナルでJSを走らせる。 - modifiedの日記
- Mac OS Xで環境変数にPATHを追加する方法 at HouseTect, JavaScriptな情報をあなたに
- feb19.jp blog - VIM (Vi) をターミナルで操作 / Mac OS X
- viエディタの使い方
ターミナルで試してみる
jsc -e 'print(escape("http://ja.wikipedia.org/wiki/アップルスクリプト"))'
jsc encodeurl.js