_puttycolor.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Registry find library
  2. var Registry = function(iTree, sComputerName) {
  3. this.iTree = (iTree || Registry.HKEY_LOCAL_MACHINE);
  4. this.sComputerName = (sComputerName || '.');
  5. var locStr = 'winmgmts:{impersonationLevel=impersonate}//'
  6. + this.sComputerName
  7. + '/root/default:StdRegProv';
  8. this.oRegistry = GetObject(locStr);
  9. };
  10. // Add some useful constants to the Registry class.
  11. (function() {
  12. var constants = { 'HKEY_CLASSES_ROOT': 0x80000000
  13. , 'HKEY_CURRENT_USER': 0x80000001
  14. , 'HKEY_LOCAL_MACHINE': 0x80000002
  15. , 'HKEY_USERS': 0x80000003
  16. , 'HKEY_CURRENT_CONFIG': 0x80000005
  17. , 'HKEY_DYN_DATA': 0x80000006
  18. , 'type': { 'REG_SZ': 1
  19. , 'REG_EXPAND_SZ': 2
  20. , 'REG_BINARY': 3
  21. , 'REG_DWORD': 4
  22. , 'REG_MULTI_SZ': 7 }
  23. };
  24. for (var c in constants) { Registry[c] = constants[c]; }
  25. })();
  26. // Create Registry class methods which which wrap WMI calls.
  27. (function () {
  28. var pro = Registry.prototype;
  29. var wmiSig = { 'EnumKey': ['hDefKey', 'sSubKeyName']
  30. , 'EnumValues': ['hDefKey', 'sSubKeyName']
  31. , 'GetBinaryValue': ['hDefKey', 'sSubKeyName', 'sValueName']
  32. , 'GetDWORDValue': ['hDefKey', 'sSubKeyName', 'sValueName']
  33. , 'GetExpandedStringValue': ['hDefKey', 'sSubKeyName', 'sValueName']
  34. , 'GetStringValue': ['hDefKey', 'sSubKeyName', 'sValueName']
  35. , 'GetMultiStringValue': ['hDefKey', 'sSubKeyName', 'sValueName']
  36. };
  37. for (var methodName in wmiSig) {
  38. pro[methodName] = (function(name, args) {
  39. return function(inSpec) {
  40. var oMeth = this.oRegistry.Methods_(name)
  41. , oIn = oMeth.inParameters.SpawnInstance_();
  42. for (var i in args) {
  43. if (inSpec[args[i]]) {
  44. oIn[args[i]] = inSpec[args[i]];
  45. }
  46. }
  47. return this.oRegistry.ExecMethod_(name, oIn);
  48. }
  49. })(methodName, wmiSig[methodName]);
  50. }
  51. })();
  52. // Traverses from the given root key and yields key/values to the handler.
  53. Registry.prototype.find = function(rootKey, handler) {
  54. var StopWalkingException = function() { }
  55. , that = this;
  56. var findInner = function(key) {
  57. // Yield all values to the handler.
  58. var oOut = that.EnumValues({hDefKey: that.iTree, sSubKeyName:key});
  59. try {
  60. var aNames = oOut.sNames.toArray()
  61. , aTypes = oOut.Types.toArray();
  62. for (var i=0; i<aNames.length; i++) {
  63. // TODO: yield the appropriate value, not just strings.
  64. if (aTypes[i] === Registry.type.REG_SZ) {
  65. var valueName = key + '\\' + aNames[i];
  66. oOut = that.GetStringValue({hDefKey: that.iTree, sSubKeyName:key, sValueName:aNames[i]});
  67. if (!handler(valueName, oOut.sValue.toString())) {
  68. throw new StopWalkingException();
  69. }
  70. }
  71. }
  72. } catch (e) { // Ignore missing toArray method.
  73. if (e instanceof StopWalkingException) throw e;
  74. }
  75. // Recurse into any sub keys.
  76. oOut = that.EnumKey({hDefKey: that.iTree, sSubKeyName:key});
  77. try {
  78. var aSubKeys = oOut.sNames.toArray();
  79. for (var i=0; i<aSubKeys.length; i++) {
  80. findInner(key + '\\' + aSubKeys[i]);
  81. }
  82. } catch (e) { // Ignore missing toArray method.
  83. if (e instanceof StopWalkingException) throw e;
  84. }
  85. };
  86. // Search until naturally done or the handler says stop.
  87. try {
  88. findInner(rootKey);
  89. } catch (e) {
  90. if (!(e instanceof StopWalkingException)) { throw e; }
  91. return false;
  92. }
  93. return true;
  94. };
  95. // my code
  96. var args = WScript.Arguments;
  97. if (args.length<1) {
  98. WScript.Echo("Drag'n'Drop .reg file to this script in Windows Explorer");
  99. WScript.Quit(666);
  100. }
  101. var FilePath = args.item(0);
  102. var result1 = WScript.CreateObject("WScript.Shell").Popup("Apply colors settings from "+FilePath+"?", 0, "Putty Color", 68);
  103. if (result1!=6 ) { WScript.Quit(666); }
  104. fs = new ActiveXObject("Scripting.FileSystemObject");
  105. f = fs.GetFile(FilePath);
  106. is = f.OpenAsTextStream( 1, 0 );
  107. var Colours = [];
  108. keyRegex = /"Colour(.*?)"$/
  109. while( !is.AtEndOfStream ){
  110. var line = is.ReadLine();
  111. if (keyRegex.exec(line)) {
  112. var colar = line.split("=");
  113. Colours[colar[0].slice(1,-1)] = colar[1].slice(1,-1)
  114. }
  115. }
  116. is.Close();
  117. var WSHShell = WScript.CreateObject("WScript.Shell");
  118. var reg = new Registry(Registry.HKEY_CURRENT_USER)
  119. , rootKey = 'Software\\SimonTatham\\PuTTY\\Sessions'
  120. , keyRegex = /Sessions\\(.*?)\\Colour(.*?)$/
  121. reg.find(rootKey, function(path, value) {
  122. var keyMatch = path;
  123. if (keyRegex.exec(path)) {
  124. var m = keyMatch.split("\\");
  125. var hashkey = m[m.length-1];
  126. WSHShell.RegWrite("HKEY_CURRENT_USER\\"+path,Colours[hashkey],"REG_SZ");
  127. }
  128. return true;
  129. });
  130. WScript.Echo("Color setings from file "+FilePath+" were applied!");