// CustomtextColumn // By wowbagger // -------------------- Options var LogLevel = { Debug: 0, Info: 1, Warning: 2, Error: 3 }; var gbUseLogging=false; var logLevel = LogLevel.Info; var scriptName = "RegExp Columns"; //Default values for extendedConfig var defaultCustomColumns = { columns: [{ //Column settings // Columns setting details https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/ScriptColumn.htm name:"RegExp1", label:"RegExp1", header: "", type: "string", defwidth: "", //int or string. //grouporder:, //"Never Modified;Modified" justify: "left", //"left", "right", "center" or "path". The default is "left". //Not implemented yet infotiponly: false, //Not available for column //Not implemented yet maxstars: 5, // int datetimeformat: "yyyy-mm-dd", //Date Format used to parse the extracted date information (time not realy supported at the moment!) //Not implemented yet nogroup: false, grouporder: "", //enableAutoGroup: true,// true (default) //Not implemented yet sorting:"normal", //enableSort: false,//flag (true/false) as described in the DOpus documentation //defsort: true, //0 default, -1 reverse sort. //Regexp settings regexp: "(.*)", // Will select everyting inputItemProperty: "name_stem", // name_stem, realpath, name, ext, contents firstValid: false, //(true/false) output: "$1",// $1$2 //Not implemented yet filter: "all" // all, files, folders }] }; // Functions var doVars = DOpus.vars; var doLogCmd = DOpus.NewCommand; var configVarKey = "RegexpCustomColumnsConfig"; function OnInit(initData) { // Provide basic information about the script by initializing the properties of the ScriptInitData object initData.name = scriptName; initData.desc = "Adds columns and displays values matched by regexp against filename."; initData.copyright = "2017 wowbagger"; initData.min_version = "12.0" initData.version = "1.5.7"; initData.default_enable = true; // variable for enabling debug, so as to enable debug via a button initData.config_desc = DOpus.NewMap(); initData.config.DEBUG = false; initData.config_desc("DEBUG") = "Set DEBUG flag to true in order to enable logging messages to the Opus Output Window"; } function OnAddCommands(addCmdData) { var cmd = addCmdData.AddCommand(); cmd.name = "RegExpColumnsConfigure"; cmd.method = "OnRegExpColumnsConfigure"; cmd.desc = "Configure the RegExp columns"; cmd.label = "RegExpColumnsConfigure"; } function OnAddColumns(addColData) { //LogDebugMessage("OnAddColumns called", true) var customColumns = LoadCustomColumns(); if(customColumns && customColumns.columns.length > 0) { //var customColumns = JSON.parse(Script.config.CustomColumns); for(i=0;i 0) { var selectedColumn = columnsList.GetItemAt((index > 0) ? index -1 : 0); selectedColumn.selected = true; } } function AddToColumnsList(dlg, name, setSelected) { var columnsList = dlg.Control("columnsList"); var location = columnsList.AddItem(name); if(setSelected) { var item = columnsList.GetItemAt(location); item.selected = true; } } function GetSelectedItem(dlg, controlName) { //LogDebugMessage("GetSelectedItem: " + controlName); var listItems = dlg.Control(controlName); for(var i = 0; i < listItems.count; ++i) { var column = listItems.GetItemAt(i); //LogDebugMessage("GetSelectedItem: " + controlName + " " + i + " of " + listItems.count + " is" + (column.selected ? "selected" : "not selected")); if(column && column.selected) { //LogDebugMessage("GetSelectedItem: found " + column.name); return column; } } } function SetSelectedItem(dlg, controlName, value) { var listItems = dlg.Control(controlName); if(typeof(value) == "undefined") { var item = listItems.GetItemAt(0); //LogDebugMessage("SetSelectedItem: " + controlName + ":defaulted to " + item.name); item.selected = true; return; } for(var i = 0; i < listItems.count; ++i) { var item = listItems.GetItemAt(i); //LogDebugMessage("SetSelectedItem: " + controlName + " " + i + " of " + listItems.count + " is " + item.name + " looking for " + value); if(item && item.name == value) { //LogDebugMessage("SetSelectedItem: " + controlName + ":found " + item.name); item.selected = true; } } } function CopyToClipboard(){ var customColumns = LoadCustomColumns(); var config = JSON.stringify(customColumns, undefined, 2).replace(/(\n)+/g,"\r\n"); var doCmd = DOpus.NewCommand; var cmd = "Clipboard SET " + config; doCmd.RunCommand(cmd); } function SaveCustomColumns(customColumns){ var newList = new Array(); for(var i = 0; i < customColumns.columns.length; ++i) { if(customColumns.columns[i]) { newList.push(customColumns.columns[i]); } } customColumns.columns = newList; var config = JSON.stringify(customColumns, undefined, 2).replace(/(\n)+/g,"\r\n"); LogDebugMessage("SaveCustomColumns: " + config); doVars(configVarKey) = config; doVars(configVarKey).persist = true; Script.InitColumns; } function ClearCustomColumns(){ if(doVars.Exists(configVarKey)) { doVars(configVarKey).Delete() } } function LoadCustomColumns(){ //LogDebugMessage("LoadCustomColumnsConfiguration from var"); //return JSON.parse(Script.config.CustomColumns); var config = doVars.Exists(configVarKey) ? JSON.parse(doVars.Get(configVarKey)) : null; if(!config) { config = defaultCustomColumns; } return config } function GetRegexpColumnConfig(name) { var customColumns = LoadCustomColumns(); for(var i = 0; i < customColumns.columns.length; ++i) { if(customColumns.columns[i].name.toLowerCase() == name.toLowerCase()) { return customColumns.columns[i]; } } } function IsDebugEnabled() { if(Script) { return gbUseLogging || Script.config.DEBUG || doLogCmd.IsSet(Script.config.DebugEnableVar); } return gbUseLogging; } function LogMessage(message, force) { if (force || IsDebugEnabled()) { DOpus.OutputString(message)}; } function LogWarningMessage(message, force) { if (LogLevel.Warning >= logLevel) { LogMessage(message, force) } } function LogInfoMessage(message, force) { if (LogLevel.Info >= logLevel) { LogMessage(message, force) } } function LogDebugMessage(message, force) { if (LogLevel.Debug >= logLevel) { LogMessage(message, force) } } // Implement the OnCustomText column (this entry point is an OnScriptColumn event). function OnCustomText(scriptColData) { var itemPath = scriptColData.item.path + "\\" + scriptColData.item.name; LogDebugMessage("OnCustomText: column=" + scriptColData.col + " columnsCount=" + scriptColData.columns.length + " itempPath=" + itemPath); // get column's config var customColumnsConfig = LoadCustomColumns(); var columnsConfig = customColumnsConfig.columns; if (!InArray(scriptColData.col, columnsConfig)) return; LogDebugMessage("1: column=" + scriptColData.col + " columnsCount=" + scriptColData.columns.length + " itempPath=" + scriptColData.Item.path); var results = RunRegExpOnItem(scriptColData.Item, columnsConfig); if (results){ for ( i = 0; i < columnsConfig.length; i++) { if (typeof(columnsConfig[i].name) != "undefined") { var result = results[columnsConfig[i].name]; if (scriptColData.columns.exists(columnsConfig[i].name)) { switch (columnsConfig[i].type) { case "date" : case "time" : case "datetime" : LogDebugMessage("Datetime for " + columnsConfig[i].name + "[" + columnsConfig[i].type + "]:" + result); var parsedDate = parseDate(results[columnsConfig[i].name], columnsConfig[i].datetimeformat); scriptColData.columns(columnsConfig[i].name).value = parsedDate ? parsedDate.getVarDate() : null; break; case "graph" : case "igraph" : case "percent" : case "stars" : LogDebugMessage("number for " + columnsConfig[i].name + "[" + columnsConfig[i].type +"]:" + result); scriptColData.columns(columnsConfig[i].name).value = (isNaN(result) ? 0 : result); break; default : LogDebugMessage("results for " + columnsConfig[i].name + "[" + columnsConfig[i].type +"]:" + result); scriptColData.columns(columnsConfig[i].name).value = result; break; } } else { LogDebugMessage("column not loaded " +columnsConfig[i].name + "["+columnsConfig[i].type +"]:" + result); } } } } } function RunRegExpOnItem(item, columnConfigurations) { LogDebugMessage("RunRegExpOnItem item=" + item.path + "\\" + item.name); var result = {}; if(!item) return; for ( colIndex = 0; colIndex < columnConfigurations.length; colIndex++) { var currentCol = columnConfigurations[colIndex]; var fileContents; var subject; var regexMatch; switch (currentCol.inputItemProperty) { case "contents" : if(!(fileContents)) { var fso = new ActiveXObject("Scripting.FileSystemObject"); var fileStream = fso.openTextFile(item.path + "\\" + item.name); fileContents = fileStream.readAll(); fileStream.Close(); } subject = fileContents; break; case "ext" : subject = item.ext; break; case "name" : subject = item.name; break; case "realpath" : subject = item.realpath; break; case "" : case "name_stem" : subject = item.name_stem; break; default: subject = findprop(item, currentCol.inputItemProperty); break; } LogDebugMessage("RunRegExpOnItem colIndex:" + colIndex + " subject=" + subject); var re = new RegExp(currentCol.regexp); if(re.test(subject)) { if(currentCol.firstValid) { regexMatch = re.exec(subject); if (regexMatch != null) { for(i = 1; i < regexMatch.length + 1; i++) { if (regexMatch[i] != '') { result[currentCol.name] = regexMatch[i]; break; } } } } else { var output = (currentCol.output == "") ? "$1" : currentCol.output; result[currentCol.name]= subject.replace(re, output); } } } return result; } function findprop(obj, path) { // thanks to andersonnnunes LogDebugMessage("findprop '" + path + "' on item=" + obj.path + "\\" + obj.name); var matched = false; var args = path.split('.'), i, l; for (i = 0, l = args.length; i < l; i++) { if (!obj[args[i]]){ break; matched = false; } obj = obj[args[i]]; matched = true; } if(!matched) return ""; try { return (typeof(obj) != "undefined") ? obj.toString() : ""; } catch(e) { return ""; } } function parseDate(input, format) { if(!input) return; var parsedDate = new Date(Date.parse(input)); if(!parsedDate || parsedDate == "NaN") { format = format || 'yyyy-mm-dd'; // default format var parts = input.match(/(\d+)/g), i = 0, fmt = {}; if(parts) { // extract date-part indexes from the format format.replace(/(yyyy|dd|mm)/g, function(part) { fmt[part] = i++; }); parsedDate = new Date(parts[fmt['yyyy']], parts[fmt['mm']]-1, parts[fmt['dd']]); } } return (!parsedDate || parsedDate == "NaN") ? null : parsedDate; } function InArray(col,arr){ for (var i=0;i