Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

PrintEverything

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

I was sure there had to be a simpler way to enumerate subfolders, but even the MSDN reference (which took forever to find) does it that way. Why can't it just be an array?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.