Tab Color Based on Folder Content

First thing to try is always to simplify what you're doing.

  • Does it work if you only add the simple FileType = "txt" line and no other config items?
  • If so, add some more lines, to work out which one(s) are the actual problem.
  • Once you know that, the problem may reveal itself, or you can point us to it and we can help.

I dumbed the script down as suggested. Here's the function:

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.groupsobject != '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');
		}
	}
}

It works as is. Changing these two lines returns a "Subscript Out of Range" error:

        if (fileItem.groupsobject != 'Programs') continue;
	    //if (fileItem.ext != '.txt') continue;

"Subscript Out of Range" error

groupsobject returns a FiletypeGroups object, not a string.

You can use InGroup() instead:

if (!fileItem.InGroup('Programs')) continue;

https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/Item.htm

That works!

In your Columns example, config parameter is reflected like this:

initData.config.AvgMinSize = "5 MB";
initData.config_desc = DOpus.Create.Map("AvgMinSize", "When calculating average sizes, ignore files smaller than this unless they are the only ones. Bytes unless KB, MB or GB suffix added.")

How would you use initData.config and inti.config_desc with a vector, including options and descriptions?

I have spent way too many hours on this and have yet to figure out how to make this work:

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

It works if I limit to either program or text, but I want to be able to check for the program file type group first. If it comes up empty, then check for .txt extension.

I have, in fact, checked brackets via CyberChef.

Any ideas how to make this work?

Are you sure? :slight_smile:


Looks like you want something like this:

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

image

Thanks for this @lxp, and thanks @Jon for pointing out that semicolon. Clearly, I need to get better at this, which includes understanding how to use tools like CyberChef and put stuff in the right place.

That said, the code you suggested does not work. I dumbed the function down as much as possible, per @Leo's earlier suggestion:

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.InGroup('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');
		}
	}
}

If I comment out either if statement, the function works with the other if statement fine. However, with the code as is, the if statements seem to cancel each other out, with no tab color change. Very frustrating.

Besides an inept coder, what might be causing this?

That is going to exclude everything except files that are both in the Programs group and have .txt extensions.

I doubt that is what you intended. It would usually exclude everything, unless .txt has been added to the Programs group.

That was not my intent. Here is what I am trying to achieve:

Color tab based on user-configured input, including:

  • Choose file type group or file type
  • If file type group selected, choose from pre-built file type group list
  • If file type selected, user enter extension
  • User enter tab color

So far - and with a lot of help - I have been able to get the tab color to change based on a hard-coded extension or specified file type group.

I have NOT been able to get the code to properly choose any configurable items listed above.

Again, I appreciate the continued assistance and will keep banging away at this. Thanks for any insights anyone has to help me move forward.

Latest attempt. I receive this error:

Error at line 108, position 4
Case 0: if (!fileItem.InGroup('Archives')) continue;
^
Expected '}' (0x800a03f1)

Here's the script:

function OnInit(initData) {
    initData.name = 'ReColorV2';
    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_desc = DOpus.Create.Map();
    initData.config_groups = DOpus.Create.Map();
    var configName = "";
	
	var vecType = DOpus.NewVector();
	vecType.push_back(0);
	vecType.push_back("FileTypeGroup");
	vecType.push_back("Extension");
	
	configName = "Category";
	initData.Config[configName] = vecType;
	initData.config_desc(configName) = "Select File Type Group or Extension";
	//initData.config_groups(configName) = "Choices";
	
	var vecGroup = DOpus.NewVector();
	vecGroup.push_back(0);
	vecGroup.push_back("Archives");
	vecGroup.push_back("Databases");
	vecGroup.push_back("Documents");
	vecGroup.push_back("Images");
	vecGroup.push_back("Movies");
	vecGroup.push_back("Music");
	vecGroup.push_back("Programs");
	
	configName = "Group"
	initData.Config[configName] = vecGroup;
	initData.config_desc(configName) = "Select File Type Group";
	//initData.config_groups(configName) = "Choices";
	
	configName = "Extension";
	initData.Config[configName] = "txt";
	initData.config_desc(configName) = "Enter file type to highlight (e.g. doc, txt, etc.)";
	initData.config_groups(configName) = "Choices";
	
}

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 (Script.Config["Category"] != 0) {
		switch (Script.Config["Group"]) {
			Case 0: if (!fileItem.InGroup('Archives')) continue;
				extFound = true;
				break;
			Case 1: if (!fileItem.InGroup('Databases')) continue;
				extFound = true;
				break;
			Case 2: if (!fileItem.InGroup('Documents')) continue;
				extFound = true;
				break;
			Case 3: if (!fileItem.InGroup('Images')) continue;
				extFound = true;
				break;
			Case 4: if (!fileItem.InGroup('Movies')) continue;
				extFound = true;
				break;
			Case 5: if (!fileItem.InGroup('Music')) continue;
				extFound = true;
				break;
			Case 6: if (!fileItem.InGroup('Programs')) continue;
				extFound = true;
				break;
		}
	} Else { 
		if (fileItem.ext != Script.Config["Extension"]) continue;
			extFound = true;
			break;
			}
}

    cmd.SetSourceTab(tabItem);

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

Appreciate any assistance.

JScript is case-sensitive: case and else need to be lowercase.

Thanks for pointing that out. It cleared the error. However, for some reason the script seems to ignore the Category selected and focuses strictly on the Extension entered.

The script may be ignoring the Category configured here:

var vecType = DOpus.NewVector();
	vecType.push_back(0);
	vecType.push_back("FileTypeGroup");
	vecType.push_back("Extension");
	
	configName = "Category";
	initData.Config[configName] = vecType;
	initData.config_desc(configName) = "Select File Type Group or Extension";
	initData.config_groups(configName) = "Choices";

And/or it may not be interpreting this correctly:

	if (Script.Config["Category"] != 0) {
		switch (Script.Config["Group"]) {
			case 0: if (!fileItem.InGroup('Archives')) continue;
				extFound = true;
				break;
			case 1: if (!fileItem.InGroup('Databases')) continue;
				extFound = true;
				break;
			case 2: if (!fileItem.InGroup('Documents')) continue;
				extFound = true;
				break;
			case 3: if (!fileItem.InGroup('Images')) continue;
				extFound = true;
				break;
			case 4: if (!fileItem.InGroup('Movies')) continue;
				extFound = true;
				break;
			case 5: if (!fileItem.InGroup('Music')) continue;
				extFound = true;
				break;
			case 6: if (!fileItem.InGroup('Programs')) continue;
				extFound = true;
				break;
		}

What am I missing? How might I test for this?

Script.Config["Category"] is going to return a string, not a number.

The same with Script.Config["Group"], assuming it's set up as in the earlier post.

You can use DOpus.Output(...) to display things in the script log panel if you need to check what they are.

Thanks for clarifying @Leo. I have it working; just need to refine it a bit.

Please, is it possible @Chuck to share the latest script if it worked?
Thanks

As requested.

TabHighlightColor.js.txt (6.2 KB)

1 Like

Thanks a lot for sharing it.
Please, I did read the post you thankfully attached, but to make sure that I understand the instructions correctly, shall I just rename the file to remove the .txt extension and let it be a .js extension?
and then drop the file into:
Preferences>Toolbars>Scripts
Or there is another way?
Thanks

You can rename the extension, but don't have to.

1 Like