Custom Command (for context menu) String Processing

Not sure if this is doable, but I'll ask anyway.

In a custom command, is there a way to process the {filepath$} argument (or any string argument DOpus makes available for use for that matter) to extract a part of the filename (using a regular expression) and use the result in a subsequent command?

Use Case: Assume you have a pattern-based set of folders (AppName.v.1.0.Test, AppName.v.1.1.Test, AppName.v.2.0.Test, etc.). I want to be able to right-click on a folder, extract the version # string, and then run a PowerShell script using that version # string as a parameter.

  • I know how to create custom context menu items, so no need to cover that. So for a complete solution, just assume I'm creating a button item in a custom toolbar;
  • I know I can just parse the path string inside the PowerShell script, but I want to learn how to do some advanced customization inside DOpus itself;
  • I envision that I'll need to create a custom Javascript command (please, no Visual Basic!!), and then feed the {filepath$} argument to that and then feed the return value to the PowerShell script, but that's where I am a bit stuck.

Also, I know how to create and use scripts inside the File Rename tool, but I want to avoid using that path. If I have to launch a tool to do it, then it's quicker and easier just to launch a PowerShell window. :grinning: My goal here is to process the selected folder with just a right-click of the mouse.

On the Opus side the way to do this is with a script which applies a regex and then launches your PowerShell script with the appropriate command line.

If you add this script to a button (make sure you set the type to Script function) it will launch a DOS prompt and echo out the version number from the folder name.

function OnClick(clickData) {
	if (clickData.func.command.filecount > 0) {
		for (var fileEnum = new Enumerator(clickData.func.command.files); !fileEnum.atEnd(); fileEnum.moveNext()) {
			if (fileEnum.item().is_dir) {
				var re = RegExp("^.*\\.(v\\.\\d+\\.\\d+)");
				var ver = re.exec(fileEnum.item().name);
				if (ver != null) {
					var cmd = DOpus.Create.Command
					cmd.AddLine("echo " + ver[1]);
					cmd.AddLine("pause");
					cmd.SetType("msdos");
					cmd.Run();
				}
			}
		}
	}
}

The command that it runs (echo followed by pause) can of course be changed to launch your PowerShell script.

If you want this on the right-click menu for folders you can add it to the context menu for folders from Settings / Filetypes / All folders. Note that when adding scripts to filetype commands you need to include a line specifying the script type at the top (@script jscript).

Nice! I actually kind of figured it out this afternoon at work, but I didn't have my forum login info there so couldn't reply.

Your script is a lot sleeker and more efficient than mine, though! I guess that's to be expected given the guru you are. :sunglasses:

You guys made it really easy now (compared to previous versions when I tried this before) by 1.) adding the 'Script' pane directly there in the button edit window, and 2.) all the "default" code when starting a new script. That really helped! A belated thanks for adding all that.

Also, it was wicked cool being able to write and debug the script "live" on-the-fly. Made it a lot easier to test out the various object methods.

Anyway, again, thanks a million! Over the course of time, this is going to save me weeks of work. :smiley:

2 Likes