How to implement a rename script in a button

Hi there

I have a couple of rename scripts that work from within the rename dialog.
However when I put them in a button they do not work.
I have read this post but am not clear about certain details.
I thought it might be helpful to others (as well as myself) to create a fresh post with a very simple example as shown:

@script vbscript
Option Explicit
Function OnGetNewName ( ByRef GetNewNameData )
	OnGetNewName="Test_" & GetNewNameData.item.name
End Function

As mentioned I have this working from within the rename dialog.
From within a button I cannto get it to work.
Below is an image of the button I have created.
Grateful for any help with this as always...

I believe that the GetNewName object is only available within the Advanced Rename dialog. The help file entry for Rename Scripts says..

This is by far the most powerful, but also most complicated, feature of the Advanced Rename dialog.

From a button script you can simply construct and execute a Rename command against selected items.

If you want to use a Rename script (as opposed to a normal/general button script) in a button, the button has to run some kind of Rename command before the script part:

RENAME FROM="*" TO="*"
@script vbscript
Option Explicit
Function OnGetNewName ( ByRef GetNewNameData )
	OnGetNewName="Test_" & GetNewNameData.item.name
End Function

Alternatively, you could save what you have in the Rename dialog as a Rename Preset, then have a simple one-line button that runs the Rename command with that preset, without any script code in the button.

As a third alternative, you could have a normal/general script in the button which renames files directly using script code, without the Rename command or rename scripts being involved at all.

Which of the three is best will depend on the situation. They all have their uses.

Thank you @Leo and @aussieboykie.
I would like to pursue the use of a rename script in a button in this post if that is ok.
I have added the required RENAME FROM="*" TO="*" to the top of the button script but no change in effect.
Maybe @Aussieboykie you are right and the GetNewName object is not avalable within a button?
Grateful for any further help

You should be able to use it in a button the same as in the Rename dialog.

But you'll need to tell us more than "it doesn't work" for us to be able to help. :slight_smile: What are you trying to do exactly? What is selected when you click the button? What happens and what do you expect instead?

Sorry for delay (I dont want to stop the momentum :slight_smile:) had trouble getting this video up

Change the Function drop-down to Standard Function, not Script Function.

More discussion here: Can't have embedded rename script in Script Function mode

Thanks Leo. I have changed the function drop down to > Standard function (Opus or external) and go the result below.

Remove the first line.

Here is a very simple button rename example using JScript. You could do the same with VBScript.

function OnClick(d){ var f = d.func; var src = f.sourcetab; var cmd = f.command; var cmdstring = "Rename FROM * TO Test_*"; cmd.runcommand(cmdstring); }

Eureka! It worked!
Thank you @Leo. Thank you also @aussieboykie I am havng to do other stuff in Java so that will likely become useful....
This is the final working setup below.
The button function pulldown menu must be set to Standard Function (Opus or external

RENAME FROM="*" TO="*"
@script vbscript
Option Explicit
Function OnGetNewName ( ByRef GetNewNameData )
	OnGetNewName="Test_" & GetNewNameData.item.name
End Function

Sorry another question....
I have noticed when i run this script it appears to work on all files in the directoty, regardless of what is selected when the script is run.
Is this something to do with the first line?
I sit possible to restrict the renaming operation to those files selected?
Many thanks....

Compare the FROM and PATTERN arguments:

https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Rename.htm

It seems that the line
RENAME FROM="*" TO="*"
is an implementation of the RENAME raw command using pattern matching syntax.
Looking at the relevant help file I am not sure how to specificy only slected files.
In fact the raw command help FROM command argument notes refer to renaming "the files specified on the command line, rather than those selected in the source file display."
It is precisely the selected files I wish to act on.
Hope this makes sense....?

Use PATTERN instead of FROM.

RENAME PATTERN="*" TO="*"