3
對於編寫Firefox附加組件來說,我還是個新手,但我還是得到了一個將補丁一起打包的工作,除了它的核心。如何在Firefox附加組件中執行命令?
在代碼中的某個位置,我需要在一個文件上執行一個系統命令。我找到了一個在網上剪下來,並試圖適應它,但沒有運氣。從我的xpi/components/script.js
:
var cmd = '/usr/bin/somecommand'
var args = ['-option', 'value', f.path ];
var execFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
execFile.initWithPath(cmd);
if (execFile.exists()) {
process.init(execFile);
process.run(false, args, args.length);
}
有人可以告訴我這裏有什麼問題嗎?我已經彙編了這個命令,並且已經有了文件名,但是我無法讓Firefox執行這段代碼片段。
是這樣的execFile
,initWithPath
,createInstance
等東西真的需要?我只是想執行的命令就像在CLI:
$ somecommand -option value filename
你怎麼知道它沒有執行,你會得到一個異常嗎? – Neil