Application Launcher を作ってみた
XPCOMから外部コマンドを呼べる事が分かったので、試しにアプリケーションランチャーを作ってみた。出来る事が少ないので未完成品。だけどコマンド起動方法の断片として他で使うかもしれないからメモ代わりに置いとく。
本当はコマンドの標準出力を受け取って、それを加工してサイドバーに出力、とか考えてたけど、どうも標準出力は受け取れないみたいだ。リダイレクトでファイルに書き出して読むしか手は無いのかな...。
/** * external Application Launcher * @author teramako teramako@gmail.com * @version 0.1 * @license MPL 1.1/GPL 2.0/LGPL 2.1 */ (function(){ var entries = [ { name: 'GranPradiso', path: 'D:\\usr\\lib\\firefox\\Gran Paradiso\\firefox.exe', args: [ '-P','GranParadiso','%URL%' ] }, { name: 'IE', path: 'C:\\Program Files\\Internet Explorer\\iexplore.exe', args: [ '%URL%' ] } ]; /** * Initialize * Setting toolbar */ function initialize(){ var mainMenu = document.getElementById('main-menubar'); var menu = document.createElement('menu'); menu.setAttribute('label','Launcher'); var menupopup = document.createElement('menupopup'); for (var i=0; i<entries.length; i++){ var menuitem = document.createElement('menuitem'); menuitem.setAttribute('label', entries[i].name ); menuitem.setAttribute('position', i ); menuitem.addEventListener('command', applicationExecute,false); menupopup.appendChild( menuitem ); } menu.appendChild( menupopup ); mainMenu.appendChild( menu ); } /** * Execute application */ function applicationExecute(){ var index = this.getAttribute('position'); var application = getApplication( entries[index].path ); var args = getReplecedArguments( entries[index].args ); application.run( false, args, args.length ); } /** * Return repleced arguments * @param {Array} _args * @return {Array} repreced arguments */ function getReplecedArguments( _args ){ var args = []; for( var i=0; i<_args.length; i++){ args.push( _args[i].replace(/%URL%/,document.getElementById('content').currentURI.spec) ); } return args; } /** * get Application from path * @param {String} _appPath ApplicationFilePath * @return {nsIProcess} application */ function getApplication( _appPath ){ var application; var appFile = Components.classes['@mozilla.org/file/local;1'] .createInstance( Components.interfaces.nsILocalFile ); appFile.initWithPath( _appPath ); if ( appFile.exists() && appFile.isExecutable() ){ application = Components.classes['@mozilla.org/process/util;1'] .createInstance( Components.interfaces.nsIProcess ); application.init( appFile ); } else { throw 'Exception: FileNotFound or FileNotExecutable'; } return application; } initialize(); })();
entries
変数にJSON形式で登録すればいろいろ起動できる。
コマンド引数に%URL%を入れておくと、現在開いているページのURLに変換される。他は用意してないがgetReplacedArguments
関数を適当に弄ればいろいろと出来るだろう。