Overview
This script runs whenever a new lister (main window) opens, and will automatically close all tabs for folders which are already open in the same window.
For dual-display listers, a tab is only considered a duplicate if it is on the same side as another with the same path. (It is easy to change if you want de-duplication across both sides, however.)
Download:
- Download: DeDuplicateTabs.js.txt (1.6 KB)
- See How to use buttons and scripts from this forum - #2 by Leo for how to install the script.
History
v1.0 (31-Oct-2020):
- Initial version.
Script code
If you just want to use this thing, see above for the download.
The script code is reproduced here to help people browsing the forum for scripting techniques.
function OnInit(initData)
{
initData.name = "DeDuplicateTabs";
initData.version = "1.0";
initData.copyright = "(c) 2020 Leo Davidson";
initData.url = "https://resource.dopus.com/t/deduplicatetabs-auto-close-duplicate-tabs-when-new-windows-open/37050/1";
initData.desc = "Automatically closes duplicate folder tabs in new windows.";
initData.default_enable = true;
initData.min_version = "12.22";
}
// Called when a new Lister is opened
function OnOpenLister(openListerData)
{
if (!openListerData.after)
{
// The tabs aren't open yet. Ask to be called again when they are.
return true;
}
var cmd = DOpus.Create.Command();
cmd.SetSourceTab(openListerData.lister.activetab);
closeDuplicateTabs(cmd, openListerData.lister.tabsleft);
closeDuplicateTabs(cmd, openListerData.lister.tabsright);
return false; // Don't call us again for this lister.
}
function closeDuplicateTabs(cmd, tabList)
{
var mapPaths = DOpus.Create.Map();
var vecTabsToClose = DOpus.Create.Vector();
var tabIndex = 0;
for (var eTabs = new Enumerator(tabList); !eTabs.atEnd(); eTabs.moveNext())
{
var tab = eTabs.item();
var tabPath = tab.path;
if (mapPaths.exists(tabPath))
{
vecTabsToClose.push_back(tab);
if (tab.visible)
{
cmd.RunCommand("Go TABSELECT=" + mapPaths(tabPath));
}
}
else
{
mapPaths(tabPath) = tabIndex;
}
tabIndex = tabIndex + 1;
}
if (vecTabsToClose.empty)
return;
cmd.Clear();
for (var eTabs = new Enumerator(vecTabsToClose); !eTabs.atEnd(); eTabs.moveNext())
{
var tab = eTabs.item();
cmd.AddLine("Go TABCLOSE=" + tab);
}
cmd.Run();
}
On-Demand Version:
If you only want duplicate tabs to be cleaned up when you click a button or hotkey, not automatically, use this button:
- Download: Close Duplicate Tabs.dcf (2.7 KB)
- See How to use buttons and scripts from this forum
Script Code (on-demand version):
function OnClick(clickData)
{
var cmd = clickData.func.command;
cmd.deselect = false; // Prevent automatic deselection
cmd.ClearFiles();
var lister = clickData.func.sourcetab.lister;
closeDuplicateTabs(cmd, lister.tabsleft);
closeDuplicateTabs(cmd, lister.tabsright);
}
function closeDuplicateTabs(cmd, tabList)
{
var mapPaths = DOpus.Create.Map();
var vecTabsToClose = DOpus.Create.Vector();
var tabIndex = 0;
for (var eTabs = new Enumerator(tabList); !eTabs.atEnd(); eTabs.moveNext())
{
var tab = eTabs.item();
var tabPath = tab.path;
if (mapPaths.exists(tabPath))
{
vecTabsToClose.push_back(tab);
if (tab.visible)
{
cmd.RunCommand("Go TABSELECT=" + mapPaths(tabPath));
}
}
else
{
mapPaths(tabPath) = tabIndex;
}
tabIndex = tabIndex + 1;
}
if (vecTabsToClose.empty)
return;
cmd.Clear();
for (var eTabs = new Enumerator(vecTabsToClose); !eTabs.atEnd(); eTabs.moveNext())
{
var tab = eTabs.item();
cmd.AddLine("Go TABCLOSE=" + tab);
}
cmd.Run();
cmd.Clear();
}