// 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; // Called by Directory Opus to initialize the script function OnInit(initData) { initData.name = "StyleSetter"; initData.version = "0.1"; 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 = true 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 = "Default"; } 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) { var configuredFileName = Script.config.SettingsFileName; var settingsFile = afterFolderChangeData.tab.path + "\\" + configuredFileName; LogMessage("OnAfterFolderChange: action:" + afterFolderChangeData.action + " file:" + settingsFile); var doCmd = DOpus.NewCommand; doCmd.SetSourceTab(afterFolderChangeData.tab); 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); } LogMessage("OnAfterFolderChange: selectedStye:" + selectedStye); if(typeof(selectedStye) == "undefined" || selectedStye == "") { LogMessage("resetting to default style"); if(Script.config.DefaultStyle == "") { doCmd.RunCommand('Prefs STYLE=^prev'); } else { doCmd.RunCommand("Prefs STYLE \"" + Script.config.DefaultStyle + "\""); } } else { LogMessage("setting style :" + selectedStye); doCmd.RunCommand("Prefs STYLE \"" + selectedStye + "\""); } } } 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; }