GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Sort Folder Tabs (on-demand, or when listers open)

Requires Directory Opus 12.6.2 or above.


Overview

The scripts below will sort the open folder tabs alphabetically, by their labels. (If you want to sort them in a different way, e.g. by full path, you could do that with minor alterations.)

Brief usage instructions are below. For more detailed instructions, see:
How to use buttons and scripts from this forum.

History

v1.0 (14-Sep-2017):

  • Initial version.

Button version (sort tabs on-demand)

If you want a button that you can click to sort your tabs on-demand, use this:

  • Download: Sort Tabs.dcf (3.1 KB)
  • Select Settings > Customize Toolbars.
  • Drag the .dcf file on to your toolbar, to where you want it.
  • Click OK in the Customize dialog.

Script Add-in version (sort tabs in new listers automatically)

If you want new listers (which are configured to open multiple tabs) to have their tabs sorted automatically, use this:

  • Download: Sort Tabs.js.txt (1.9 KB)
  • Select Settings > Preferences.
  • Go to Toolbars / Scripts in the tree on the left of the Preferences window.
  • Drag the .js.txt file in to the list of scripts.

Script code

If you just want to use this thing, see above for the downloads.

The script code is reproduced here to help people browsing the forum for scripting techniques.

The sortTabs function makes up the bulk of both versions and is identical between the two.

Button version:

function OnClick(clickData)
{
	var cmd = clickData.func.command;
	cmd.deselect = false; // Prevent automatic deselection
	cmd.ClearFiles();

	sortTabs(cmd, clickData.func.sourcetab.lister.tabsleft);
	sortTabs(cmd, clickData.func.sourcetab.lister.tabsright);
}

function sortTabs(cmd, tabList)
{
	var setLabels = DOpus.Create.StringSet();
	var mapLabelToTabVec = DOpus.Create.Map();

	for (var eTabs = new Enumerator(tabList); !eTabs.atEnd(); eTabs.moveNext())
	{
		var tab = eTabs.item();
		var label = tab.displayed_label.toLowerCase();

		setLabels.insert(label);

		if (!mapLabelToTabVec.exists(label))
		{
			mapLabelToTabVec(label) = DOpus.Create.Vector();
		}
		mapLabelToTabVec(label).push_back(tab);
	}

	var vecLabels = DOpus.Create.Vector();

	for (var eLabels = new Enumerator(setLabels); !eLabels.atEnd(); eLabels.moveNext())
	{
		vecLabels.push_back(eLabels.item());
	}
	vecLabels.sort();

	cmd.Clear();
	var tabPos = 0;

	for (var eLabels = new Enumerator(vecLabels); !eLabels.atEnd(); eLabels.moveNext())
	{
		var vecTabs = mapLabelToTabVec(eLabels.item());
		for (var eTabs = new Enumerator(vecTabs); !eTabs.atEnd(); eTabs.moveNext())
		{
			var tab = eTabs.item();
			cmd.AddLine("Go TABPOS=" + tabPos + "," + tab);
			++tabPos;
		}
	}

	if (tabPos > 1)
	{
		cmd.Run();
	}
}

Script Add-in version:

function OnInit(initData)
{
	initData.name = "SortTabs";
	initData.version = "1.0";
	initData.copyright = "(c) 2017 Leo Davidson";
	initData.url = "https://resource.dopus.com/t/sort-folder-tabs-on-demand-or-when-listers-open/26890";
	initData.desc = "When a new lister opens, alphabetically sort its folder tabs by label.";
	initData.default_enable = true;
	initData.min_version = "12.6.2";
}

// 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);
	sortTabs(cmd, openListerData.lister.tabsleft);
	sortTabs(cmd, openListerData.lister.tabsright);

	return false; // Don't call us again for this lister.
}

function sortTabs(cmd, tabList)
{
	var setLabels = DOpus.Create.StringSet();
	var mapLabelToTabVec = DOpus.Create.Map();

	for (var eTabs = new Enumerator(tabList); !eTabs.atEnd(); eTabs.moveNext())
	{
		var tab = eTabs.item();
		var label = tab.displayed_label.toLowerCase();

		setLabels.insert(label);

		if (!mapLabelToTabVec.exists(label))
		{
			mapLabelToTabVec(label) = DOpus.Create.Vector();
		}
		mapLabelToTabVec(label).push_back(tab);
	}

	var vecLabels = DOpus.Create.Vector();

	for (var eLabels = new Enumerator(setLabels); !eLabels.atEnd(); eLabels.moveNext())
	{
		vecLabels.push_back(eLabels.item());
	}
	vecLabels.sort();

	cmd.Clear();
	var tabPos = 0;

	for (var eLabels = new Enumerator(vecLabels); !eLabels.atEnd(); eLabels.moveNext())
	{
		var vecTabs = mapLabelToTabVec(eLabels.item());
		for (var eTabs = new Enumerator(vecTabs); !eTabs.atEnd(); eTabs.moveNext())
		{
			var tab = eTabs.item();
			cmd.AddLine("Go TABPOS=" + tabPos + "," + tab);
			++tabPos;
		}
	}

	if (tabPos > 1)
	{
		cmd.Run();
	}
}

Dear Leo,

Thank you so much for all the work done. Really sorry and grateful. I will be waiting for the new version and make sure to keep you posted about the outcome. Thanks again!

Patrick

1 Like