Column - Custom Text and Regexp

What about for the name? I didn't test, but would it cause issues if there are two columns with the same name? As this script allows the user to add multiple columns, I thought prefixes would prevent any name conflicts with columns added via other scripts.
I agree that the column name check in the callback is not needed. I will remove it from the sample scripts to avoid confusion.

Nice :slight_smile:

The script's name is part of the column's name in any context except inside the script itself (where it is implicit).

See the COLUMNSADD example in my reply above.

Makes sense, thanks.

Hi Wowbagger,

I already started a very "similar" script myself, for exchange of experiences - if you are interested - you could find it attached to this post.

RegExColumns.zip (7.7 KB)

Bye,

Miran

Nice script, mind if I steal some ideas? I like that you support other column types.

Curious, wont this line of code take the second match?

result = result[1];

This is a good idea. This would be a good thing to have built in to Dopus.

	// DOpus Version Check 
	if(!DOpus.version.atLeast(MIN_VERSION)){DOpus.OutputString("ERR: DOpus Version " + MIN_VERSION + "required to run this script!")}

I want you to "steal", that's the main reason I postet the script :slight_smile:

Yes it would, and it should :slight_smile: I only need the first group for the custom column, i first though about extracting "all" custom columns with one regexp, but as DOpus calls the function for every column individualy (because it is the only valid way) it does not make sense.
The only feature that require to catch all groups would be for concatenated column values, where the input for the new column is not one connected string in the filename but two parts of the filenamen....while writing, this sounds like a valid use case :slight_smile:

V1.1 with Multi capture group support
RegExColumns.zip (7.97 KB)
V1.3 added type rating
RegExColumns.zip (9.03 KB)

Indeed, or even better, not just a log output, but a greyed out entry in the preferences page. I will submit this as a feature request.

I was confused about where to place the patterns because the Configure button is greyed out. Finally understood where to configure the script so here goes in case someone else is wondering.

By renaming the osp file to zip (or adding osp in Prefs/zip files/zip extensions) and extracting the js file, the script becomes editable and it's possible to paste patterns to configure the script.

This should be immensely valuable.
Thank you!
:thumbsup:


Hi wowbagger,
Your script is very useful. I would suggest a small improvement to allow the use of capture groups. The reason is that JS probably has one of the most crippled regex flavors. To work around the lack of lookbehind or \K, you need capture groups. This could be important for instance in a file where you want to create a column using the nth group of digits.

With the current script this is a minor change. I'd suggest updating the column definitions to allow an optional "group" property.
Then we only need to replace line 65 with something like:

var capturegroup = (typeof config.group === 'undefined') ? 0 : config.group; 
scriptColData.value = result[capturegroup];

Here is a quick example of definitions for a hypothetical file convention that would include a phone number and name:

  columns: [{
    //Name of column, Title.
    name:"area",
    //input values
    itemProperty: "name_stem",
    //regexp to extract value
    re: /^\d+/,
    // group to be returned (optional if whole match, i.e. group 0)
    // group: 0
  }, {
    name:"phone",
    itemProperty: "name_stem",
    re: /^\d+\D+(\d+[- ]\d+)/,
    group: 1
  }, {
    name:"who",
    itemProperty: "name_stem",
    re: /^\d+\D+\d+[- ]\d+\s*(\S+)/,
    group: 1
  }]


(Attaching these incorporated changes for the convenience of anyone interested. Didn't know what to do with the version number and naming convention, sorry if it is wrong.)

Other Thoughts
Here we're running three match attempts when one pattern would be enough to fan the data into three groups, and that's a bit of a shame. It would be better if we could use a single regex for all three columns, using various capture groups as results. I haven't studied the scripting interface so have no idea if processing multiple columns at once is possible.
CustomtextColumn with Groups.js.txt (2.38 KB)

Another small addition I'm finding useful:
When creating a naming convention with a view of parsing long file names into regex columns, one risk is to run out of characters for the file name (the Windows max for path + file name is 260).

The following tweak adds columns so you can keep track of the length of file names.

In the column definitions:

  { name:"c.namelen",
    // this column give you the length of the file name
  },

  { name:"c.charsleft",
    // this column give you the number of chars still available to use in the path + file name
    // Windows imposes a 260 maximum length for the Path+Filename
  },

// no need for a custom column for the length of file + pathname as that column is available under General / Path length

In OnCustomText:

  // filename length column?
  if (config.name == 'c.namelen') {
    scriptColData.value = scriptColData.item.name.length.toString()
  }

  // characters left column?
  else if (config.name == 'c.charsleft') {
    // Windows imposes a 260 maximum length for the Path+Filename    
    // how many more chars available in the path + filename? 
    var PathLength = new String(scriptColData.item.realpath).length
    scriptColData.value = (260-PathLength).toString()
  }

  else { // it's a regex column
    if(!config.itemProperty)
    // etc

There's a built-in Path Length column which should be better/quicker for that one of the three. I think the other two are new & useful, though.

Ahhh, indeed! I missed that. Removed it. Thank you. :slight_smile:

Thanks for all the feedback @playful and @Miran. Both your scripts were good reading.
A new version V1.5 has been created that uses a dialog to configure the columns. See First post.

Easy to use and very useful.
Thank you.

1 Like

Updated to version 1.5.2. This added support for passing in extend item properties, (I.E. metadata.doc.author) as the source input for the regexp to execute against.

This idea and code is thanks to @andersonnnunes, who posted the idea here.

1 Like

Updated to version 1.5.5.
Changes

  • fixed grouping, the nogroup was always being set to true.

As reported here

Oh so the problem was RegEx and not Directory Opus.
I confirm it's fixed. Thanks

Well technically not the RegExp. The RegExp that special string we created to extract parts of the file name. The issue was in this custom column script. I didnt get the grouping right, and was disabling grouping on all the columns it created. Dopus was doing exactly as asked.
Thanks for confirming it's fixed.

Hi @wowbagger

I have just tried your script. Very nice! Two nits to report.

  1. The Apply button text reads "Appy Changes" instead of "Apply Changes".
  2. If I modify a column label and press apply the change is not shown. The change is made but doesn't show up until the column is closed and reopened.

Regards, AB

Now for a real question. I have created a regexp to isolate an identifier in the filename with the format XX followed by 3 digits followed by zero or more suffix letters. This identifier can appear anywhere in the name and is not necessarily present.

I am using: (.\*)(\bXX\d{3}[\w]*)(.\*) and my output criterion is $2. It works fine when there is a match for $2 but when there is no match the result is the whole original string. e.g.

Some text XX123a some description.txt ==> XX123a
XXxxx some description.txt ==> XXxxx some description

I would like a mismatch to return an empty string. I am no expert so my regexp may well be incorrect for what I want and of course this behaviour may be working as designed. Advice welcomed....

Regards, AB

hi @aussieboykie.

I don't know if I would call that a bug of a feature ;).
I updated the script to first check if the regexp is a match before preforming the replace. Otherwise the replace method would return the the input as there was nothing to replace. This seems better.

I don't know if you can force the column to reload. The script only reloads the column values when you press apply.

Updated to version 1.5.6

  • Only return a value if the regexp is a match.
  • Fixed typo in the Apply button