How to color file types from a script?

Hi : I have a .js script to color according to file types, but it does not work, Someone can please help me. What is wrong?

Note: I want to color file types using a script.

I create a new button in Preferences/Customize toolbars and paste my code.

Here is the script :

// Import the Directory Opus API
import { DO_EVENT_AFTERFOLDERCHANGE, DO_EVENT_ONINIT } from "directoryopus-api";

// Initialization function
function OnInit() {
  // Register event handlers
  DO_RegisterEvent(DO_EVENT_AFTERFOLDERCHANGE, OnAfterFolderChange);
}

// Function to run when the folder changes
function OnAfterFolderChange() {
  // Get the current folder
  var currentFolder = DO_GetCurrentFolder();

  // Get all files in the current folder
  var files = DO_GetFilesFromFolder(currentFolder);

  // Define a dictionary to map file extensions to colors
  var colorMap = {
    ".exe": DO_RGB(255, 0, 0), // Red
    ".zip": DO_RGB(255, 0, 0), // Red
    ".rar": DO_RGB(255, 0, 0), // Red
    ".msi": DO_RGB(255, 0, 0), // Red
    ".iso": DO_RGB(255, 0, 0), // Red
    ".mobi": DO_RGB(128, 0, 128), // Purple
    ".epub": DO_RGB(128, 0, 128), // Purple
    ".pdf": DO_RGB(128, 0, 128), // Purple
    ".txt": DO_RGB(255, 255, 255), // White
    ".json": DO_RGB(255, 255, 255), // White
    ".ini": DO_RGB(255, 255, 255), // White
    ".csv": DO_RGB(255, 255, 255), // White
    ".docx": DO_RGB(0, 128, 255), // Cyan
    ".xlsx": DO_RGB(0, 128, 255), // Cyan
    ".pptx": DO_RGB(0, 128, 255), // Cyan
    ".mpp": DO_RGB(0, 128, 255), // Cyan
    ".accdb": DO_RGB(0, 128, 255), // Cyan
    ".mp3": DO_RGB(255, 128, 0), // Orange
    ".mp4": DO_RGB(255, 128, 0) // Orange
  };

  // Iterate through the selected files and apply colors based on their extensions
  for (var i = 0; i < files.length; i++) {
    var file = files[i];
    var extension = file.Extension;

    if (colorMap.hasOwnProperty(extension)) {
      var color = colorMap[extension];
      DO_SetFileColor(file, color);
    }
  }
}

// Register the OnInit function
DO_RegisterEvent(DO_EVENT_ONINIT, OnInit);
1 Like

What is DO_SetFileColor at the bottom of the script? It does not seem to be defined anywhere within the script (and isn't part of the Opus script API).

Sorry , I edit the script. That is the good one.
How can install the directory opus api?
Or maybe someone has an alternative solution?
Thanks

What about this script, What is wrong?

function OnInit(initData) {
    initData.name = "Auto File Color Filter";
    initData.desc = "Automatically color files based on their extensions without user selection.";
    initData.copyright = "RAIG";
    initData.version = "1.4";
    initData.default_enable = true;
}

function OnAfterFolderChange(afterFolderChangeData) {
    var cmd = DOpus.Create.Command();
    cmd.SetSourceTab(afterFolderChangeData.tab); // Ensure the command works on the current tab

    // Red for executables, zips, etc.
    cmd.RunCommand('Select NONE');  // Deselect all first
    cmd.RunCommand('Select "*.exe" "*.zip" "*.rar" "*.msi" "*.iso" EXACT DESELECTNOMATCH');
    cmd.RunCommand('Properties SETCOLOR=rgb(255,0,0)');  // Red color
    
    // Purple for eBooks and PDFs
    cmd.RunCommand('Select NONE');  // Deselect all first
    cmd.RunCommand('Select "*.mobi" "*.epub" "*.pdf" EXACT DESELECTNOMATCH');
    cmd.RunCommand('Properties SETCOLOR=rgb(128,0,128)');  // Purple color
    
    // White for text files
    cmd.RunCommand('Select NONE');  // Deselect all first
    cmd.RunCommand('Select "*.txt" "*.json" "*.ini" "*.csv" EXACT DESELECTNOMATCH');
    cmd.RunCommand('Properties SETCOLOR=rgb(255,255,255)');  // White color
    
    // Cyan for Microsoft Office files
    cmd.RunCommand('Select NONE');  // Deselect all first
    cmd.RunCommand('Select "*.doc" "*.docx" "*.xls" "*.xlsx" "*.ppt" "*.pptx" "*.mpp" "*.mdb" "*.accdb" EXACT DESELECTNOMATCH');
    cmd.RunCommand('Properties SETCOLOR=rgb(0,255,255)');  // Cyan color
    
    // Orange for media files
    cmd.RunCommand('Select NONE');  // Deselect all first
    cmd.RunCommand('Select "*.mp3" "*.mp4" "*.avi" "*.mkv" "*.wav" "*.flac" EXACT DESELECTNOMATCH');
    cmd.RunCommand('Properties SETCOLOR=rgb(255,165,0)');  // Orange color
}

https://docs.dopus.com/doku.php?id=reference:command_reference:internal_commands:properties

SETCOLOR is not an option of the PROPERTIES command.
cmd.RunCommand('Properties SETCOLOR=rgb(0,255,255)'); can not work.

You don't have to do nothing. It is available in the script.
Reference is in the manual :
https://docs.dopus.com/doku.php?id=reference:scripting_reference

This can be done by assigning (via Settings/Preferences) labels by mask (mask being in the form *.ext or if you want multiple file types for the same color *.(ext1|ext2|ext3)$ with regexp ticked in the dialog)

What is "directoryopus-api" ??

Your code looks like one that comes from a script add-in, not the one in a script button.
If you want to interact with events such as OnAfterFolderChange, you can not do that in a button.
Scripts that you put in a button trigger when you click the button, and have an OnClick entry point (if you open/edit the button and choose script from the drop down, you get the proper boilerplate).

If you want to interact with event, go to Preferences/Scripts and create a script (you'll be able to choose what events your script will be able to manage).

Skip the AI nonsense and use labels via Preferences / Labels / Label Assignments.

The script below does not have errors but DOES NOTHING.
Help please.

// This script colors files based on their extension

// Define the colors and extensions for each color
var colors = {
  "red": ["txt", "log", "ini", "cfg"],
  "green": ["js", "css", "html", "xml"],
  "blue": ["jpg", "png", "gif", "bmp"],
  "purple": ["pdf", "docx", "xlsx", "pptx"]
};

// Called after a new folder is read in a tab
function OnAfterFolderChange(afterFolderChangeData)
{
  // Get the active list (assuming it's the file list)
  var list = DOpus.ActiveList;

  // Check if the list is not empty
  if (list != null && list.Count > 0) {
    // Loop through all files in the current directory
    for (var i = 0; i < list.Count; i++) {
      var file = list.Item(i);

      // Get the file extension
      var extension = file.Name.split(".").pop().toLowerCase();

      // Check if the extension is in the colors dictionary
      for (var color in colors) {
        if (colors[color].includes(extension)) {
          // Set the file color
          file.Color = color;
          break;
        }
      }
    }
  }
}

You did not read what I wrote :slight_smile:
You did not read what @lxp wrote :slight_smile:
You did not read the manual :slight_smile:

The answer is in our posts : to do what you want, you need Labels.

1 Like

Yes, if you just want to assign different colors to different extensions, using a script that applies permanent labels to files is a very bad way to do it.

You can set up wildcard labels via Preferences / Labels / Label Assignments which do things directly and in a much easier and more efficient way.