Hi. I have developed the script below. It was based on this one by lxp: https://resource.dopus.com/t/duplicate-and-rename/39142. When it's complete, I'll share it in the working scripts section. The language is JScript.
Desired Functionality
- User selects a file.
- User clicks a toolbar button.
- Script checks: just 1 file is selected.
- Script checks: selected file's filename contains a version number that matches 1of 3 possible regex patterns.
- If those conditions are met, then...
- Script creates a new file which is exact copy of source file.
- New file's filename is same as source file, except that rightmost section of its file version is incremented by 1.
- Source file is deselected in the lister.
- Newly created file is selected in the lister.
- User can open / execute new file by pressing "enter".
[?] QUESTION 1
All steps above are working, except for step 9.
How do I achieve step 9?
That is, what is the JScript command to select (or ?set focus to) the newly created file in the lister?
In the script, the variable that holds the filename is "newName".
This doesn't work:
select newName;
Do I need to specify the full path, or indicate that the file is in the active lister, or something?
[?] QUESTION 2
Can I change function name and parameter?
Or is one or both of those a dopus or jscript command or keyword?
I copied them both from the script by lxp (above).
Original:
function OnClick(clickData)
Desired:
function CopyFileIncrementVersionNumber(sourceFile)
From experimentation, it seems that it's OK to change the parameter name "clickData", but not the function name "OnClick".
Full script below. Probably more whitespace and longer variable names than some of you would like!
function OnClick(clickData)
{
var cmd;
var tab;
var item;
var stem;
var ext;
var textPart01;
var textPart02;
var versionPart01;
var versionPart02;
var versionPart03;
var versionPart01Pad;
var versionPart02Pad;
var versionPart03Pad;
var regEx1;
var regEx2;
var regEx3;
var matched1;
var matched2;
var matched3;
var createfile;
var newName;
var cmdLine;
cmd = clickData.func.command;
tab = clickData.func.sourcetab;
cmd.deselect = false;
cmd.ClearFiles();
if (tab.selected_files.count != 1)
return;
item = tab.selected_files(0);
stem = item.name_stem;
ext = item.ext;
createfile = false;
// Version Number Format: "(v01)"
regEx1 = /^(.*)\(v(\d+)\)(.*)$/;
matched1 = stem.match(regEx1);
if (matched1)
{
createfile = true;
textPart01 = matched1[1];
textPart02 = matched1[3];
versionPart01 = matched1[2];
versionPart01Pad = versionPart01.length;
// next line: may get data type error if was string now integer
versionPart01 = parseInt(versionPart01,10) + 1;
versionPart01 = ZeroPad(versionPart01,versionPart01Pad);
newName = textPart01 + '(v' + versionPart01 + ')' + textPart02 + ext;
}
// Version Number Format: "(v01.01)"
regEx2 = /^(.*)\(v(\d+).(\d+)\)(.*)$/;
matched2 = stem.match(regEx2);
if (matched2)
{
createfile = true;
textPart01 = matched2[1];
textPart02 = matched2[4];
versionPart01 = matched2[2];
versionPart02 = matched2[3];
versionPart02Pad = versionPart02.length
// next line: may get data type error if was string now integer
versionPart02 = parseInt(versionPart02,10) + 1;
versionPart02 = ZeroPad(versionPart02,versionPart02Pad);
newName = textPart01 + '(v' + versionPart01 + '.' + versionPart02 + ')' + textPart02 + ext;
}
// Version Number Format: "(v01.01.01)"
regEx3 = /^(.*)\(v(\d+).(\d+).(\d+)\)(.*)$/;
matched3 = stem.match(regEx3);
if (matched3)
{
createfile = true;
textPart01 = matched3[1];
textPart02 = matched3[5];
versionPart01 = matched3[2];
versionPart02 = matched3[3];
versionPart03 = matched3[4];
versionPart03Pad = versionPart03.length
// next line: may get data type error if was string, but now integer
versionPart03 = parseInt(versionPart03,10) + 1;
versionPart03 = ZeroPad(versionPart03,versionPart03Pad);
newName = textPart01 + '(v' + versionPart01 + '.' + versionPart02 + '.' + versionPart03 + ')' + textPart02 + ext;
}
if (!createfile)
return;
cmdLine = 'Copy DUPLICATE FILE="' + item + '" AS="' + newName + '"';
cmd.RunCommand(cmdLine);
}
function ZeroPad(numberString, digitCount)
{
numberString = numberString + "";
while(numberString.length < digitCount)
{
numberString = "0" + numberString;
}
return numberString;
}