Custom column: Number of characters of the name

Hi.
I realized only now the possibility of creating a custom column.

I would like to create a column that returns the number of characters of the file name excluding the extension.
Something similar to the "path length" column but without the path character count.
I looked at some examples but I did not understand how to do it.
For example, can I call some opus directory function in the script, or is it better to directly count the characters by creating a function?

I need some tips to get started.

Thanks.

You can access your item's filename with name_stem. name_stem.length will return its character count.

Hope that wasn't too brief :wink:

Here are a few more details

// This is a script for Directory Opus.
// http://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts
function OnInit(initData) {
    initData.name = 'NameLen Column';
    initData.version = '1.0';
    initData.copyright = '';
    initData.url = '';
    initData.desc = '';
    initData.default_enable = true;
    initData.min_version = '12.0';

    var col = initData.AddColumn();
    col.name = 'NameLen';
    col.method = 'OnColumn';
    col.label = 'NameLen';
    col.header = 'NameLen';
    col.justify = 'right';
    col.defwidth = 6;
    col.autorefresh = 1;
    col.autogroup = true;
    col.type = 'number';
}

function OnColumn(scriptColData) {
    if (scriptColData.col != 'NameLen')
        return;
    if (scriptColData.item.is_dir) {
        scriptColData.value = scriptColData.item.name.length;
    } else {
        scriptColData.value = scriptColData.item.name_stem.length;
    }
}

Drag this into your Scripts folder: NameLen.js.txt (929 Bytes)

2 Likes

Good script!

It's a very minor point but, for what it's worth, you should be able to use name_stem with the directory case as well. Opus doesn't consider directories as having extensions (in most situations), so name_stem will (should!) always return the full directory name, even if there is a dot in it somewhere.

if (scriptColData.item.is_dir)
    scriptColData.value = scriptColData.item.name.length;
else
    scriptColData.value = scriptColData.item.name_stem.length;

Should be the same as:

scriptColData.value = scriptColData.item.name_stem.length;

Doesn't change anything except code length, of course. But might help simplify things sometimes.

(If name_stem and name are different for an item where is_dir is true, let us know as that might be a bug.)

Yes, it is. Now this monster script becomes a one-liner:

function OnColumn(scriptColData) {
    if (scriptColData.col == 'NameLen') scriptColData.value = scriptColData.item.name_stem.length;
}

I'm now back from work.
I tried the script with the latest version of the function and everything works perfectly. And reading the post I begin to get an idea of what to do and how.

Thanks Lxp and thank you Leo.