Is there a way to capture an OnRename event?
When an item is renamed I would like to check if the parent or a sibling has the same name (excluding extension, also possibly starts with that name) and rename that as well.
I can create a script to do the rename and then also check the other files. But I like using the inline rename, and thus looking for an event driven approach.
Does a script to do this already exist?
Leo
May 6, 2019, 11:03pm
2
OnFileOperationComplete exists for this purpose.
There's an example here:
The beta version is out now .
Here's an example script that uses the new event. It will print out (to the script output window - Help / Logs / Script Log) information about rename commands.
option explicit
' fileoptest
' (c) 2018 jpotter
' This is…
1 Like
Nice, will give it ago. Thanks
That worked
This is how I used it if anyone else wants some thing similar.
function OnFileOperationComplete(fileOperationCompleteData)
{
if(fileOperationCompleteData.query)
{
if (fileOperationCompleteData.action == "rename")
{
return fileOperationCompleteData.cmdline.indexOf("SCRIPTARG RenameMatchingExtended:True") == -1;
}
}
else if (fileOperationCompleteData.action == "rename" )
{
for (var e = new Enumerator(fileOperationCompleteData.data); !e.atEnd(); e.moveNext())
{
var oldPath = e.item();
var newName = fileOperationCompleteData.data(oldPath);
DOpus.Output(oldPath + " -> " + newName);
var renamedItem = DOpus.FSUtil.GetItem(oldPath);
var parentItem = DOpus.FSUtil.GetItem(renamedItem.path);
var newNameStem = GetNewNameStem(renamedItem, newName);
var oldNameStem = renamedItem.name_stem;
//Rename sibling and niblings
var folderEnum = DOpus.FSUtil.ReadDir(parentItem, true);
while (!folderEnum.complete)
{
var item = folderEnum.next;
CheckAndRename(item, oldNameStem, newNameStem);
}
//Rename parent
CheckAndRename(parentItem, oldNameStem, newNameStem);
}
}
}
function CheckAndRename(itemToRename, oldNamePart, newNamePart)
{
if(itemToRename.name.indexOf(oldNamePart) != -1)
{
//using regexp might case issues for special chars
var udpatedName = itemToRename.name.replace(oldNamePart, newNamePart);
var cmd = "Rename FROM \"" + itemToRename + "\" TO \"" + udpatedName + "\" SCRIPTARG RenameMatchingExtended:True";
DOpus.Output(cmd);
DOpus.NewCommand.RunCommand(cmd);
}
}
function GetNewNameStem(renamedItem, newName)
{
if(renamedItem.is_dir) {
return newName;
}
else {
var re = new RegExp("^(.*?)(?:\.[^\.]*)?$");
return newName.replace(re, "$1");
}
}
I uploaded the script here
The Dopus Rename command has an option called RENAMEMATCHING.
RENAMEMATCHING
Keeps files with the same stem and different extensions together when renumbering. For example, IMGP1032.JPG and IMGP1032.WAV when renamed would be given the same number. …
1 Like