Guest User

Untitled

a guest
Nov 20th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. <job id="main">
  2. <runtime>
  3. <description>
  4. Print everything in a folder (and its subfolders, recursively)
  5. to the default printer. (c) 2011 mstephenson2, please share
  6. with friends, bsd license
  7. </description>
  8. </runtime>
  9.  
  10. <resource id="WelcomeText">PrintEverything - easy way to print all files in a folder and its subfolders</resource>
  11.  
  12. <script language="JScript">
  13. // return an array of all interesting files (as Strings) in the Folder argument,
  14. // and look through subfolders too. to be called by 'the client'
  15. function getAllFiles(Folder) {
  16. var ToReturn = new ActiveXObject("Scripting.Dictionary"); // seems to be the easiest collection to work with under WSH's JS
  17. __getAllFiles(Folder, ToReturn);
  18. return (new VBArray((ToReturn).Keys())).toArray(); // nasty, but now it's a vanilla JS array. thanks msdn!
  19. }
  20.  
  21. // recursive, does all the work, kicked off from the 'public' getAllFiles()
  22. function __getAllFiles(Folder, ToAdd) {
  23. var fileEnum = new Enumerator(Folder.Files)
  24. for (fileEnum.moveFirst(); !fileEnum.atEnd(); fileEnum.moveNext()) {
  25. var currFile = fileEnum.item();
  26. var currPath = currFile.Path.toString();
  27. var currType = currFile.Type.toString();
  28. var fileIsScript = (currType.indexOf("Script") != -1);
  29. var fileIsHidden = (currFile.attributes & 2);
  30. var fileIsSpotlightStore = (currPath.indexOf("Spotlight-V100") != -1); // had a thumbdrive going between osx and win
  31. if (!(fileIsScript || fileIsHidden || fileIsSpotlightStore)) {
  32. ToAdd.add(currPath, ""); // no values needed, we'll look for only keys later
  33. }
  34. }
  35.  
  36. var subFolderEnum = new Enumerator(Folder.SubFolders);
  37. for (subFolderEnum.moveFirst(); !subFolderEnum.atEnd(); subFolderEnum.moveNext()) {
  38. var currSubFolder = subFolderEnum.item();
  39. __getAllFiles(currSubFolder, ToAdd);
  40. }
  41. }
  42. </script>
  43.  
  44. <script language="JScript">
  45. // execution begins here
  46. var fso = WScript.CreateObject("Scripting.FileSystemObject");
  47. var wshShell = WScript.CreateObject("WScript.Shell");
  48. var rootFolder = fso.GetFolder(wshShell.CurrentDirectory);
  49.  
  50. wshShell.Popup(getResource("WelcomeText"), 0, "PrintEverything", 0);
  51.  
  52. var allFiles = getAllFiles(rootFolder);
  53.  
  54. var dirStatsMsg = "We are in the folder:\n" + rootFolder.Path + "\n\n";
  55. dirStatsMsg += "In total, we found " + allFiles.length + " files to send to the default printer\n\n";
  56. dirStatsMsg += "Do you want to send them to the default printer now?\n";
  57. var responseInt = wshShell.Popup(dirStatsMsg, 0, "PrintEverything", 4); // a yes/no box that waits for user response
  58.  
  59. if (responseInt == 7) { // no
  60. wshShell.Popup("We didn't send anything to the default printer. Exiting.", 0, "PrintEverything", 0);
  61. WScript.Quit();
  62. }
  63.  
  64. var appShell = WScript.CreateObject("Shell.Application");
  65.  
  66. var printsSuccess = 0;
  67. var printsFail = 0;
  68. for (var i = 0; i < allFiles.length; i++ ) {
  69. var currFilePath = allFiles[i];
  70. var currFileDir = currFilePath.substring(0, currFilePath.lastIndexOf("\\"));
  71. var currFileName = currFilePath.substring(currFilePath.lastIndexOf("\\") + 1, currFilePath.length);
  72. var objFolder = appShell.NameSpace(currFileDir);
  73. var objItem = objFolder.ParseName(currFileName);
  74. var verbCollection = objItem.Verbs();
  75. //verbCollection.item(1).DoIt(); // not sure if we can count on &Print always being at this spot
  76.  
  77. var invokedPrintVerb = false;
  78. var verbEnum = new Enumerator(verbCollection);
  79. for (verbEnum.moveFirst(); !verbEnum.atEnd(); verbEnum.moveNext()) {
  80. var currVerb = verbEnum.item();
  81. var verbName = new String(currVerb.Name);
  82. if (verbName == "&Print") {
  83. currVerb.DoIt(); // best method name ever
  84. invokedPrintVerb = true;
  85. printsSuccess++;
  86. break;
  87. }
  88. }
  89.  
  90. if (!invokedPrintVerb) {
  91. wshShell.Popup("Couldn't figure out how to print " + currFilePath, 0, "PrintEverything", 0);
  92. printsFail++;
  93. }
  94. }
  95.  
  96. var finalMsg = "Done! Sent " + printsSuccess + " to the default printer. ";
  97. if (printsFail > 0) {
  98. finalMsg += "Failed to send " + printsFail + ". ";
  99. }
  100. finalMsg += "Exiting.";
  101.  
  102. wshShell.Popup(finalMsg, 0, "PrintEverything", 0);
  103. </script>
  104. </job>
Add Comment
Please, Sign In to add comment