Sort Open File Tabs via a macro

This is a continuation of an old thread http://community.activestate.com/forum/sorting-tabs

First, toddw proposed this code:

// Move the current tab
var view = ko.views.manager.currentView;
var tabbox = view.parentNode;
while (tabbox && tabbox.nodeName != "tabbox" && tabbox.nodeName != "xul:tabbox") {
    tabbox = tabbox.parentNode;
}
var index = 1;
var tabToMove = tabbox.selectedTab;
var targetTab = tabbox._tabs.childNodes[index];
tabbox._tabs.insertBefore(tabToMove, targetTab);

Then r.izumita expanded on it with this:

var view = ko.views.manager.currentView;
var tabbox = view.parentNode;
while (tabbox && tabbox.nodeName != "tabbox" && tabbox.nodeName != "xul:tabbox") {
    tabbox = tabbox.parentNode;
}
var index = 1;
var childNodes = tabbox._tabs.childNodes
for (var i = 0;  i < childNodes.length; i++) {
    for (var j = childNodes.length - 1; j > i; j--) {
        if (childNodes[j].label < childNodes[j-1].label) {
            tabbox._tabs.insertBefore(childNodes[j], childNodes[j-1])
        }
    }
}

Then r.izumita added case-insesitive sorting

var view = ko.views.manager.currentView;
var tabbox = view.parentNode;
while (tabbox && tabbox.nodeName != "tabbox" && tabbox.nodeName != "xul:tabbox") {
    tabbox = tabbox.parentNode;
}
var index = 1;
var childNodes = tabbox._tabs.childNodes
for (var i = 0;  i < childNodes.length; i++) {
    for (var j = childNodes.length - 1; j > i; j--) {
        if (childNodes[j].label.toLowerCase() < childNodes[j-1].label.toLowerCase()) {
            tabbox._tabs.insertBefore(childNodes[j], childNodes[j-1])
        }
    }
}

Then nathan added the ability to group by extension:

var view = ko.views.manager.currentView;
var tabbox = view.parentNode;
while (tabbox && tabbox.nodeName != "tabbox" && tabbox.nodeName != "xul:tabbox") {
    tabbox = tabbox.parentNode;
}
var index = 1;
var childNodes = tabbox._tabs.childNodes
for (var i = 0;  i < childNodes.length; i++) {
    for (var j = childNodes.length - 1; j > i; j--) {
        if (childNodes[j].label.toLowerCase() < childNodes[j-1].label.toLowerCase()) {
            tabbox._tabs.insertBefore(childNodes[j], childNodes[j-1])
        }
    }
}
for (var i = 0;  i < childNodes.length; i++) {
    for (var j = childNodes.length - 1; j > i; j--) {
        sExt1 = childNodes[j].label.replace(/.*\./,'');
        sExt2 = childNodes[j-1].label.replace(/.*\./,'');
        if (sExt1.toLowerCase() < sExt2.toLowerCase()) {
            tabbox._tabs.insertBefore(childNodes[j], childNodes[j-1])
        }
    }
}

I was personally interested to make it sort by full path so I came up with this:

var view = ko.views.manager.currentView;
var tabbox = view.parentNode;
while ( tabbox && tabbox.nodeName != "tabbox" && tabbox.nodeName != "xul:tabbox" )
{
	tabbox = tabbox.parentNode;
}

var index = 1;
var childNodes = tabbox._tabs.childNodes;

for ( var i = 0;  i < childNodes.length; i++ )
{
	for ( var j = childNodes.length - 1; j > i; j-- )
	{
		if ( childNodes[ j ].tooltipText.toLowerCase() < childNodes[ j - 1 ].tooltipText.toLowerCase() )
		{
			tabbox._tabs.insertBefore( childNodes[ j ], childNodes[ j - 1 ] );
		}
	}
}

Also available for download here: http://komodoide.com/resources/macros/atmanactive--sortopentabsbyfullpath/

Now, the Open Files Panel has a fantastic sorting and grouping functions, and it would be wonderful if it could apply it’s current sorting to Open Files Tabs.

Given the previous macro examples, it looks like it could easily be crafted if only I knew how to access the Open Files Panel’s list and elements via JavaScript.

If anyone knows, any help is greatly appreciated.

You can look at the openfiles source code here:

You can access this via ko.openfiles

A good way to quickly test some code is to use the Javascript shell in the Komodo developer extension which gives you TAB autocompletion. So you can type ko.openfiles. and hit TAB twice to see what properties are available.