Skip to content

Instantly share code, notes, and snippets.

@romgrk
Created April 6, 2017 19:30
Show Gist options
  • Select an option

  • Save romgrk/34313861febe58610031c9dd9750d301 to your computer and use it in GitHub Desktop.

Select an option

Save romgrk/34313861febe58610031c9dd9750d301 to your computer and use it in GitHub Desktop.
/*
* Check if there are files to process.
*/
var files = listFiles(get('global.in'))
var config = readJSON(join(get('global.work'), 'config.json'))
if (files.length !== 0 && config != undefined) {
set('outputPath', config.outputPath)
set('currentYear', config.currentYear)
set('printer', config.printer)
Script.ReturnValue = 0 // Continue
} else {
Script.ReturnValue = 1 // Stop execution
}
/*
* File utilities
*/
function readJSON(path) {
try {
return JSON.parse(readFile(path))
} catch(e) { }
}
function readFile(path){
var fs = new ActiveXObject("Scripting.FileSystemObject");
var file = fs.OpenTextFile(path, 1, true);
var res = file.ReadAll();
file.Close();
return res;
}
function listFiles(path) {
var fs = new ActiveXObject("Scripting.FileSystemObject");
var fc = new Enumerator(fs.GetFolder(path).files);
var list = [];
for (; !fc.atEnd(); fc.moveNext()) {
list.push(fc.item());
}
return list;
}
function join() {
'use strict';
return Array.prototype.slice.call(arguments, 0).join('\\')
}
/*
* Utilities
*/
function get(name) { return Watch.getVariable(name); }
function set(name, value) { Watch.setVariable(name, value); }
function log(msg) { try { Watch.log(toString(msg), 2) } catch(e) { WScript.stdout.WriteLine(toString(msg)) } }
function err(msg) { try { Watch.log(toString(msg), 1) } catch(e) { WScript.stdout.WriteLine(toString(msg)) } }
function exp(string) { return Watch.expandString(string); }
function xml(string) { return Watch.expandString("xmlget('/request[1]/values[1]/" + string + "[1]',Value,KeepCase,No Trim)"); }
function toString(value) { try { return JSON.stringify(value) } catch(e) { return ''+value } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment