Created
September 17, 2014 04:48
-
-
Save justsh/90fedd5c9b00a85d552f to your computer and use it in GitHub Desktop.
Helper script for passing selected files in Windows Explorer to WScript via StExBar. Processes with external programs such as jpegtran and pngout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* optimg.js | |
* This file runs optimg optimization on all selected files | |
* | |
* To use with StExBar, add the command line: | |
* WScript.exe "path/to/optimg.js" //E:javascript %sel*paths | |
*/ | |
(function (WScript, Error, String) { | |
"use strict"; | |
var argv, argc, errno = {}; | |
errno.EEXIST = 58; | |
argv = WScript.Arguments; | |
argc = argv.length; | |
if (argc < 1) { | |
WScript.Echo("Usage: [CScript | WScript] optimg.js path/to/file(s)"); | |
WScript.Quit(1); | |
} | |
// Functions | |
var basename = function (path) { | |
return path.replace(/\\/g, '/').replace(/.*\//, ''); | |
}; | |
var dirname = function (path) { | |
return path.replace(/\\/g, '/').replace(/\/[^\/]*\/?$/, ''); | |
}; | |
var noext = function (path) { | |
return path.substr(0, path.lastIndexOf('.')) || input; | |
}; | |
var getext = function (path) { | |
return path.split('.').pop(); | |
}; | |
// Ensure support for an "endswith" function to the String prototype | |
if (typeof String.prototype.endswith !== 'function') { | |
String.prototype.endswith = function (suffix) { | |
return this.indexOf(suffix, this.length - suffix.length) !== -1; | |
}; | |
} | |
var optimg = function (obj) { | |
obj = obj || {}; | |
if (typeof obj.imagePaths === "undefined") { | |
throw new Error("optimg requires a path to a directory of images"); | |
} | |
if (typeof obj.outDirectory === "undefined") { obj.outDirectory = "opt"; } | |
if (typeof obj.delimiter === "undefined") { obj.delimiter = " "; } | |
if (typeof obj.suffix === "undefined") { obj.suffix = ""; } | |
if (typeof obj.jpgBinary === "undefined") { obj.jpgBinary = "jpegtran"; } | |
if (typeof obj.pngBinary === "undefined") { obj.pngBinary = "pngout"; } | |
if (typeof obj.jpegtranArgs === "undefined") { | |
obj.jpegtranArgs = "-copy none -optimize -progressive"; | |
} | |
if (typeof obj.pngoutArgs === "undefined") { | |
obj.pngoutArgs = ""; | |
} | |
var fso, shell, files, jpgCommand, pngCommand, i, f, d, b, optname, | |
tmpfile, tmpstream, runstrings; | |
runstrings = []; | |
// remove the quotes | |
fso = WScript.CreateObject("Scripting.FileSystemObject"); | |
shell = WScript.CreateObject("WScript.Shell"); | |
files = obj.imagePaths.replace(/\"(.*)\"/, "$1").split(obj.delimiter); | |
if (obj.jpgBinary === "jpegtran") { | |
jpgCommand = obj.jpgBinary + " " + obj.jpegtranArgs; | |
} | |
if (obj.pngBinary === "pngout") { | |
pngCommand = obj.pngBinary + " " + obj.pngoutArgs; | |
} | |
for (i = 0; i < files.length; i += 1) { | |
f = files[i]; | |
if (f.toLowerCase().endswith(".jpg")) { | |
d = dirname(f) + '/' + obj.outDirectory + '/'; | |
try { | |
fso.CreateFolder(d); | |
} catch (e) { | |
/*jslint bitwise: false */ | |
if (e.number & 0xFFFF !== errno.EEXIST) { throw e; } | |
/*jslint bitwise: true */ | |
} | |
b = basename(f); | |
optname = d + noext(b) + obj.suffix + "." + getext(b); | |
runstrings.push(jpgCommand + " \"" + f + "\" \"" + optname + "\""); | |
} else if (f.toLowerCase().endswith(".png")) { | |
d = dirname(f) + '/' + obj.outDirectory + '/'; | |
try { | |
fso.CreateFolder(d); | |
} catch (e) { | |
/*jslint bitwise: false */ | |
if (e.number & 0xFFFF !== errno.EEXIST) { throw e; } | |
/*jslint bitwise: true */ | |
} | |
b = basename(f); | |
optname = d + noext(b) + obj.suffix + "." + getext(b); | |
runstrings.push(pngCommand + " \"" + f + "\" \"" + optname + "\""); | |
} | |
} | |
// WARN: Use a temporary file or you will hit the max char limit for cmd /c | |
tmpfile = shell.ExpandEnvironmentStrings("%TEMP%") + "/optimg.bat"; | |
tmpstream = fso.CreateTextFile(tmpfile, true); | |
tmpstream.Write(runstrings.join("\n")); | |
tmpstream.Close(); | |
// | |
// Use cmd /c so that you can interpret multiple commands or bat files | |
// without having to spawn _n_ number of shells for _n_ commands | |
// | |
// NOTE: (7) Displays the window as a minimized window. | |
// The active window remains active. | |
shell.Run("cmd /c " + tmpfile, 7, true); | |
shell = null; | |
// TODO: verify that files were actually optimized | |
WScript.Echo("Attempted to optimize " + runstrings.length + " of " + | |
files.length + " files"); | |
}; | |
optimg({ "imagePaths": argv(0), "delimiter": "*" }); | |
}(WScript)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment