I've created simple firefox add-on, which should search for selected on page substring in files. Everything is ok until I try to search for substring in some specific language, russian fo example. The search command executed using call to windows cmd command propmt as follow:
function getShell()
{
var env = Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment);
var shell = new FileUtils.File(env.get("COMSPEC"));
var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
process.init(shell);
return process;
}
var shell = getShell();
var text = "россия";
var folder = "c:\\temp\\test\\*.*";
var cmd = "chcp 65001 & c:\\progra~1\\gnuwin32\\bin\\grep.exe -i -r " + text + " " + folder + " > c:\\temp\\test\\output.txt";
cmd = utf16to8(cmd);
args = ["/k", cmd];
shell.run(true, args, args.length);
So, I've tried deode utf-16 to utf-8 and execute (changing the code page inside shell execution) with no success. But I've succeeded as follow:
var cmd = "chcp 1251 & c:\\progra~1\\gnuwin32\\bin\\grep.exe -i -r " + text + " " + folder + " > c:\\temp\\test\\output.txt";
cmd = UnicodeToWin1251(cmd);
args = ["/k", cmd];
shell.run(true, args, args.length);
The last one works fine, but it does not work with other languages. How to convert internal javascript string to utf-8 and execute command usig 65001 codepage?
UnicodeToWin1251 taken here utf16to8 taken here
I've resolved the issue. To get javascript engine correctly pass parameters in unicode encoding it is necessary to use method runw of the nsIProcess. So, the line of code to launch shell command will be as follow:
In such case arguments, using UTF-16, will be passed to the executable on its command line.
RTFM here