How to run this Python script file in UltraEdit on the active file for incremental replace with undo after replacement.
- ultraedit_incremental_replace.zip (5.79 KiB) 0

Code: Select all
@echo off
if "%~1" == "" exit /B
set "TempFileName=%TEMP%\%~nx1"
copy %1 "%TempFileName%" >nul
python.exe D:\FOR_TEST\ultraedit_incremental_replace.py "%TempFileName%"
type "%TempFileName%"
del "%TempFileName%"Code: Select all
C:\Windows\System32\cmd.exe /D /C D:\FOR_TEST\ultraedit_incremental_replace.cmd "%f"Code: Select all
python ultraedit_incremental_replace_v3.py --stdin --stdout < 12_034AR_ch0.xhtml > output.xhtmlCode: Select all
if (UltraEdit.document.length > 0) // Is any file opened?
{
// Define environment for this script.
UltraEdit.insertMode();
// In UltraEdit for Windows the command columnModeOff is a method of the UltraEdit object.
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
// In UltraEdit for Linux/Mac the command columnModeOff is a method of the UltraEdit document object.
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
// Save the active file.
UltraEdit.save();
// Delete the entire file content.
UltraEdit.activeDocument.selectAll();
UltraEdit.activeDocument.deleteText();
// Run user tool not saving active file and inserting text into the empty active file.
UltraEdit.runTool("Incremental Replace");
// Move caret to top of the file.
UltraEdit.activeDocument.top();
}Code: Select all
InsertMode
ColumnModeOff
Save
SelectAll
Delete
RunTool "Incremental Replace"
TopCode: Select all
InsertMode
ColumnModeOff
RunTool "Incremental Replace"
SelectAll
Clipboard 9
Copy
CloseFile NoSave
SelectAll
Paste
ClearClipboard
Clipboard 0
TopCode: Select all
if (UltraEdit.document.length > 0) // Is any file opened?
{
// Define environment for this script.
UltraEdit.insertMode();
// In UltraEdit for Windows the command columnModeOff is a method of the UltraEdit object.
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
// In UltraEdit for Linux/Mac the command columnModeOff is a method of the UltraEdit document object.
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
var nDocIndex = UltraEdit.activeDocumentIdx;
// Run user tool saving active file and capturing the output to a new file.
UltraEdit.runTool("Incremental Replace");
// Copy entire captured output to user clipboard 9.
UltraEdit.activeDocument.selectAll();
UltraEdit.selectClipboard(9);
UltraEdit.activeDocument.copy();
// Close the new file with the captured output without saving it.
UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
// Make the former active file active again.
UltraEdit.document[nDocIndex].setActive();
// Replace the entire file content by captured output.
UltraEdit.activeDocument.selectAll();
UltraEdit.activeDocument.paste();
// Clear the user clipboard 9 and make Windows clipboard active.
UltraEdit.clearClipboard();
UltraEdit.selectClipboard(0);
// Move caret to top of the file.
UltraEdit.activeDocument.top();
}Code: Select all
if (UltraEdit.document.length > 0) // Is any file opened?
{
var sFullTempFileName = "C:\\User\\UserName\\AppData\\Local\\Temp\\IncrementalReplace.tmp";
// Define environment for this script.
UltraEdit.insertMode();
// In UltraEdit for Windows the command columnModeOff is a method of the UltraEdit object.
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
// In UltraEdit for Linux/Mac the command columnModeOff is a method of the UltraEdit document object.
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
// Get document index of the active file.
var nDocIndex = UltraEdit.activeDocumentIdx;
var bReplaceAll = false;
// If there is no selection in the active file, select everything in the active file.
if (!UltraEdit.activeDocument.isSel())
{
UltraEdit.activeDocument.selectAll();
bReplaceAll = true;
}
// Copy the current selection to user clipboard 9.
UltraEdit.selectClipboard(9);
UltraEdit.activeDocument.copy();
// Create a new file and make sure it is using UTF-8 encoding with DOS/Windows line endings.
// The last three lines of this code block can be removed if a new file is created according
// to UltraEdit configuration by default as UTF-8 encoded file with DOS/Windows line endings.
UltraEdit.newFile();
UltraEdit.activeDocument.unixMacToDos();
UltraEdit.activeDocument.unicodeToASCII();
UltraEdit.activeDocument.ASCIIToUTF8();
// Paste from user clipboard 9, save as temporary file and close the file.
UltraEdit.activeDocument.paste();
UltraEdit.saveAs(sFullTempFileName);
UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
// Run user tool NOT saving active file and capturing the output to a new file.
UltraEdit.runTool("Incremental Replace");
// Copy entire captured output to user clipboard 9.
UltraEdit.activeDocument.selectAll();
UltraEdit.activeDocument.copy();
// Close the new file with the captured output without saving it.
UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
// Make the former active file active again.
UltraEdit.document[nDocIndex].setActive();
// Replace the current selection by captured output.
UltraEdit.activeDocument.paste();
// Clear the user clipboard 9 and make Windows clipboard active.
UltraEdit.clearClipboard();
UltraEdit.selectClipboard(0);
if(bReplaceAll)
{
// Move caret to top of the file.
UltraEdit.activeDocument.top();
}
}
Code: Select all
// @Engine=WebView2
if (UltraEdit.document.length > 0) // Is any file opened?
{
var sFullTempFileName = UltraEdit.getEnvironmentVariable("TEMP") + "\\IncrementalReplace.tmp";
Code: Select all
python.exe D:\FOR_TEST\ultraedit_incremental_replace.py "%Env:TEMP\IncrementalReplace.tmp"Code: Select all
%Env:SystemRoot\System32\cmd.exe /D /C python.exe D:\FOR_TEST\ultraedit_incremental_replace.py "%Env:TEMP\IncrementalReplace.tmp" & del "%Env:TEMP\IncrementalReplace.tmp"Code: Select all
// Ultimate Incremental Replace Script v9.0
// Robust temp file handling with cleanup
if (UltraEdit.document.length > 0) {
// Setup environment
UltraEdit.insertMode();
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
var activeDoc = UltraEdit.activeDocument;
var isSelection = activeDoc.isSel();
var contentToProcess = "";
var nDocIndex = UltraEdit.activeDocumentIdx;
// 1. Get content to process (selection or entire buffer)
if (isSelection) {
contentToProcess = activeDoc.selection;
} else {
activeDoc.selectAll();
contentToProcess = activeDoc.selection;
activeDoc.cancelSelect(); // Deselect after getting content
}
// 2. Create a new document in UltraEdit and write the content to it.
UltraEdit.newFile();
UltraEdit.activeDocument.unixMacToDos();
UltraEdit.activeDocument.unicodeToASCII();
UltraEdit.activeDocument.ASCIIToUTF8();
UltraEdit.activeDocument.write(contentToProcess);
// Define the temporary file path. Ensure c:\temp exists.
var tempDir = "c:\\temp\\";
var tempFileName = "IncrementalReplace" + ".tmp";
var tempPath = tempDir + tempFileName;
// 3. Save this new document to our temporary file path.
try {
UltraEdit.saveAs(tempPath);
} catch(e) {
// Fallback to document directory
tempPath = UltraEdit.document[nDocIndex].path.substring(0,
UltraEdit.document[nDocIndex].path.lastIndexOf("\\") + 1) +
tempFileName;
UltraEdit.saveAs(tempPath);
}
// 4. Run the User Tool.
// Because our temporary file is now the active document, "%f" will correctly
// expand to "C:\Temp\IncrementalReplace.tmp". The tool will then process
// and delete this file automatically.
UltraEdit.runTool("Incremental Replace");
// 5. The tool's output is now in a new, active document. Copy it.
// The tool's output is now in a new, active document.
// Copy the entire result to a user clipboard.
UltraEdit.activeDocument.selectAll();
UltraEdit.selectClipboard(9);
UltraEdit.activeDocument.copy();
// 6. Close the output file without saving.
// Close the output file and the temporary input file without saving.
var outputPath = UltraEdit.activeDocument.path;
// 7. Close files
UltraEdit.closeFile(outputPath, 2); // 2 = No Save
UltraEdit.closeFile(tempPath, 2); // 2 = No Save
// 8. Restore original document
UltraEdit.document[nDocIndex].setActive();
// 9. Replace content (Paste the result back, replacing the original selection or the whole file)
if (isSelection) {
UltraEdit.activeDocument.paste();
} else {
UltraEdit.activeDocument.selectAll();
UltraEdit.activeDocument.paste();
UltraEdit.activeDocument.top();
}
// 10. Clear the user clipboard and reset to the system clipboard
UltraEdit.clearClipboard();
UltraEdit.selectClipboard(0);
}
else {
UltraEdit.messageBox("No document is open!");
}
Code: Select all
Command Tab:
Menu item name: Incremental Replace
Command line: %Env:SystemRoot\System32\cmd.exe /D /C python "PATH\TO\MY\incremental_replace_for_ultraedit_v8.py" --stdout "%f" & del "%f"
Working directory: %p (or leave blank)
Options Tab:
Program type: DOS program ✓
Save active file: Unchecked [emoji unicode-emoji="274c"]274c[/emoji]
Save all files first: Unchecked [emoji unicode-emoji="274c"]274c[/emoji]
Output Tab:
Command output (DOS commands):
Append to existing
Replace existing
Create new file ✓
Output to list box
Show DOS box: Unchecked [emoji unicode-emoji="274c"]274c[/emoji]
Capture output: Checked ✓
Clear output before run: Unchecked [emoji unicode-emoji="274c"]274c[/emoji]
Replace selected text with:
No replace ✓
Captured output
Clipboard
Handle output as:
ASSI
UTF-8 ✓
UTF-16