Any chance to know if a rename script is showing a preview?

I build a rename script that changes names based on the content of a text file, selected by the user. The problem happens when the preview is on, it also trigger the dialog for selecting the textfile. Then when it actually the rename happens, opens the dialog again, is there's a way to avoid that? For example, know if the script is running for the preview (no rename is done yet) or is the actual rename. Any though are welcome.
THanks.
THis is the code:

@script jscript

var textfile;
var line;
var linenumber = -1;
var cancel_open = false;
function OnGetNewName (G)
{
    if (cancel_open)
        return true;
    linenumber++;
    DOpus.Output("------------------------");
    DOpus.Output("File:" + G.item);
    DOpus.Output("newname:" + G.newname);
    DOpus.Output(textfile);
    if (!textfile) 
    {
        DOpus.Output("textfile not defined!");
        var item = DOpus.Dlg.Open("Select text file:", G.item.realpath);
        if (!item.result)
        {
            cancel_open = true;
            return true;
        }
        textfile = String(item);
        DOpus.Output("textfile:" + textfile);
        if (!DOpus.FSUtil.Exists(textfile))
        {
            DOpus.Output(textfile + " is not a valid text file!");
            return true;
        };
        item = null;
    };
    if (!line)
    {
    var objStream = new ActiveXObject("ADODB.Stream");
        with (objStream) {
        CharSet = "utf-8";
        Open();
        LoadFromFile(textfile);
        line = ReadText();
        close();}
        objStream = null;
        line = line.replace(/\n{2,}/gi,"\n").replace("\\","/").replace("?","").replace('"',"'").replace(/ +/gi," ");
        line = line.split(/\n+|\r\n+/g)
    }
    if (line[linenumber] == null)
        return true;
    var nuevo_nombre = G.newname.replace(/\[FILE\]/gi, line[linenumber]);
    DOpus.Output(G.item + " renamed to " + nuevo_nombre);
    return (nuevo_nombre + G.item.ext);
}

In the NEWNAME section, wherever the user places [FILE], will be replace with a line of the textfile, based of the number of files renamed.

If you want an interactive UI, it's better to have a script which displays the UI and then calls the Rename command (or whatever is needed), rather than have the Rename command call your script.

I don't understand you, so is there a way to know if a rename script is running for the preview (no rename is done yet) or is the actual rename

No, scripts run by Rename are not told if they are in preview mode or not. Rename scripts aren't expected to show user interface. But other kinds of scripts can, and those other scripts can still rename files.

To explain in more detail:

Close the Rename dialog.

Go to Settings > Customize Toolbars and create a new toolbar button. Set the button to Script Function. You can then write a script which runs in the button. The script can ask the user for a file (or whatever it needs) and process the input it is given. The script can then rename other files, or do whatever it needs to. The script can call the Opus rename command to rename files, or it can use JScript/VBScript's native rename functionality if that is easier.

That is the right way around to do things if you want to show the user a UI and use the information they provide to do a custom rename. You want your script to be in control of Rename, instead of Rename being in control of your script.