Skip to content

Instantly share code, notes, and snippets.

@markekraus
Created February 1, 2022 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markekraus/8cbe672ac4177b4843478d5ec8fe5f8d to your computer and use it in GitHub Desktop.
Save markekraus/8cbe672ac4177b4843478d5ec8fe5f8d to your computer and use it in GitHub Desktop.
xEdit script that will change prefixes or suffixes on the EditorID field of every selected record.
{
This script will change prefixes or suffixes on the EditorID field
of every selected record.
Doesn't modify records that already have the correct prefix/suffix.
}
unit UserScript;
var
DoPrepend: boolean;
oldFix: string;
newFix: string;
function Initialize: integer;
var
i: integer;
begin
Result := 0;
// ask for prefix or suffix mode
i := MessageDlg('Prepend [YES] or append [NO] to Editor ID?', mtConfirmation, [mbYes, mbNo, mbCancel], 0);
if i = mrYes then DoPrepend := true else
if i = mrNo then DoPrepend := false else begin
Result := 1;
Exit;
end;
// ask for old prefix/suffix
if not InputQuery('Enter', 'Old Prefix/suffix', oldFix) then begin
Result := 2;
Exit;
end;
// ask for new prefix/suffix
if not InputQuery('Enter', 'New Prefix/suffix', newFix) then begin
Result := 2;
Exit;
end;
end;
function Process(e: IInterface): integer;
var
elEditorID: IInterface;
regexp: TPerlRegEx;
begin
elEditorID := ElementBySignature(e, 'EDID');
if Assigned(elEditorID) then
if DoPrepend then begin
regexp := TPerlRegEx.Create;
regexp.Subject := GetEditValue(elEditorID);
regexp.RegEx := '^(' + oldFix +')(.*)$';
if regexp.MatchAgain then begin
AddMessage('Changing ' + Name(e) + ' EditorID from "' + GetEditValue(elEditorID) +'" to "' + newFix + regexp.Groups[2] + '"');
SetEditValue(elEditorID, newFix + regexp.Groups[2]);
end
end
else begin
regexp := TPerlRegEx.Create;
regexp.Subject := GetEditValue(elEditorID);
regexp.RegEx := '^(.*)(' + oldFix +')$';
if regexp.MatchAgain then begin
AddMessage('Changing ' + Name(e) + ' EditorID from "' + GetEditValue(elEditorID) +'" to "' + regexp.Groups[1] + newFix + '"');
SetEditValue(elEditorID, regexp.Groups[1] + newFix);
end
end;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment