Tab Color Based on Folder Content

What options exist to change tab color based on folder content? For example, how could a user set a tab to a specific color if the folder contains a particular file extension or defined group of file extensions?

Folder formats can apply tab colors, so you could use the content type formats for that. Alternatively, a script could set the colors based on its own custom rules.

I considered folder formats, but could not sort how to change tab color only when a file with a given extension was in the folder. How is this done via folder formats?

Content type formats can trigger by file types/extensions. That's what they do. But note that they won't override the format used for folders that have a custom format defined for their path.

You'd want to set the threshold very low if you wanted just a single file with that extension to trigger the format. Using scripting may be a better fit if you want it to work with just one file, even if there are lots of other files in the folder.

Thanks for clarifying. I have several folders with custom formats, so I think scripting would be the better solution. Will look into it and would appreciate any advice.

Not much of a coder, but as I understand the flow, for each tab:

  1. enumerate through each file
  2. compare file extension with identified extension(s) user wants flagged
  3. Set particular tab color according to whether or not flagged extension(s) exist

Do I have the logic right?

That should work. You’ll probably need to handle the OnAfterFolderChange event. Possibly also the event for new tabs (I can’t remember if you get a folder change as well for those).

Thanks for the event pointer. Afraid I'm not there yet as I'm still trying to figure out the basics. Again not much of a coder...

I reviewed scripts in the Buttons/Scripts category that I thought might be of use. I also reviewed the manual script examples and various objects (FSUtil, FileTypeGroup, Item).

I think I need to create a function that:

  • uses FSUtil.ReadDir to enumerate through the folder contents
  • uses Item.groups to get the file's group OR uses Item.ext to get file's extension, depending on how I set it up
  • compares the group OR extension to the desired group (I assume using an if operation, but not sure how)
  • IF there is a match, use some method to recolor the tab (haven't found that yet)

I would also need to handle events like OnOpenLister, OnOpenTab and OnAfterFolderChange

What am I missing (besides obvious coding talent :slight_smile:)?

You can easily repurpose the script from a few days ago. Let the main routine loop through the files, count the extensions (leave the groups for later), decide on a color, and set it via Go TABCOLOR (Demo).

Thanks for the advice; that's the script I was attempting to decipher. And the GO TABCOLOR reference is helpful.

Not sure I understand using it to count extensions, however. I want to find a particular extension and if there's even just one, set the tab color.

Even easier then, no counting needed :+1:

I like the idea a lot.
Please, is it possible to have such a script?
Thanks
KK!

Unfortunately, I have made zero progress on this. The script alludes me.

I have read, reread, and re-reread through the sample script and other examples several times. I understand the script must first loop through each tab and item within each tab:

for (var e = new Enumerator(tab.lister.tabs); !e.atEnd(); e.moveNext()) {
        var tabItem = e.item();

The script should then check each item's extension and compare it to a given extension:

I am stuck here

Then the script should recolor the tab only if at least one item's extension (or file type group) in the tab matches the given extension (or file type group):

    var R = 255;
    var G = 128;
    var B = 0;
    cmd.RunCommand('Go TABCOLOR=' + R + ',' + G + ',' + B);

Please advise. Thanks!

May this inspire you :slight_smile:

for (var e = new Enumerator(tab.lister.tabs); !e.atEnd(); e.moveNext()) {
    var tabItem = e.item();

    var extFound = false;

    for (var ee = new Enumerator(tabItem.files); !ee.atEnd(); ee.moveNext()) {
        var fileItem = ee.item();
        if (fileItem.ext != '.txt') continue;
        extFound = true;
        break;
    }

    cmd.SetSourceTab(tabItem);

    if (extFound) {
        cmd.RunCommand('Go TABCOLOR=255,128,0');
    } else {
        cmd.RunCommand('Go TABCOLOR=reset');
    }
}

That worked, thanks! Here's the initial script:

function OnInit(initData) {
    initData.name = 'RecolorTabBasedOnFileExtension';
    initData.version = '2023-02-05';
    initData.copyright = '';
    initData.url = 'https://resource.dopus.com/t/tab-color-based-on-folder-content/43305';
    initData.desc = 'Change tab color based on file extensions';
    initData.default_enable = true;
    initData.min_version = '12.0';
}

function OnOpenLister(openListerData) {
    DOpus.Output('*** OnOpenLister');
    if (!openListerData.after) return true;
    checkFileExtension(openListerData.lister.activetab);
    return false;
}

function OnActivateLister(activateListerData) {
    DOpus.Output('*** OnActivateLister');
    if (!activateListerData.active) return;
    checkFileExtension(activateListerData.lister.activetab);
}

function OnOpenTab(openTabData) {
    DOpus.Output('*** OnOpenTab');
    checkFileExtension(openTabData.tab);
}

function OnActivateTab(activateTabData) {
    DOpus.Output('*** OnActivateTab');
    checkFileExtension(activateTabData.newtab);
}

function OnSourceDestChange(sourceDestChangeData) {
    DOpus.Output('*** OnSourceDestChange');
    checkFileExtension(sourceDestChangeData.tab);
}

function OnAfterFolderChange(afterFolderChangeData) {
    DOpus.Output('*** OnAfterFolderChange');
    checkFileExtension(afterFolderChangeData.tab);
}

function OnDoubleClick(doubleClickData) {
    DOpus.Output('*** OnDoubleClick');
    checkFileExtension(doubleClickData.tab);
}

function OnDisplayModeChange(displayModeChangeData) {
    DOpus.Output('*** OnDisplayModeChange');
    checkFileExtension(displayModeChangeData.tab);
}

function OnTabClick(tabClickData) {
    DOpus.Output('*** OnTabClick');
    checkFileExtension(tabClickData.tab);
}

var cmd = DOpus.Create().Command();
var fsu = DOpus.FSUtil();

function checkFileExtension(tab) {
	for (var e = new Enumerator(tab.lister.tabs); !e.atEnd(); e.moveNext()) {
    var tabItem = e.item();

    var extFound = false;

    for (var ee = new Enumerator(tabItem.files); !ee.atEnd(); ee.moveNext()) {
        var fileItem = ee.item();
        //if (fileItem.groups != 'programs') continue;
		if (fileItem.ext != '.txt') continue;
        extFound = true;
        break;
    }

    cmd.SetSourceTab(tabItem);

    if (extFound) {
        cmd.RunCommand('Go TABCOLOR=255,128,0');
    } else {
        cmd.RunCommand('Go TABCOLOR=reset');
		}
	}
}

That said, want to get a bit more sophisticated and let the user choose between a file type group or extension. It does not initialize:

function OnInit(initData) {
    initData.name = 'RecolorTabBasedOnFileTypeGroupOrExtension';
    initData.version = '2023-02-05';
    initData.copyright = '';
    initData.url = 'https://resource.dopus.com/t/tab-color-based-on-folder-content/43305';
    initData.desc = 'Change tab color based on file extensions';
    initData.default_enable = true;
    initData.min_version = '12.0';

	function ConfigHelper(initData){
      this.data=data; this.descriptions=null; this.last=null;
      this.add = function(name, val, description){
         this.data.config[name]=val; this.last=[this.data.config[name],name];
         if (description!=undefined) this.des(description); return this;}
      this.des = function(description){
         if (!(description && DOpus.version.AtLeast("11.6.1"))) return this;
         if (!this.descriptions){ this.descriptions=DOpus.NewMap();
            data.config._descriptions=this.descriptions; }
         this.descriptions(this.last[1])=description; return this;}
      this.val = function(val){
         if (typeof this.last[0]=="object") this.last[0].push_back(val);
         else this.last[0]=val; return this;}
	}
	//==================================================
	var cfg = new ConfigHelper(initData);
	cfg.add("Highlight Type",    DOpus.Create.Vector()).
		des("File Type Group or Specific Extension").
		val(0).val("FileTypeGroup").val("Extension");
	cfg.add("FTG",     "programs").
		des("Name of File Type Group");
	cfg.add("EXT",     "exe").
		des("Extension name");	 
	cfg.add("Red Value", "0");
		des("Red Value");
	cfg.add("Green Value", "0");
		des("Green Value");
	cfg.add("Blue Value", "0");
		des("Blue Value");	
}

function OnOpenLister(openListerData) {
    DOpus.Output('*** OnOpenLister');
    if (!openListerData.after) return true;
    checkFileExtension(openListerData.lister.activetab);
    return false;
}

function OnActivateLister(activateListerData) {
    DOpus.Output('*** OnActivateLister');
    if (!activateListerData.active) return;
    checkFileExtension(activateListerData.lister.activetab);
}

function OnOpenTab(openTabData) {
    DOpus.Output('*** OnOpenTab');
    checkFileExtension(openTabData.tab);
}

function OnActivateTab(activateTabData) {
    DOpus.Output('*** OnActivateTab');
    checkFileExtension(activateTabData.newtab);
}

function OnSourceDestChange(sourceDestChangeData) {
    DOpus.Output('*** OnSourceDestChange');
    checkFileExtension(sourceDestChangeData.tab);
}

function OnAfterFolderChange(afterFolderChangeData) {
    DOpus.Output('*** OnAfterFolderChange');
    checkFileExtension(afterFolderChangeData.tab);
}

function OnDoubleClick(doubleClickData) {
    DOpus.Output('*** OnDoubleClick');
    checkFileExtension(doubleClickData.tab);
}

function OnDisplayModeChange(displayModeChangeData) {
    DOpus.Output('*** OnDisplayModeChange');
    checkFileExtension(displayModeChangeData.tab);
}

function OnTabClick(tabClickData) {
    DOpus.Output('*** OnTabClick');
    checkFileExtension(tabClickData.tab);
}

var cmd = DOpus.Create().Command();
var fsu = DOpus.FSUtil();

function checkFileExtension(tab) {
	for (var e = new Enumerator(tab.lister.tabs); !e.atEnd(); e.moveNext()) {
    var tabItem = e.item();

    var extFound = false;
	var highlight = Script.config["Highlight Type"];
	var ftg = Script.config["FTG"];
	var extSelected = Script.config["EXT"];
	var rValue = Script.config["Red Value"];
	var gValue = Script.config["Green Value"];
	var bValue = Script.config["Blue Value"];
	
    for (var ee = new Enumerator(tabItem.files); !ee.atEnd(); ee.moveNext()) {
        var fileItem = ee.item();
        if (highlight != "FileTypeGroup") { continue;
			if (fileItem.groups != ftg) continue;
			extFound = true;
		}
		else (highlight != "Specific Extension")
			if (fileItem.ext != extSelected) continue;
			extFound = true;
        break;
    }

    cmd.SetSourceTab(tabItem);

    if (extFound) {
        cmd.RunCommand('Go TABCOLOR=' + rValue + ',' + gValue + ',' + bValue);
    } else {
        cmd.RunCommand('Go TABCOLOR=reset');
		}
	}
}

What have I missed?

I have never used the external config options, especially not the config helper, which might be outdated. You'd need the consult with the author tbone himself.

In the lower part, you've got some brackets wrong, not sure what you meant to achieve. Running the script through a formatter like CyberChef will help find errors.

Thank you for the advice and link. I will keep at it.

The ScriptConfig object is very simple to use, and I'd recommend using it directly, at least at first. Otherwise you have to understand how ConfigHelper works on top of how the underlying object works.

A couple of simple examples of using ScriptConfig can be found here:

Thanks for the formatter reference @lxp and thanks for the references @Leo. I have attempted to use ScriptConfig in JScript. This fails to initialize:

function OnInit(initData) {
    initData.name = 'RecolorTabBasedOnFileTypeGroupOrExtension';
    initData.version = '2023-02-05';
    initData.copyright = '';
    initData.url = 'https://resource.dopus.com/t/tab-color-based-on-folder-content/43305';
    initData.desc = 'Change tab color based on file extensions';
    initData.default_enable = true;
    initData.min_version = '12.0';
	
	initData.config.HighlightType = DOpus.Create.Vector("File Type Group","Extension");
	initData.config.HighlightFTG = DOpus.Create.Vector("Archives","Databases","Documents","Images","Movies","Music","Programs");
	initData.config.FileType = "txt";
	initData.config.RedValue = "0";
	initData.config.GreenValue = "0";
	initData.config.BlueValue = "0";
	
	initData.config_desc = DOpus.Create.Map();
	initData.config_desc[HighightType] = "Select File Type Group or Extension";
	initData.config_desc[HighlightFTG] = "Select File Type Group; used only if File Type Group selected above"
	initData.config_desc[FileType] = "Select file type; used only if Extension selected above"
	initData.config_desc[RedValue] = "Enter number between 0 and 255"
	initData.config_desc[GreenValue] = "Enter number between 0 and 255"
	initData.config_desc[BlueValue] = "Enter number between 0 and 255"
}

function OnOpenLister(openListerData) {
    DOpus.Output('*** OnOpenLister');
    if (!openListerData.after) return true;
    checkFileExtension(openListerData.lister.activetab);
    return false;
}

function OnActivateLister(activateListerData) {
    DOpus.Output('*** OnActivateLister');
    if (!activateListerData.active) return;
    checkFileExtension(activateListerData.lister.activetab);
}

function OnOpenTab(openTabData) {
    DOpus.Output('*** OnOpenTab');
    checkFileExtension(openTabData.tab);
}

function OnActivateTab(activateTabData) {
    DOpus.Output('*** OnActivateTab');
    checkFileExtension(activateTabData.newtab);
}

function OnSourceDestChange(sourceDestChangeData) {
    DOpus.Output('*** OnSourceDestChange');
    checkFileExtension(sourceDestChangeData.tab);
}

function OnAfterFolderChange(afterFolderChangeData) {
    DOpus.Output('*** OnAfterFolderChange');
    checkFileExtension(afterFolderChangeData.tab);
}

function OnDoubleClick(doubleClickData) {
    DOpus.Output('*** OnDoubleClick');
    checkFileExtension(doubleClickData.tab);
}

function OnDisplayModeChange(displayModeChangeData) {
    DOpus.Output('*** OnDisplayModeChange');
    checkFileExtension(displayModeChangeData.tab);
}

function OnTabClick(tabClickData) {
    DOpus.Output('*** OnTabClick');
    checkFileExtension(tabClickData.tab);
}

var cmd = DOpus.Create().Command();
var fsu = DOpus.FSUtil();

function checkFileExtension(tab) {
	for (var e = new Enumerator(tab.lister.tabs); !e.atEnd(); e.moveNext()) {
    var tabItem = e.item();

    var extFound = false;
	var highlight = Script.config.HighlightType;
	var ftg = Script.config.HighlightFTG;
	var extSelected = Script.config.FileType;
	var rValue = Script.config.RedValue;
	var gValue = Script.config.GreenValue;
	var bValue = Script.config.BlueValue;
	
		for (var ee = new Enumerator(tabItem.files); !ee.atEnd(); ee.moveNext()) {
			var fileItem = ee.item();
			if (highlight != "File Type Group") { continue;
				if (fileItem.groups != ftg) continue;
				extFound = true;
			}
			else (highlight != "Extension") 
				if (fileItem.ext != extSelected) continue;
				extFound = true;
			}
			break;
		}

    cmd.SetSourceTab(tabItem);

    if (extFound) {
        cmd.RunCommand('Go TABCOLOR=' + rValue + ',' + gValue + ',' + bValue);
	} 
	else {
        cmd.RunCommand('Go TABCOLOR=reset');
	}
}

What might I be missing?