Dialog in a rename script

Hi all,

I'm working on a button to rename files, removing all but the first n charachters of the file name. Since I didn't get anywhere using RegEx, I'm going for a script now.
This code works, albeit with a fixed number of characters:

[code]Rename PATTERN * TO * AUTORENAME
@script jscript

var len = 6

function OnGetNewName(data) {
if (len > 0) {
var stem = data.item.name_stem;
stem = stem.substr(0, len);
var newName = stem + data.item.ext;
return newName;
}
}
[/code]

Now I want to add a dialog for entering the number of characters. This value will apply to all files: I want the dialog to show only once, so I add it to the global section before the actual OnGetNewName() function:

[code]Rename PATTERN * TO * AUTORENAME
@script jscript

var len = -1

var dlg = DOpus.Dlg();
dlg.message = "Rename files, removing all but the first n characters.\nEnter number of characters:";
dlg.title = "Leave first charachters";
dlg.buttons = "&Rename|&Cancel";
dlg.icon = "question";
dlg.max = 3;
if (dlg.Show() > 0) {
len = parseInt(dlg.input);
}

function OnGetNewName(data) {
if (len > 0) {
var stem = data.item.name_stem;
stem = stem.substr(0, len);
var newName = stem + data.item.ext;
return newName;
}
}[/code]

The dialog works, but nothing happens afterwards: Apparently, the function is not called at all.
Is it not possible to add a dialog to a rename script like this?
Next, I tried adding the dialog to the OnGetNewName() function and running it only for the first file, using a global flag:

[code]Rename PATTERN * TO * AUTORENAME
@script jscript

var len = 0
var firstFile = true;

function OnGetNewName(data) {
if (firstFile) {
firstFile = false;
var dlg = DOpus.Dlg();
dlg.message = "Rename files, removing all but the first n characters.\nEnter number of characters:";
dlg.title = "Leave first charachters";
dlg.buttons = "&Rename|&Cancel";
dlg.icon = "question";
dlg.max = 3;
if (dlg.Show() > 0) {
len = parseInt(dlg.input);
len = isNaN(len) ? 0 : len;
}
}

if (len > 0) {
    var stem = data.item.name_stem;
    stem = stem.substr(0, len);
    var newName = stem + data.item.ext;
    return newName;
}

}
[/code]

That does what I need, but my question about the dialog before the function remains: What are the rules for the global part of the script?

Thanks,
MartO

RegExp basics: Removing characters from start/end of names, scroll down to "Advanced Bonus: Buttons that ask you how many characters to remove".

Thanks for that, leo, but I already have buttons for removing a certain number of characters. No I want to rename leaving a certain number of characters. As I mentioned, I already tried that and ran into problems. Even with help from jon something didn't work with the nested curly brackets and I decided not to follow that road any further.

Instead, my question pertains to the structure of rename scripts and what code is allowed in the global/initialization part of the button.

Thanks,
MartO

Try this script button:

[code]@script jscript

function OnClick(ClickData)
{
var dlg = ClickData.Func.Dlg;
dlg.message = "Rename files, removing all but the first n characters.\nEnter number of characters:";
dlg.title = "Leave first charachters";
dlg.buttons = "&Rename|&Cancel";
dlg.icon = "question";
dlg.max = 3;
if (dlg.Show() > 0)
{
var len = parseInt(dlg.input);

	if (len > 0)
	{
		var cmd = ClickData.Func.Command;
		cmd.AddLine("Rename AUTORENAME TYPE=dirs  REGEXP PATTERN=\"^(.{" + len + "}).*$\"           TO=\"\\1\"");
		cmd.AddLine("Rename AUTORENAME TYPE=files REGEXP PATTERN=\"^(.{" + len + "}).*(\\.[^.]+)$\" TO=\"\\1\\2\"");
		cmd.Run();
	}
}

}[/code]

Hi leo,

I really appreciate your quick and friendly support...
and your solution is useful and everything...
and it's probably better to go for a 'real' script instead of a rename script anyway...
and I don't mean to be a pain... :wink:

...but that didn't answer my question. In this case I didn't just want to achieve the goal but also to understand why my way was wrong!
There seem to be some restrictions: What kind of code are we allowed to write between...

Rename PATTERN * TO * AUTORENAME @script jscript...and...function OnGetNewName(data) { ...
...? And (possibly even) why?

But if that's too much to ask, then never mind, it's really just of academic interest...

Cheers,
MartO

I am not sure and it would take hours of research to give a definitive answer. Since there's a better way to do the same thing which avoids the problem entirely, it's best to just use that.

Fair enough, thanks!