GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Column - Custom Text and Regexp


#11

Makes sense, thanks.


#12

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


#13

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!")}

#14

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.


Sort/Group files/folders by Filter
#15

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:



#16

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)


#17

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

#18

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.


#19

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


#20

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.


#21

Easy to use and very useful.
Thank you.


#22

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.


Metadata and columns
[Solved] JScript AddColumn does not work, what am I doing wrong?
#23

Updated to version 1.5.5.
Changes

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

As reported here


#24

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


#25

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.


#26

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


Refreshing a custom column settings
#27

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


#28

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

#29

Good idea @aussieboykie this has been added. Thanks for the suggestion.

Updated to version 1.6

  • When updating a column, if that column is on a tab, it will removed and re-added. This does not work that well when renaming a column.
  • Added an Add to Lister button to the Dialog
  • Added a Remove from Lister button to the Dialog

#30

Thank you so much for your work, @wowbagger
That is a great Script Add-in.

I made a new version (1.7) on your behalf, and I would be glad if you check it.

Changelog: (code commented with “By @asakka”)

  • Added a checkbox in column settings, when enabled in case of “graph” or “igraph” types, the script make the graph relative to the largest value of all items inside current folder. The performance of the script is great, as I check for the max value only once and store it in script variable as long as the folder path is not changed.
  • Added three sample columns to demonstrate the “Graph Related” feature.
  • Added a button in the dialog to restore the sample columns

RegExpColumns.osp (34.6 KB)


Custom graph and igraph columns with relative values