Firefox3 でプロファイル名を得る

なんだか気色悪いが、jslib を参考に書いた物。

homeDir を return すれば、プロファイルのディレクトリパスが得られるようになる。

function getCurrentProfileName () {
  let homeDir = Cc['@mozilla.org/file/directory_service;1']
                  .getService(Ci.nsIProperties)
                  .get("ProfD", Ci.nsIFile)
                  .path;
    return homeDir.replace(/.*[\\\/]/, '').replace(/.+?\./, '');
}
alert(getCurrentProfileNaZme());

プロファイル名に "." が含まれるとおかしくなるのを修正した。(02:40)

どこかで見かけた間違ったパターン。

こちらは、"firefox -p " として指定された物を返す。
指定無しの場合は、最後に指定したものが返る?
いずれにせよ、現在のプロファイル名が得られるとは限らないので罠。

function getCurrentProfileName_Wrong () {
  return Cc['@mozilla.org/toolkit/profile-service;1']
            .getService(Ci.nsIToolkitProfileService)
            .selectedProfile
            .name;
}
alert(getCurrentProfileName_Wrong());

プロファイル毎に runtimepath を設定する。

以下のように配置する前提。
変えたい場合は適当にコードをいじるべし。

.vimperator/
    default/
      colors/
      plugin/
    profile2/
      colors/
      plugin/
    profile3/
      colors/
      plugin/
    ...

.vimperatorrc に埋め込むコード

{
  let defaultName = 'default';
  let option = liberator.modules.options.get('runtimepath');
  let root = option.get();
  let homeDir = Cc['@mozilla.org/file/directory_service;1']
                  .getService(Ci.nsIProperties)
                  .get("ProfD", Ci.nsIFile)
                  .path;
  let profileName = homeDir.replace(/.*[\\\/]/, '').replace(/.+?\./, '');
  let runtimepath = root + '/' + profileName;
  if (liberator.modules.io.getFile(runtimepath).exists()) {
    option.set(runtimepath);
  } else {
    option.set(root + '/' + defaultName);
  }
}

coderepos に vimperator-plugins/branches/1.2 が出来たので、書いてみた。

あうば

1.2では runtimepath とかなかったりする?

  • > 1.2対応版書いた

プロファイル切り替え、1.2対応版

http://d.hatena.ne.jp/janus_wel/20081104/1225823130 を受けてじゃあ 1.2 用の環境を作ろうかなと思ったらすでに id:nokturnalmortum が動き出していた。速いよ。そして最後に書いてあるように 1.2 には runtimepath オプションがないので動かなかった。 2.0pre で切り替えする分には使える ( はず ) 。

1.2に無理矢理対応してみた。
前のと同様に、rc に入れればOK。
Headと公式の1.2で確認しました。
うまくいかなかったらおせーてください。

{
  // プロファイル名のディレクトリがないときはコレ
  let defaultName = 'default';
  // 1.2用のルートを設定
  let vimp12root = null;

  {
    let homeDir = Cc['@mozilla.org/file/directory_service;1']
                    .getService(Ci.nsIProperties)
                    .get("ProfD", Ci.nsIFile)
                    .path;
    let profileName = homeDir.replace(/.*[\\\/]/, '').replace(/.+?\./, '');

    let option = options.get('runtimepath');

    if (option) {
      let root = option.get();
      let runtimepath = root + '/' + profileName;
      if (io.getFile(runtimepath).exists()) {
        option.set(runtimepath);
      } else {
        option.set(root + '/' + defaultName);
      }
    } else {
      let root = vimp12root || ("~/" + config.name.toLowerCase() + "/");
      let runtimepath = root + profileName;
      let dir = io.getFile(runtimepath);
      if (!dir.exists())
        runtimepath = root + defaultName;
      io.getSpecialDirectory = function (directory) {
        let pluginDir = ioManager.getFile(ioManager.expandPath(runtimepath + "/" + directory));
        return pluginDir.exists() && pluginDir.isDirectory() ? pluginDir : null;
      };
    }
  }
}

1.2とHead で実現方法に違いがあるけど、大丈夫だよね。多分。
こういうのはプラグインというわけには行かないのがちと残念。
つーか、まともなプロファイル名取得方法は無いものか。