1

I find a script here

Mac OS X (Lion) Chrome: shortcut for "Search With Google"

which shows how to do google search using shell script in OS X Automator.

The original script is:

open "http://www.google.com/search?q=$(ruby -rcgi -e 'print CGI.escape $<.read.chomp')"

I am trying to adapt this script to a custom search for Chinese characters encoded in "gb2312".

Currently my script goes like:

open "http://www.yueyv.cn/index.asp?keyword=$(ruby -rcgi -e 'print CGI.escape $<.read.chomp.encode("gb2312")')"

It works fine in terminal. For example, if testing with character "一", the script opens http://www.yueyv.cn/index.asp?keyword=%D2%BB/

However when adding this script as a service in OS X automator, it opens http://www.yueyv.cn/index.asp?keyword=/

The code of "一" is gone.

I've googled for quite a while without a result. Can anybody help me? Thank you.

1 Answers1

1

Terminal sets LANG to a value like en_US.UTF-8 by default if you haven't unchecked "Set locale environment variables on startup". Automator doesn't, so the ruby command results in an invalid byte sequence error.

$ unset LANG
$ echo 一|ruby -rcgi -e 'puts CGI.escape $<.read.chomp.encode("gb2312")'
-e:1:in `encode': "\xE4" on US-ASCII (Encoding::InvalidByteSequenceError)
    from -e:1:in `<main>'
$ echo 一|LC_CTYPE=UTF-8 ruby -rcgi -e 'puts CGI.escape $<.read.chomp.encode("gb2312")'
%D2%BB

Try to use LC_CTYPE=UTF-8 ruby. Or replace the ruby command with iconv -f utf-8 -t gb2312|xxd -p|tr -d \\n|sed 's/../%&/g'.

Lri
  • 42,502
  • 8
  • 126
  • 159