Leo
January 1, 2023, 9:54pm
4
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.
Chuck
January 1, 2023, 10:05pm
5
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.
Chuck
January 2, 2023, 5:22pm
6
Not much of a coder, but as I understand the flow, for each tab:
enumerate through each file
compare file extension with identified extension(s) user wants flagged
Set particular tab color according to whether or not flagged extension(s) exist
Do I have the logic right?
Leo
January 2, 2023, 8:15pm
7
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).
Chuck
January 2, 2023, 9:26pm
8
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 )?
lxp
January 2, 2023, 10:04pm
9
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 ).
Chuck
January 2, 2023, 10:21pm
10
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.
lxp
January 2, 2023, 10:29pm
11
Chuck:
if there's even just one
Even easier then, no counting needed
I like the idea a lot.
Please, is it possible to have such a script?
Thanks
KK!
Chuck
January 26, 2023, 12:44pm
13
Unfortunately, I have made zero progress on this. The script alludes me.
Chuck
February 5, 2023, 3:50pm
14
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!
lxp
February 5, 2023, 5:19pm
15
May this inspire you
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');
}
}
Chuck
February 5, 2023, 9:27pm
16
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?
lxp
February 6, 2023, 10:44am
17
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.
Chuck
February 6, 2023, 1:03pm
18
Thank you for the advice and link. I will keep at it.
Leo
February 6, 2023, 1:15pm
19
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:
Chuck
February 7, 2023, 12:54am
20
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?
Leo
February 7, 2023, 12:56am
21
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.
Chuck
February 7, 2023, 2:03am
22
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;
lxp
February 7, 2023, 5:53am
23
"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