// The OnInit function is called by Directory Opus to initialize the script add-in function OnInit(initData) { // Provide basic information about the script by initializing the properties of the ScriptInitData object initData.name = "Regex Columns with Groups"; initData.desc = "Adds columns and displays values matched by regexp against filename."; initData.copyright = "2014 wowbagger"; initData.min_version = "11.5.1" initData.version = "1.2"; initData.default_enable = true; // Create a new ScriptColumn object and initialize it to add the column to Opus for(var i = 0; i < config.columns.length; ++i) { var cmd = initData.AddColumn(); cmd.name = config.columns[i].name; cmd.method = "OnCustomText"; cmd.label = config.columns[i].name; cmd.autogroup = true; cmd.autorefresh = true; cmd.justify = "left"; } } //Define columns here, includes title and regexp. var config = { //Configuration for different patterns columns: [{ //Name of column, Title. name:"area", //input values itemProperty: "name_stem", //regexp to extract value re: /^\d+/, // group to be returned (optional if whole match, i.e. group 0) // group: 0 }, { name:"phone", itemProperty: "name_stem", re: /^\d+\D+(\d+[- ]\d+)/, group: 1 }, { name:"who", itemProperty: "name_stem", re: /^\d+\D+\d+[- ]\d+\s*(\S+)/, group: 1 }] } // Implement the OnCustomText column (this entry point is an OnScriptColumn event). function OnCustomText(scriptColData) { // get column's config var config = GetConfig(scriptColData.col); if(!config) return; var result; if(!config.itemProperty) { // Get first regexp match on items default property. result = config.re.exec(scriptColData.item); } else { // Get first regexp match on items defined property . result = config.re.exec(scriptColData.item[config.itemProperty]); } if (result != null) { //display value var capturegroup = (typeof config.group === 'undefined') ? 0 : config.group; scriptColData.value = result[capturegroup]; } } function GetConfig(key) { var match = null; for(var i = 0; i < config.columns.length; ++i) { if(config.columns[i].name == key) { match = config.columns[i]; break; } } return match; }