Tab Color Based on Folder Content

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

You mean the software will detect it?

This is after enabling the scripts:

Does this mean that it did not work?
Thanks

What are you expecting the script to do?

Out of the box, it looks like it colors tabs for normal folders based on the types of files in them. Your screenshot shows two empty collections, so I'm not sure if the script would do anything for those.

1 Like

Go to Preferences > Scripts and left click on the TabHighlightColor script name. The "Edit Configuration for 'TabHighlightColor'" dialog should appear:

The first choice, Category, determines whether the script will look for an Extension or FileTypeGroup.

If Category is set for Extension, then it will look for the extension entered in the Extension value and ignore the FileTypeGroup value.

If Category is set fot FileTypeGroup, then it will look for the filetypegroup entered in the FileTypeGroup value and ignore the Extension value.

Once set, DOpus will trigger the script in a number of situations. That said, the script will do nothing if the tab has no file(s) ,matching your configured criteria.

As a test, place a txt file in your Downloads folder. Configure the script Category value to Extension and set the Extension value to txt.

image

Click "Apply" on the Preferences screen, then refresh the Download tab and the script should work.

Now set the script Category value to FileTypeGroup and set the FileTypeGroup value to programs.

image

Click "Apply" on the Preferences screen, then refresh the Download tab . Provided there are no programs in your Downloads folder, the script should find no matches and remove the tab color accordingly.

Hope this helps.

1 Like

Please, if I want to add another category, is it possible?

Yes. Start here: https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Groups.htm

Whatever groups you add would need to be added in the script, as well. You can look at the script in a text editor like Notepad to see where and how the groups are referenced.

1 Like

You put a lot of effort into this script indeed.
Thanks