Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 1 down vote accepted

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'.

share|improve this answer
    
It works perfectly! It is the second time your answer helps me. Thank you so much! – rouraito Mar 5 '14 at 15:30
    
I realize you put "tr -d \\n" command in order to get rid of new line character in the string and dump the "0a" in the hex output. I think it should be placed after the string like this:"string"|tr -d \\n|iconv -f utf-8 -t gb2312|xxd -p|sed 's/../%&/g' – rouraito Mar 17 '14 at 14:52
    
No, tr -d \\n is meant to remove linefeeds from the output of xxd -p (which prints 60 characters per line). You can use printf %s "$(cat)" to remove a linefeed from the end of the input. Or replace echo with printf %s. – user495470 Mar 18 '14 at 13:15
    
My mistake. Thank you for clarifying this. – rouraito Mar 19 '14 at 12:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .