Geting a Script To Focus on the Source Pane Strategically

I'd like to be able to add a command to my script to move the focus to the source pane after completing it's work.

Set Focus=Source doesn't seem to be working.

I have a script hot-keyed to a button on my mouse that does a conditional rename of a file then moves it from the source pane to the destination pane. The problem is when it's done it doesn't bring the focus back to the source pane.

What am i missing?

Also, if it's not too complex, how to I set the focus in this example to the file below the file moved last?

FILEZ.TXT
FILEA.TXT <<--- I select this file and hit my hotkey
FILEC.TXT <<-- How can i return focus to the row directly below regardless of sort?
FILEF.TXT

Possible?

Thanks for any guidance.

Moving a file from source to destination should not normally change what the source is. You should be able to avoid having to change it back by avoiding whatever is causing it to change in the first place.

What does the script look like currently?

Here it is. The 4th line " Set Focus=Source" is giving me the trouble. Focus stays on the destination pane. Removing @nodeselect seems to have no effect.

@nodeselect 
Rename PATTERN=*.* TO=*.*
Copy Move
Set Focus=Source 

@script jscript 
function OnGetNewName(getNewNameData) {
    var tmpName = getNewNameData.newname_stem;
    var tmpExt = getNewNameData.newname_ext;

    if (tmpName.slice(-7) == ' -elite' ) {
        tmpName = tmpName.substr(0, tmpName.length - 7);
	tmpName += ' -vbkup';
	return tmpName + tmpExt;
    } 
    
    
    if (tmpName.slice(-7) == ' -vbkup')  {
        tmpName = tmpName.substr(0, tmpName.length - 7);
    } else {
        tmpName += ' -vbkup';
    }

    
    return tmpName + tmpExt;
}

That shouldn't change which side is source/dest at all, as far as I can tell.

Are you using a hotkey which is bound to multiple functions? Or are you clicking the mouse button over the destination side when triggering it? It may be switching source/dest sides as a side effect of something completely outside of the script you're looking at.


The script can also be done in a better way.

Let me make sure I understand what it's aiming to do first:

  • Move the selected files from source to destination, while renaming them as follows (preserving file extensions):
    • If the filename ends in -elite, replace that with -vbkup
    • If the filename instead already ends in -vbkup, remove that from the end of the name.
    • If the filename instead ends in neither -elite nor -vbkup, append -vbkup to it.

Is that correct?

The hotkey on my mouse just does a keystroke that is the same as a keyboard shortcut I have set on the script. So I select a file or two (always on the source side), then hit the button on my mouse. The same behavior happens if I just hit the run command when I'm in the script editor.

Yes you have the right summary of what I'm trying to do from a renaming perspective.

If the focus is really not on the source anymore it's very likely a problem outside this script.

After the Copy command the focus is on the first file FILEZ.TXT but it's not selected. It will get selected if cursor down is pressed.

So the difficult part is to memorize FILEC.TXT and select it after the Copy command. For this some JScript is probably needed.

*** update

If you move the rename part of your script into a preset this little snippet might be all you need.

function OnClick(clickData) {
    var cmd = clickData.func.command;
    cmd.deselect = false;

    cmd.RunCommand('Select NEXT=nodeselect');
    cmd.RunCommand('Rename PRESET=TheEliteVbkupPreset');
    cmd.RunCommand('Copy MOVE');
}

Never created a rename script before.

I assume making the preset is in the "Script" button on the rename dialog. not sure how to retrofit the script to work as a rename preset. I tried just copy and pasting it and getting rid of the copy move and focus instruction. When I try to run VBKUPPRESET i get the message Windows cannot find 'Function'.

My rename Script Looks like this:

function OnGetNewName(getNewNameData)
{
	// Add script code here.
	// 
	// Main inputs (all read-only):
	// 
	// - getNewNameData.item:
	//     Object with information about the item being renamed.
	//     e.g. item.name, item.name_stem, item.ext,
	//          item.is_dir, item.size, item.modify,
	//          item.metadata, etc.
	//     item.path is the path to the parent folder.
	//     item.realpath is the full path to the file, including its name,
	//          and with things like Collections and Libraries resolved to
	//          the real directories that they point to.
	// - getNewNameData.oldname_field:
	// - getNewNameData.newname_field:
	//     Content of the "Old Name" and "New Name" fields in the Rename dialog.
	// - getNewNameData.newname:
	// - getNewNameData.newname_stem and newname_stem_m:
	// - getNewNameData.newname_ext  and newname_ext_m:
	//     The proposed new name for the item, based on the non-script
	//     aspects of the Rename dialog. Scripts should usually work from this
	//     rather than item.name, so that they add to the dialog's changes.
	//     newname_ext is the file extension, or an empty string if none.
	//     newname_stem is everything before the file extension.
	//     The *_m versions handle multi-part extensions like ".part1.rar".
	// - getNewNameData.custom:
	//     Contains any custom field values for additional user input.
	// 
	// Return values:
	// 
	// - return true:
	//     Prevents rename.
	//     The proposed getNewNameData.newname is not used.
	// - return false: (Default)
	//     Allows rename.
	//     The proposed getNewNameData.newname is used as-is.
	// - return "string":
	//     Allows rename.
	//     The file's new name is the string the script returns.

@nodeselect 
Rename PATTERN=*.* TO=*.*

@script jscript 
function OnGetNewName(getNewNameData) {
    var tmpName = getNewNameData.newname_stem;
    var tmpExt = getNewNameData.newname_ext;

    if (tmpName.slice(-7) == ' -elite' ) {
        tmpName = tmpName.substr(0, tmpName.length - 7);
	tmpName += ' -vbkup';
	return tmpName + tmpExt;
    } 
    
    
    if (tmpName.slice(-7) == ' -vbkup')  {
        tmpName = tmpName.substr(0, tmpName.length - 7);
    } else {
        tmpName += ' -vbkup';
    }

    
    return tmpName + tmpExt;
}
	
}

and my VBKUPPRESET preset looks like this:

function OnClick(clickData) {
    var cmd = clickData.func.command;
    cmd.deselect = false;

    cmd.RunCommand('Select NEXT=nodeselect');
    cmd.RunCommand('Rename PRESET=VBKUPPRESET');
    cmd.RunCommand('Copy MOVE');
}
    
    return tmpName + tmpExt;

}

You have mismatched { ... } blocks in both your scripts.

Here are your button and the preset. The script needed a bit of fine tuning.

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    cmd.deselect = false;

    cmd.RunCommand('Rename PRESET=TheEliteVbkupPreset');
    cmd.SetFiles(tab.selected);
    cmd.RunCommand('Select NEXT=nodeselect');
    cmd.RunCommand('Copy MOVE');
}
function OnGetNewName(getNewNameData) {
    var tmpName = getNewNameData.newname_stem;
    var tmpExt = getNewNameData.newname_ext;

    if (tmpName.slice(-7) == ' -elite') {
        tmpName = tmpName.substr(0, tmpName.length - 7);
        tmpName += ' -vbkup';
        return tmpName + tmpExt;
    }


    if (tmpName.slice(-7) == ' -vbkup') {
        tmpName = tmpName.substr(0, tmpName.length - 7);
    } else {
        tmpName += ' -vbkup';
    }
    return tmpName + tmpExt;
}

TheEliteVbkupButton.dcf (883 Bytes)
TheEliteVbkupPreset.orp (752 Bytes)


1 Like

It's working! That's so great! I'm learning a lot from this.

The DCF button doesn't show in the list of my user defined commands. It's confusing me a bit how the script is rigged directly to the button. I always thought the button just executed the commands.

I have a few of these to do. Is there any way I can get it to show in the command list so i can duplicate it and work with it?

Just import the .dcf file

(Maybe change the stupid name before so you are not stuck with it forever :wink: )

1 Like

That did the trick! I was able to duplicate everything just like i need.

Opus is SO cool.

Thanks for your awesome help!!! This will save me tons of time.