// Lister Title Clock // (c) 2016 jsys // This is a script for Directory Opus. // See http://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information. // Called by Directory Opus to initialize the script function OnInit(initData) { initData.name = "Lister Title Clock"; initData.version = "1.0"; initData.copyright = "(c) 2016 jsys"; initData.desc = "Displays realtime clock in the Listers' titlebars."; initData.default_enable = true; initData.min_version = "11.10.2"; // not verified in v11, verified in v12.0, should work in v11 so I didn't want to limit it artificially // Settings & defaults initData.config_desc = DOpus.Create.Map(); var option_name = ""; option_name = "Lister title format"; initData.Config[option_name] = initData.Config[option_name] = "%T | %REALTIME"; initData.config_desc(option_name) = "%REALTIME is additional token used by this Add-In. See the SET LISTERTITLE in Opus docs for the list of tokens you can combine with %REALTIME."; option_name = "24-hour time"; initData.Config[option_name] = initData.Config[option_name] = true; initData.config_desc(option_name) = "Set this to False to have the time displayed in the 12-hour AM/PM style."; option_name = "Prepare for removal"; initData.Config[option_name] = initData.Config[option_name] = false; initData.config_desc(option_name) = "Resets the title changes done by this Add-In. Set this to True and restart the Opus before removal of this Add-In."; } function OnStartup(StartupData) { while (refreshListerTitle()) { // keep going } } // ------------- function OnShutdown(ShutdownData) { runDopusCommand('Set LISTERTITLE ""'); } function OnScriptConfigChange(ConfigChangeData) { // I couldn't find how to get the script's name (not filename) -- it's not Script.name or some such logical thing -- so I've hardcoded it here in the messagebox which I am so unhappy about that I had to write this comment :( MsgBox("Lister Title Clock", "You need to restart the Directory Opus before config changes to the \"Lister Title Clock Add-In\" can take effect.\n\nTo properly restart the Directory Opus, please go to the File menu and select \"Exit Directory Opus\".") } // ------------- function MsgBox(title, message) { dlg = DOpus.Dlg; dlg.title = title; dlg.message = message; dlg.icon = "information"; dlg.buttons = "OK"; return dlg.Show(); } function zfill(num, size) // http://stackoverflow.com/a/2998822 { var s = num+""; while (s.length < size) s = "0" + s; return s; } function formatHoursAMPM(hours) // adapted from http://stackoverflow.com/a/8888498 { var ampm = hours >= 12 ? 'pm' : 'am'; hours = hours % 12; hours = hours ? hours : 12; // the hour '0' should be '1' return [hours, ampm]; } function getCurrentTime() { var dt = new Date(); var hours = dt.getHours(); // 24 hours var minutes = dt.getMinutes(); var seconds = dt.getSeconds(); var suffix = ""; if (Script.Config["24-hour time"] == false) { var hours_suffix = formatHoursAMPM(hours); hours = hours_suffix[0]; suffix = " " + hours_suffix[1]; } else { hours = zfill(hours, 2); } return hours + ":" + zfill(minutes, 2) + ":" + zfill(seconds, 2) + suffix; } function runDopusCommand(cmd) { var dopusCommand = DOpus.NewCommand; dopusCommand.RunCommand(cmd); } function refreshListerTitle() { if (Script.Config["Prepare for removal"]) { DOpus.Output("Prepare for removal"); runDopusCommand('Set LISTERTITLE ""'); return false; } else { var new_title = Script.Config["Lister title format"]; new_title = new_title.replace("%REALTIME", getCurrentTime()); runDopusCommand('Set LISTERTITLE "notoggle:' + new_title + '"'); DOpus.Delay(500); // 500 ms instead of 1000 ms: prevents display desync with RTC which would sometimes look like we missed one second (e.g., jump from 09 to 11 seconds) return true; } }