# Tab Color Based on Folder Content

**URL:** https://resource.dopus.com/t/tab-color-based-on-folder-content/43305
**Category:** Help & Support
**Created:** [January 1, 2023, 5:10pm UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305 "2023-01-01T17:10:17Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Chuck](https://resource.dopus.com/user_avatar/resource.dopus.com/chuck/32/23917_2.png) [@Chuck](https://resource.dopus.com/u/Chuck)
#### Post date: [January 1, 2023, 5:10pm UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/1 "2023-01-01T17:10:17Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)
#### Post date: [January 1, 2023, 8:33pm UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/2 "2023-01-01T20:33:29Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Chuck](https://resource.dopus.com/user_avatar/resource.dopus.com/chuck/32/23917_2.png) [@Chuck](https://resource.dopus.com/u/Chuck)
#### Post date: [January 1, 2023, 9:10pm UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/3 "2023-01-01T21:10:44Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)
#### Post date: [January 1, 2023, 9:54pm UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/4 "2023-01-01T21:54:30Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Chuck](https://resource.dopus.com/user_avatar/resource.dopus.com/chuck/32/23917_2.png) [@Chuck](https://resource.dopus.com/u/Chuck)
#### Post date: [January 1, 2023, 10:05pm UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/5 "2023-01-01T22:05:19Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Chuck](https://resource.dopus.com/user_avatar/resource.dopus.com/chuck/32/23917_2.png) [@Chuck](https://resource.dopus.com/u/Chuck)
#### Post date: [January 2, 2023, 5:22pm UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/6 "2023-01-02T17:22:04Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)
#### Post date: [January 2, 2023, 8:15pm UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/7 "2023-01-02T20:15:03Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![Chuck](https://resource.dopus.com/user_avatar/resource.dopus.com/chuck/32/23917_2.png) [@Chuck](https://resource.dopus.com/u/Chuck)
#### Post date: [January 2, 2023, 9:26pm UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/8 "2023-01-02T21:26:17Z")

</div>

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 🙂)?

---

<div class="post-metadata">

### Author: ![lxp](https://resource.dopus.com/user_avatar/resource.dopus.com/lxp/32/33734_2.png) [@lxp](https://resource.dopus.com/u/lxp)
#### Post date: [January 2, 2023, 10:04pm UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/9 "2023-01-02T22:04:50Z")

</div>

You can easily repurpose the script [from a few days ago](https://resource.dopus.com/t/item-count-in-tab-name/43276). 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](https://resource.dopus.com/t/set-folder-tabs-to-random-non-repeated-colors-by-default/32300)).

---

<div class="post-metadata">

### Author: ![Chuck](https://resource.dopus.com/user_avatar/resource.dopus.com/chuck/32/23917_2.png) [@Chuck](https://resource.dopus.com/u/Chuck)
#### Post date: [January 2, 2023, 10:21pm UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/10 "2023-01-02T22:21:41Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![lxp](https://resource.dopus.com/user_avatar/resource.dopus.com/lxp/32/33734_2.png) [@lxp](https://resource.dopus.com/u/lxp)
#### Post date: [January 2, 2023, 10:29pm UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/11 "2023-01-02T22:29:22Z")

</div>

> [@Chuck](#):
>
> if there's even just one

Even easier then, no counting needed 👍

---

<div class="post-metadata">

### Author: ![KebordKauboi](https://resource.dopus.com/user_avatar/resource.dopus.com/kebordkauboi/32/37904_2.png) [@KebordKauboi](https://resource.dopus.com/u/KebordKauboi)
#### Post date: [January 26, 2023, 8:03am UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/12 "2023-01-26T08:03:20Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chuck](https://resource.dopus.com/user_avatar/resource.dopus.com/chuck/32/23917_2.png) [@Chuck](https://resource.dopus.com/u/Chuck)
#### Post date: [January 26, 2023, 12:44pm UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/13 "2023-01-26T12:44:16Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chuck](https://resource.dopus.com/user_avatar/resource.dopus.com/chuck/32/23917_2.png) [@Chuck](https://resource.dopus.com/u/Chuck)
#### Post date: [February 5, 2023, 3:50pm UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/14 "2023-02-05T15:50:33Z")

</div>

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:

```auto
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:

```auto
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):

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

```

Please advise. Thanks!

---

<div class="post-metadata">

### Author: ![lxp](https://resource.dopus.com/user_avatar/resource.dopus.com/lxp/32/33734_2.png) [@lxp](https://resource.dopus.com/u/lxp)
#### Post date: [February 5, 2023, 5:19pm UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/15 "2023-02-05T17:19:57Z")

</div>

May this inspire you 🙂

```javascript
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');
    }
}

```

---

<div class="post-metadata">

### Author: ![Chuck](https://resource.dopus.com/user_avatar/resource.dopus.com/chuck/32/23917_2.png) [@Chuck](https://resource.dopus.com/u/Chuck)
#### Post date: [February 5, 2023, 9:27pm UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/16 "2023-02-05T21:27:00Z")

</div>

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

```auto
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:

```auto
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?

---

<div class="post-metadata">

### Author: ![lxp](https://resource.dopus.com/user_avatar/resource.dopus.com/lxp/32/33734_2.png) [@lxp](https://resource.dopus.com/u/lxp)
#### Post date: [February 6, 2023, 10:44am UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/17 "2023-02-06T10:44:00Z")

</div>

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](https://gchq.github.io/CyberChef/#recipe=JavaScript_Beautify('%20%20%20%20','Single',true,true)) will help find errors.

---

<div class="post-metadata">

### Author: ![Chuck](https://resource.dopus.com/user_avatar/resource.dopus.com/chuck/32/23917_2.png) [@Chuck](https://resource.dopus.com/u/Chuck)
#### Post date: [February 6, 2023, 1:03pm UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/18 "2023-02-06T13:03:14Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)
#### Post date: [February 6, 2023, 1:15pm UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/19 "2023-02-06T13:15:05Z")

</div>

The [ScriptConfig](https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/ScriptConfig.htm) object is very simple to use, and I'd recommend using it directly, at least at first. Otherwise you have to understand how [ConfigHelper](https://resource.dopus.com/t/helper-confighelper-easier-config-item-handling/19129) works on top of how the underlying object works.

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

- [Auto-load Thumbnails only in specific folders](https://resource.dopus.com/t/auto-load-thumbnails-only-in-specific-folders/33666)  
VBScript. Configurable list of folders, and a configurable boolean flag.

- [Columns: Maximum and Average File Size](https://resource.dopus.com/t/columns-maximum-and-average-file-size/25572)  
JScript. Configurable minimum file size.

---

<div class="post-metadata">

### Author: ![Chuck](https://resource.dopus.com/user_avatar/resource.dopus.com/chuck/32/23917_2.png) [@Chuck](https://resource.dopus.com/u/Chuck)
#### Post date: [February 7, 2023, 12:54am UTC](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305/20 "2023-02-07T00:54:49Z")

</div>

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

```auto
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?

[Next page](https://resource.dopus.com/t/tab-color-based-on-folder-content/43305.md?page=2)
