// StyleSetter // (c) 2017 wowbagger // This is a script for Directory Opus. // See http://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information. var doFsu = DOpus.FSUtil; var doVars = DOpus.vars; var doCmd = DOpus.NewCommand; var lastSetVar = "StyleSetterLastSet"; // Called by Directory Opus to initialize the script function OnInit(initData) { initData.name = "StyleSetter"; initData.version = "0.5"; initData.copyright = "(c) 2017 wowbagger"; // initData.url = "https://resource.dopus.com/viewforum.php?f=35"; initData.desc = "Auto Set Styles"; initData.default_enable = true; initData.min_version = "12.0"; initData.config_desc = DOpus.NewMap(); // variable for enabling debug, so as to enable debug via a button initData.config.DebugEnableVar = "$glob:debug" initData.config.DEBUG = false initData.config_desc("RequireEnabledVariable") = "Require the EnabledVariable be set for the script to run. If false the EnabledVariable is ignored."; initData.config.RequireEnabledVariable = false; initData.config_desc("EnabledVariableName") = "Variable used to enable StyleSetter,. Value set with a toolbar button."; initData.config.EnabledVariableName = "StyleSetterEnabled"; initData.config_desc("SettingsFileName") = "The name of the file that will have the settings in it."; initData.config.SettingsFileName = "folder.xml"; initData.config_desc("DefaultStyle") = "The name of the default Style."; initData.config.DefaultStyle = "StyleSetterDefault"; } function IsDebugEnabled() { return Script.config.DEBUG || doCmd.IsSet(Script.config.DebugEnableVar); } function LogMessage(message) { if (IsDebugEnabled()) { DOpus.OutputString(message) }; } // Called after a new folder is read in a tab function OnAfterFolderChange(afterFolderChangeData) { if (!Script.config.RequireEnabledVariable || afterFolderChangeData.tab.lister.vars.Exists(Script.config.EnabledVariableName)) { var configuredFileName = Script.config.SettingsFileName; var settingsFile = afterFolderChangeData.tab.path + "\\" + configuredFileName; LogMessage("OnAfterFolderChange: action:" + afterFolderChangeData.action + " file:" + settingsFile); if (afterFolderChangeData.result && (afterFolderChangeData.action == 'back' || afterFolderChangeData.action == 'forward' || afterFolderChangeData.action == 'dblclk' || afterFolderChangeData.action == 'parent')) { var selectedStye = ""; if (doFsu.Exists(settingsFile)) { var selectedStye = ReadStyleConfig(settingsFile); } if (typeof(selectedStye) == "undefined" || selectedStye == "") { if (Script.config.DefaultStyle != "") { selectedStye = Script.config.DefaultStyle; } } SetStyle(selectedStye, afterFolderChangeData.tab); } } } function SetStyle(styleName, tab) { if (typeof(styleName) != "undefined" && styleName != "") { var lastSet; if (tab.lister.vars.Exists(lastSetVar)) { lastSet = tab.lister.vars.Get(lastSetVar); } if (styleName != lastSet) { LogMessage("setting style :" + styleName); var styleCmd = DOpus.NewCommand; styleCmd.SetSourceTab(tab); styleCmd.RunCommand("Prefs STYLE \"" + styleName + "\""); tab.lister.vars.Set(lastSetVar, styleName); } } } function ReadStyleConfig(filePath) { var msxml = new ActiveXObject("MSXML2.DOMDocument.6.0"); LogMessage("ReadStyleConfig: file:" + filePath); var xpath = ".//*[local-name() = 'style']"; //Load and validate the specified file into the DOM. msxml.load(filePath); result = ""; if (msxml.parseError.errorCode != 0) { LogMessage("XML load error code: " + msxml.parseError.errorCode) } else { var node = msxml.documentElement.selectSingleNode(xpath); if (node) { result = node.text; } } return result; }