One of the standout features of Directory Opus is its capability to manage multiple listers and a multitude of open tabs. However, this flexibility can quickly lead to a cluttered workspace if redundant tabs aren’t regularly closed.
The script helps simplify tab management by automatically identifying and closing duplicate tabs, ensuring that only unique and necessary tabs remain open. This allows users to maintain a cleaner, more efficient workspace and reduces the time spent manually sorting through tabs.
For power users and anyone looking to eliminate the clutter, this script is a game-changer, saving time and improving the overall experience with Directory Opus.
The Script
The following JavaScript code clears duplicates among the tabs, while ensuring that source, destination, and locked tabs are not closed. Here’s an overview of the script:
The main function triggers on click, prepares the command, and prioritizes tabs based on whether they’re currently selected, locked, or both.
Duplicates are flagged and then closed from right to left.
Selected and locked tabs are preserved.
Temporary flags are used to identify and manage duplicates.
To make adding this command to your Directory Opus installation simpler, the above script has been wrapped into a handy “Close Duplicate Tabs” Directory Command File (DCF), which includes a label, tooltip, and icon. You can double-click the file to execute it directly, or customize the Directory Opus toolbar and drop it there for easier access.
You can download the ready-to-use file, or create it yourself using the following XML code. Just be sure to save the new file with the .dcf file extension so that it’s properly handled by Directory Opus."
<?xml version="1.0"?><buttonbackcol="none"display="icon"label_pos="right"textcol="none"><label>Close Duplicate Tabs</label><tip>Close duplicate tabs in the active lister</tip><icon1>#closetab</icon1><functiontype="script"><instruction>@script JScript</instruction><instruction>function OnClick(clickData) {</instruction><instruction> DOpus.ClearOutput();</instruction><instruction> var cmd = clickData.func.command;</instruction><instruction> cmd.deselect = false;</instruction><instruction/><instruction> var lister = DOpus.listers.lastactive;</instruction><instruction> var sortedTabs = FilterAndSort(lister.tabs);</instruction><instruction/><instruction> CloseDuplicateTabs(cmd, sortedTabs);</instruction><instruction>}</instruction><instruction/><instruction>function FilterAndSort(collection) {</instruction><instruction> var filteredArray = [];</instruction><instruction/><instruction> // Convert collection to array</instruction><instruction> for (var i = 0; i < collection.count; i++) {</instruction><instruction> filteredArray.push(collection[i]);</instruction><instruction> }</instruction><instruction/><instruction> // Sort the array</instruction><instruction> filteredArray.sort(function(a, b) {</instruction><instruction> // First criteria: source or dest</instruction><instruction> if ((a.source && !b.source) || (a.dest && !b.dest)) return -1;</instruction><instruction> if ((!a.source && b.source) || (!a.dest && b.dest)) return 1;</instruction><instruction/><instruction> // Second criteria: locked</instruction><instruction> if (a.lock !== "off"&& b.lock === "off") return -1;</instruction><instruction> if (a.lock === "off"&& b.lock !== "off") return 1;</instruction><instruction/><instruction> // Default order if all criteria are equal</instruction><instruction> return 0;</instruction><instruction> });</instruction><instruction/><instruction> return filteredArray;</instruction><instruction>}</instruction><instruction/><instruction>function CloseDuplicateTabs(cmd, tabs) {</instruction><instruction> // A temporary "isDuplicate" flag is added to each tab during the</instruction><instruction> // removal process</instruction><instruction> CreateDuplicateFlag(tabs);</instruction><instruction/><instruction> // Identify the duplicate tabs, while always retaining source, dest, and</instruction><instruction> // locked tabs</instruction><instruction> for (var i = 0; i < tabs.length; i++) {</instruction><instruction> for (var j = tabs.length - 1; j > i; j--) {</instruction><instruction> var a = tabs[i];</instruction><instruction> var b = tabs[j];</instruction><instruction> if (!b.source && !b.dest && b.lock === "off"&& a.path.def_value === b.path.def_value) {</instruction><instruction> tabs[j].vars.Set("isDuplicate", true);</instruction><instruction> }</instruction><instruction> }</instruction><instruction> }</instruction><instruction/><instruction> // Close the duplicate tabs from the end of the collection first. Since</instruction><instruction> // the full tab list includes all left tabs followed by all right tabs,</instruction><instruction> // right tab duplicates will be removed first.</instruction><instruction> for (var i = tabs.length - 1; i >= 0; i--) {</instruction><instruction> var tab = tabs[i];</instruction><instruction> if (tab.vars.Get("isDuplicate") === true) {</instruction><instruction> cmd.RunCommand("Go TABCLOSE=" + tab);</instruction><instruction> }</instruction><instruction> }</instruction><instruction/><instruction> // Clear the temporary "isDuplicate" flag from the tabs</instruction><instruction> DeleteDuplicateFlag(tabs);</instruction><instruction>}</instruction><instruction/><instruction>function CreateDuplicateFlag(tabs) {</instruction><instruction> for (var i = 0; i < tabs.length; i++) {</instruction><instruction> tabs[i].vars.Set("isDuplicate", false);</instruction><instruction> }</instruction><instruction>}</instruction><instruction/><instruction>function DeleteDuplicateFlag(tabs) {</instruction><instruction> for (var i = 0; i < tabs.length; i++) {</instruction><instruction> tabs[i].vars.Delete("isDuplicate");</instruction><instruction> }</instruction><instruction>}</instruction></function></button>
By using this script, you can quickly close duplicate tabs and eliminate unnecessary clutter from your Directory Opus open tab list.