Skip to content

Instantly share code, notes, and snippets.

@blech75
Created May 19, 2011 14:38
Show Gist options
  • Save blech75/980887 to your computer and use it in GitHub Desktop.
Save blech75/980887 to your computer and use it in GitHub Desktop.
batch screenshot script for Firefox and IE on Windows using WSH
// FIXME: use %PROGRAMFILES% to get at "c:\\Program\ Files"
// FIXME: put SAVE_DIR on the current user's desktop?
// FIXME: make it configurable via command-line options?
var BROWSERS = {
'ff' : {
'PATH' : "c:\\Program\ Files\\Mozilla\ Firefox\\",
'SAVE_DIR' : ".\\firefox\\"
},
'ie' : {
'PATH' : "",
'SAVE_DIR' : ".\\internet-explorer\\"
}
};
// the URL file is unnamed and required
if (WScript.Arguments.Unnamed.length < 1) {
WScript.Echo("Error: must specifiy input file.");
WScript.Quit();
}
var browsers_to_check = [];
// the browsers are named, and optional
// /b:ff,ie
if (WScript.Arguments.Named.length > 0) {
var b_opt = WScript.Arguments.Named.Item("b");
var req_browsers = b_opt.split(",");
for (var i=0; i < req_browsers.length; i++) {
if (typeof(BROWSERS[req_browsers[i]]) != 'undefined') {
browsers_to_check.push(req_browsers[i]);
}
}
} else {
// prime browsers_to_check w/ the available browsers
for (var b in BROWSERS) {
browsers_to_check.push(b);
}
}
WScript.Echo("Browsers to check: " + browsers_to_check.join(","));
var url_list_filename = WScript.Arguments(0);
var ForReading = 1, ForWriting = 2, ForAppending = 8;
var TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0;
var fsobj = new ActiveXObject("Scripting.FileSystemObject");
// FIXME: check if input file exists!
var file = fsobj.GetFile(url_list_filename);
var textstream = file.OpenAsTextStream(ForReading, TristateUseDefault);
var save_dir;
for (var i=0; i < browsers_to_check.length; i++) {
save_dir = BROWSERS[browsers_to_check[i]]['SAVE_DIR'];
// FIXME: if the target folders exist, do we drop the files into the folder or bail?
if (fsobj.FolderExists(save_dir)) {
WScript.Echo("Warning: " + save_dir + " already exists.");
} else {
fsobj.CreateFolder(save_dir);
}
}
function takeScreenshot(browser, url, filename) {
var cmd = "";
switch(browser) {
case 'ff':
cmd = BROWSERS[browser]['PATH'] + "firefox.exe -savepng " + url + " -saveas " + BROWSERS[browser]['SAVE_DIR'] + filename;
break;
case 'ie':
cmd = BROWSERS[browser]['PATH'] + "IECapt.exe --url=" + url + " --out=" + BROWSERS[browser]['SAVE_DIR'] + filename;
break;
}
WScript.Echo(" > " + browser);
var shell = new ActiveXObject("WScript.Shell");
exec = shell.Exec(cmd);
// hmph. so it looks like WshShell.Exec is asynchronous
while (exec.Status == 0) {
WScript.Sleep(100);
}
}
var status, url, path, file, screenshot_file, exec;
while (!textstream.AtEndOfStream) {
url = textstream.ReadLine();
// FIXME: validate URL before attempting to use it
// FIXME: handle URLs that have 1) no filenames 2) query strings, 3) etc.
path = url.split("/");
file = path[path.length-1];
screenshot_file = file + ".png";
WScript.Echo(url + " >> " + screenshot_file);
for (var i=0; i< browsers_to_check.length; i++) {
takeScreenshot(browsers_to_check[i], url, screenshot_file);
}
}
textstream.Close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment