folder.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const { Cc, Ci, Cu, CC } = require("chrome");
  6. const Services = require("Services");
  7. const l10n = require("gcli/l10n");
  8. const dirService = Cc["@mozilla.org/file/directory_service;1"]
  9. .getService(Ci.nsIProperties);
  10. function showFolder(aPath) {
  11. let nsLocalFile = CC("@mozilla.org/file/local;1", "nsILocalFile",
  12. "initWithPath");
  13. try {
  14. let file = new nsLocalFile(aPath);
  15. if (file.exists()) {
  16. file.reveal();
  17. return l10n.lookupFormat("folderOpenDirResult", [aPath]);
  18. } else {
  19. return l10n.lookup("folderInvalidPath");
  20. }
  21. } catch (e) {
  22. return l10n.lookup("folderInvalidPath");
  23. }
  24. }
  25. exports.items = [
  26. {
  27. name: "folder",
  28. description: l10n.lookup("folderDesc")
  29. },
  30. {
  31. item: "command",
  32. runAt: "client",
  33. name: "folder open",
  34. description: l10n.lookup("folderOpenDesc"),
  35. params: [
  36. {
  37. name: "path",
  38. type: { name: "string", allowBlank: true },
  39. defaultValue: "~",
  40. description: l10n.lookup("folderOpenDir")
  41. }
  42. ],
  43. returnType: "string",
  44. exec: function(args, context) {
  45. let dirName = args.path;
  46. // replaces ~ with the home directory path in unix and windows
  47. if (dirName.indexOf("~") == 0) {
  48. let homeDirFile = dirService.get("Home", Ci.nsIFile);
  49. let homeDir = homeDirFile.path;
  50. dirName = dirName.substr(1);
  51. dirName = homeDir + dirName;
  52. }
  53. return showFolder(dirName);
  54. }
  55. },
  56. {
  57. item: "command",
  58. runAt: "client",
  59. name: "folder openprofile",
  60. description: l10n.lookup("folderOpenProfileDesc"),
  61. returnType: "string",
  62. exec: function(args, context) {
  63. // Get the profile directory.
  64. let currProfD = Services.dirsvc.get("ProfD", Ci.nsIFile);
  65. let profileDir = currProfD.path;
  66. return showFolder(profileDir);
  67. }
  68. }
  69. ];