Tapatalk

How to run Python script file in UltraEdit?

How to run Python script file in UltraEdit?

681
Advanced UserAdvanced User
681

PostSep 03, 2025#1

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

6,824625
Grand MasterGrand Master
6,824625

PostSep 04, 2025#2

How do you run this Python script file from within a command prompt window?

Configure a user tool in UltraEdit which runs python.exe with its fully qualified file name with the options used by you also in the command prompt window and with "%f" as placeholder for the fully qualified file name of active (Python) script file in UltraEdit. The working directory in the tool configuration should be defined with just %p for using the directory of the active (Python) script file in UltraEdit as current working directory for python.exe interpreting the Python script file.

The other options for the user tool should be configured to your requirements. See in help of UltraEdit the page Configure tools command (Advanced tab/User tools dropdown). I recommend to capture the output of python.exe to the output window to see standard output and standard error output by python.exe during script execution in the output window of UltraEdit after python.exe finished the script execution.

See also the forum topic: How to run Python programs from UltraEdit?

681
Advanced UserAdvanced User
681

PostSep 05, 2025#3

Hi Mofi,
First off, thank you for the excellent guidance. The Python tool integration is now working as intended.
I have a follow-up question regarding how UltraEdit handles the changes. The script executes and modifies the open file, and UltraEdit correctly detects the external change. The problem is that once I accept the modification by clicking "Yes" on the prompt, the entire undo history for that action is lost.
My goal is to be able to run the script and then use the standard undo command to reverse the changes if needed. Do you know if this is possible? I appreciate any insights you might have.

ultraedit_tool_config.png (61.47KiB)

ultraedit_file_changed.png (24.54KiB)

6,824625
Grand MasterGrand Master
6,824625

PostSep 05, 2025#4

That is not possible. UltraEdit can record only changes on a file opened in UltraEdit as long as being modified only by UltraEdit. If a file currently opened in UltraEdit using a temporary file is modified by another process like Python, and UltraEdit detects the original file modification, and the user confirms the reload of the original file modified outside, UltraEdit closes and deletes the currently used temporary file which also results in clearing the undo history. Then a new temporary file is created as copy of the original file, and this new temporary file is displayed in the document window of the file.

The undo history recorded by UltraEdit cannot be used anymore if another application modifies the original file. Think about an undo record like string XYZ (three characters) inserted at byte offset 16332 of file test.xhtml. Then another application makes a modification of the file test.xhtml which, for example, results in byte offset 16332 is becoming byte offset 17498 and the string XYZ is removed by the other application. When you could use now the undo record of UltraEdit on the file contents created by another application, the undo action would delete three characters from byte offset 16332 to byte offset 16334 to undo the insertion of string XYZ although the reloaded original file does not contain anymore the string XYZ at the byte offset 16332 at all, but something completely different.

It looks like you think it would be possible to add to the undo history of active file the entire content of the currently used temporary file, then close and delete the currently used temporary file, create a new temporary file as copy of the original file modified by another process, and then open that new temporary file and display it in the document window of that file. Yes, that could be done for small files, but not for very large files which cannot be loaded completely into the RAM of the used machine. I think, UltraEdit does not support an automatic mechanism to back up the current contents of a file in RAM as an undo record before reloading a file modified by another process while UltraEdit is working with a temporary file of the original file.

But it is of course possible that a backup of the original file is made before it is modified by another application executed with a user tool from within UltraEdit. An UltraEdit macro or script can be used to create a backup of the active file before running the user tool also from within the macro/script. In your use case could be also the Python script modified to make a backup of the file before doing the replacements on the file of which file name is passed to the Python script. Well, if the active file is saved automatically by UltraEdit by checking the user tool option Save active file on tab Options and the backup feature of UltraEdit is in general enabled, then the modified file can be reloaded in UltraEdit and if the Python result is not as expected, the backup file created by UltraEdit on save before running Python could be used to restore the initial file contents before running Python.

What you want could be achieved by doing the following:
  1. The Python script is written to output the contents of the file with the replacements to standard output instead of modifying the original file of which file name is passed to the Python script as first argument.
  2. The user tool is configured to capture the output of Python and replace the current selection by the captured output.
  3. There is always selected the entire active file before the user tool is executed with UltraEdit saving the active file and then executing the command line of the user tool.
In this case UltraEdit itself replaces the entire content of the active file with the new content which is output by Python and makes an appropriate undo record. That makes it possible to get the original file content back by pressing Ctrl+Z or using the command Revert to saved.

If you do not want to modify the Python script, there could be used a batch file to get the desired behavior. Content of the batch file:

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%"
The command line of the user tool is modified to:

Code: Select all

C:\Windows\System32\cmd.exe /D /C D:\FOR_TEST\ultraedit_incremental_replace.cmd "%f"
The other user tool options on tab Command are kept.
On the tab Options must additionally be checked the option Save active file.
On the tab Output must be checked the option Capture output and selected the option Captured output for Replace selected text with:.

Now you just must press Ctrl+A to select the entire active file contents and then run the user tool. The active file content is replaced by UltraEdit itself by whatever the Python script file produced. And this modification done by UltraEdit according to output of the Python script can be undone by pressing Ctrl+Z.

If you forget to select the entire file content first before running the user tool, the captured output is inserted at the current position of the caret in the active file. That does not look good, but you can easily undo that wrong insert instead of entire contents replacement by pressing Ctrl+Z and then press Ctrl+A and run the user tool once again.

It is of course also possible that the batch file output is captured by UltraEdit and written always into a new file instead of replacing something in the active file. Then you can decide what to do with the new file: use it further as replacement of the active file or discard it without saving it.

681
Advanced UserAdvanced User
681

PostSep 08, 2025#5

Hello Mofi,

First, thank you so much for your detailed and insightful reply to my original post. Your suggestion to use a Python script with the Capture output feature to enable undo functionality was exactly what I needed, and it put me on the right track.

I have successfully implemented the script, but I've hit a strange and persistent problem that only occurs with larger files (my test file is a 68 KB XHTML file). I'm hoping you might have seen this before.

The problem:
When I run the tool on a large file with all text selected, the replacement is always incomplete. It correctly processes the beginning of the file, but the output is truncated, leaving the bottom part of the original file untouched. If I run the tool with no text selected, the full, correct output is inserted at the top of the file, but it doesn't replace the existing content.

The key evidence:
Here is what makes this so confusing: the Python script itself works perfectly.
If I run the script manually from a standard Windows command prompt, it processes the entire file and generates a complete, correct output file every single time.

This command works flawlessly:

Code: Select all

python ultraedit_incremental_replace_v3.py --stdin --stdout < 12_034AR_ch0.xhtml > output.xhtml
The problem only happens when the exact same command line is executed via an UltraEdit user tool. I have tried two different tool configurations, and both fail in the same way.

Configuration 1: Direct Python call

This uses the original Python script (v3) that reads from stdin and writes to stdout.
  • Python Script: ultraedit_incremental_replace_v3.py
  • Command Line: python D:\FOR_TEST\ultraedit_incremental_replace_v3.py --stdout "%f"
  • Options: Save active file checked.
  • Output: Capture output, Replace selected text with: Captured output, Handle as: UTF-8.
  • Result: Truncated output.
Configuration 2: "Smart" batch file with temp files

Based on my testing, I developed a more robust method using a batch file to manage temporary files, hoping to avoid any potential pipe issues. This uses a modified Python script (v4) that takes file paths as arguments.
  • Python Script: ultraedit_incremental_replace_v4.py (reads from file path arg 1, writes to file path arg 2)
  • Batch File: ultraedit_incremental_replace_v4.cmd (runs the v4 script with temp files, then uses type to print the result to stdout)
  • Command Line: "D:\FOR_TEST\ultraedit_incremental_replace_v4.cmd" "%f"
  • Options: Save active file checked.
  • Output: Capture output, Replace selected text with: Captured output, Handle as: UTF-8.
  • Result: Still the exact same truncated output.
My question:

I am completely stumped as to why a process that works perfectly from the command line fails consistently when launched by UltraEdit's user tool feature. It seems to be related to how UltraEdit captures a large stream of output.

Do you have any insights into this behavior? Is there a known limitation, a timing issue, or perhaps a different way to configure the tool that would make the Capture output feature more reliable for larger data sets?

I have all the scripts, the batch file, and the sample data ready to share if that would help.

Thank you again for any further advice you can offer.
FOR_TEST.zip (233.39 KiB)   0

6,824625
Grand MasterGrand Master
6,824625

PostSep 09, 2025#6

You are right. When the file and therefore the selection is larger than some KB, UltraEdit inserts the entire captured output, but does not replace the entire selection. This is a bug of UltraEdit which I could reproduce with version 2025.0.0.39. This bug should be reported by you to UltraEdit support by email for getting it fixed in a future version of UltraEdit.

A workaround for this UltraEdit bug is using an UltraEdit script which first saves the active file, next deletes the entire content of the active file (first undo record) and then runs the tool with capturing the entire output and inserting it into the now empty file (second undo record). That works if in the user tool configuration is not checked the option Save active file as the file is empty on running the user tool.

The script file is:

Code: 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();
}
An UltraEdit macro with the following macro commands could be also used as workaround for the UltraEdit bug of not replacing the entire selection.

Code: Select all

InsertMode
ColumnModeOff
Save
SelectAll
Delete
RunTool "Incremental Replace"
Top
Another solution is using in the user tool configuration Save active file but change on tab Output the configuration to Create new file for the Command output and No replace for Replace selected text with and using the following macro:

Code: Select all

InsertMode
ColumnModeOff
RunTool "Incremental Replace"
SelectAll
Clipboard 9
Copy
CloseFile NoSave
SelectAll
Paste
ClearClipboard
Clipboard 0
Top
The captured output written into a new file is selected and copied to user clipboard 9 by the macro. Then the new file with the captured output is closed without saving it. The former active file is now active again where the entire file content is selected and replaced by user clipboard 9 content. User clipboard 9 is finally cleared and the Windows clipboard is activated before moving the caret to top. The advantage of this solution is that just one undo record is created during the macro execution. There must be selected the option previously active tab for the setting after current tab is closed, move to at Advanced - Settings or Configuration - Application layout - File tabs - Miscellaneous.

The same could be done also with an UltraEdit script:

Code: 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();
}

681
Advanced UserAdvanced User
681

PostSep 09, 2025#7

Hello Mofi,

Thank you so much for your incredible help and for diagnosing the issue so precisely. You were absolutely right – it was a bug in UltraEdit's handling of large selections with Capture output.

I have implemented your suggested workaround using the UltraEdit script that captures the output to a new file and then copies it back. It works perfectly!

The process is now completely reliable with files of any size, and it still provides the single undo step that I needed. This is the exact solution I was hoping for. Your expertise is truly invaluable to this community, and I am extremely grateful for the time you took to help me solve this complex problem.

Thank you again!

PostSep 12, 2025#8

Hello Mofi,

I wanted to follow up with a huge thank you. Your script-based workaround for the Capture output bug was perfect, and the tool is now working reliably for full, saved files. It has been a tremendous help!

Now that the core functionality is solid, I am hoping to make the tool even more seamless and integrated, like a native feature. This has led me to a new design challenge that I am not sure how to approach correctly.

My goal is to modify the workflow so that it can handle the following scenarios:
  1. Process Selected Text: If I have text selected in a document, the Python script should only receive and process that specific selection.
  2. Process Full Buffer: If there is no selection, the script should process the entire content of the active document.
  3. Support Unsaved Files: The tool should work perfectly even if the active file is an unsaved "untitled" document.
The current solution relies on the user tool's "%f" parameter, which passes the path of the saved active file. This is where I'm stuck, as this approach won't work for an unsaved buffer or for passing just a portion of a file's content (a selection).

This leads to my specific question: "What is the recommended UltraEdit scripting technique for managing temporary files?"

Specifically, how can a script correctly:
  1. Create a temporary file in the user's temp directory?
  2. Write the necessary content to it (selection or full buffer)?
  3. Reliably delete the temporary file after the tool has finished its work?
What would be the recommended UltraEdit scripting approach to solve this? I imagine it involves the master script somehow preparing the input for the tool, but I'm not sure of the best practice for handling the content (especially for unsaved files) and passing it to the Python process.

Any guidance or pointers you could provide would be incredibly helpful.

Thank you again for all your amazing support.

6,824625
Grand MasterGrand Master
6,824625

PostSep 12, 2025#9

The possibility of using an environment variable like TEMP inside an UltraEdit script depends on two criteria:
  1. There must be used UltraEdit with a version ≥ 2024.0 (31.0).
  2. The script must use the external WebView2 engine instead of the built-in SpiderMonkey JavaScript engine.
I do not know if the two criteria are fulfilled by your used version of UltraEdit.

Here is a script which works with UltraEdit for Windows with a version ≥ 17.30 using the built-in SpiderMonkey JavaScript engine. The user of this script must specify at top the fully qualified file name of the temporary file to use like "C:\\User\\UserName\\AppData\\Local\\Temp\\IncrementalReplace.tmp".

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();
   }
}
The top of the UltraEdit script using WebView2 engine and the scripting command UltraEdit.getEnvironmentVariable() introduced with UE/UES v2024.0 would be as follows.

Code: Select all

// @Engine=WebView2
if (UltraEdit.document.length > 0)  // Is any file opened?
{
   var sFullTempFileName = UltraEdit.getEnvironmentVariable("TEMP") + "\\IncrementalReplace.tmp";
The other lines of the UltraEdit script are identical.

There is no UltraEdit scripting command to delete a file (for security reasons). That must be done by the executed user tool. It can be done by the Python script after finishing processing the input file and recognizing the file name IncrementalReplace.tmp (or there is an optional parameter passed to the Python script instructing it to delete the input file as last action) or the command line of the user tool runs the Windows Command Processor for the execution of Python and then deletes the temporary file.

The user tool command line in case of Python script deletes itself the temporary input file as defined in the UltraEdit script file would be:

Code: Select all

python.exe D:\FOR_TEST\ultraedit_incremental_replace.py "%Env:TEMP\IncrementalReplace.tmp"
The user tool command line on Windows Command Processor is executed to run Python and then delete the temporary file would be:

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"
The user tool option Save active file must not be checked in the user tool configuration.

681
Advanced UserAdvanced User
681

PostSep 13, 2025#10

Hello Mofi,

I wanted to post one final update and a huge thank you. Your expert guidance was the key to solving this, and I now have a complete solution that works perfectly on all my test cases.

The tool now seamlessly handles:
  • Processing only selected text.
  • Processing the entire file when there is no selection.
  • Working on both saved and unsaved files.
Most importantly, the final workflow is robust enough to work perfectly on both of my development environments: my older UEStudio v12.20.0.1004 and the newer UltraEdit x64 v27.00.0.22. The key was to use a master JavaScript that relies only on native UltraEdit commands to create the temporary file, which ensures great compatibility. Your cmd.exe command for processing and auto-deleting the temporary file was the brilliant final piece that made the cleanup process so elegant.

For the benefit of the community, here is the final working setup that is confirmed to work on both of these versions.

1. The Final JavaScript (incremental_replace_run_python_tool_v9.js)

This script uses native UltraEdit commands to create a temporary file, making it highly compatible. It requires a folder like c:\temp to be created manually on the user's machine.

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!");
}

2. The Final User Tool Configuration

This configuration uses the cmd.exe method to run the Python script and handle the cleanup automatically. The Python script used is the v8.py version with the modern GUI.

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

I couldn't have solved this without your help. Your knowledge of UltraEdit's scripting and tool capabilities is incredible, and I'm very grateful for your patient and detailed guidance.

Thank you again!
incremental_replace_for_ultraedit_v8.zip (11.18 KiB)   0

1
NewbieNewbie
1

PostDec 09, 2025#11

@Mofi i know this is an older post, but is 
"The user tool is configured to capture the output of Python and replace the current selection by the captured output." the best way to do large batches of find and replace with multiple different variables needed to be found and replaced?

I currently have a process that is incredibly manual and I am looking to write a Python script that will take multiple finds from the document in UltraEdit (maybe 6 or 7) and replace. Goal is to use a YAML and just pass the replaces back. Not sure what would be best practice here.
There are 10+ finds that I would want to replace with blanks on that line, and that's just one use case of the script I'm trying to create.

6,824625
Grand MasterGrand Master
6,824625

PostDec 09, 2025#12

Samir had already a Python script which does more on the file opened in UltraEdit/UEStudio than running a set of finds/replaces and wanted additionally that all done by the Python script on the file can be undone in UltraEdit with the Undo command of UltraEdit. That is the main reason for the unusual approach as described here in this topic.

A Python script or a PowerShell script can be also used to run finds and replaces on active file opened in UltraEdit based on data in a YAML file if it is not necessary that all the replaces done by Python can be undone in UltraEdit with its Undo command. The default is that UltraEdit reloads the file modified by another process started with a user tool from within UltraEdit. But that results in no possibility to undo the changes made by the script interpreter on the file.

However, it is also possible to use an UltraEdit macro or an UltraEdit script to run multiple finds and replaces on active file. In this case UltraEdit itself modifies the active file and for that reason UltraEdit itself records all the changes which can be undone by the user after macro/script finished.

It is in general better to run finds and replaces on a file opened already in UltraEdit with an UltraEdit macro or an UltraEdit script. Both can be coded to support also a variable list of search and replace strings stored in another file.

I posted several macros in the UltraEdit macros forum which get a somehow delimited list of search and replace strings from another file (file 2 or file B also opened in UltraEdit) and run the replaces for each pair of find and replace strings read from the file line by line. UltraEdit macros do not support variables making it a bit more difficult and less efficient to use the list of search and replace strings from a file, but it is in general possible.

UltraEdit scripts are nowadays better for such a task because of an UltraEdit script can load first all the search and replace strings from a file  opened by the script itself or already opened by the user into the memory of the built-in script interpreter and then do the replaces on the active file (or first opened file or the file with file extension ? or …). One user stored in the extra file not only the find and the replace strings, but also the replace options to use, i.e. run the replace case-sensitive or case-insensitive, with match word or without that option, with Perl regular expression engine or a simple text replace without regular expression engine. It is of course also possible to search for a specific string in active file and replace all found occurrences by another string with an automatic incremented number.

I wrote and posted lots of UltraEdit scripts in the UltraEdit scripts forum loading a variable list of search and replace strings from a file of whatever format the user wanted to have for that file which are applied on an opened file in UltraEdit. There are also posted by me lots of UltraEdit scripts doing a find and replace with an automatic by the script incremented number in replace string.

It depends on your requirements regarding to efficiency and the ability to undo the changes all at once or replace by replace if it is better to code an UltraEdit script/macro or use a different script interpreter like Python or PowerShell and let them modify the file currently opened in UltraEdit. Important is also the file size of the files to modify in future with a scripting solution. A file with several GB or even TB can be quickly modified also with an UltraEdit script/macro, but recording the changes is in this case not possible. The replaces are applied on the file data on storage media directly in such a use case with a very large or huge file to modify without the ability to undo the changes (except with making first a backup of the large/huge file which takes also a longer time).