How do I pass parameters to an UltraEdit script from the command line?

1k views Asked by At

Now an UltraEdit script is executed from the command line with:

uedit64.exe /s="J:\SkyDrive\work\ue-script\newFile.js"

Is it possible to pass parameters to UltraEdit script from command line? And how do I get them in the script?

Maybe like this:

uedit64.exe /s="J:\SkyDrive\work\ue-script\newFile.js" /pars="parameter1=value1,parameter2=value2"

And then get parameter1=value1 and parameter2=value2 in newFile.js.

1

There are 1 answers

0
Mofi On

UltraEdit scripts are executed usually from the command line to reformat one or more text files completely without user interaction and without depending on parameters. Or an UltraEdit script is started manually by a user from within UltraEdit without or with some minimal user interaction using getString and/or getValue. There are many scripting languages and script interpreters for doing something depending on parameters like VBScript, PowerShell, Perl, Python, etc.

It is not possible to specify additional custom parameters for an UltraEdit macro/script on the command line of UltraEdit. The command line arguments are interpreted by uedit64.exe or uedit32.exe and UltraEdit macros and scripts don't have access to arguments list of the executable.

I'm aware of three possibilities to pass strings (parameters) to an UltraEdit script from another process before starting UltraEdit and executing the script:

  1. Via the clipboard, or
  2. via a text file, or
  3. by modifying the script before execution.

1. Pass parameters to an UltraEdit/UEStudio script via the clipboard

The first solution is easy to implement. But it has the big disadvantage that Windows clipboard content is modified on starting and no other process should copy something to the clipboard before the parameters and their values are read by the script. These disadvantages are very problematic if UltraEdit should be executed in the background for executing the script.

The following two commands must be executed on the command line or in a batch file:

echo parameter1=value1,parameter2=value2 | %SystemRoot%\System32\clip.exe
uedit64.exe /fni /s="J:\SkyDrive\work\ue-script\newFile.js"

clip.exe has been available since Windows Vista and Windows Server 2003. But there is no clip.exe on Windows XP. However, clip.exe from Windows Server 2003 can be also used on Windows XP.

It is highly recommended to use /fni (force new instance) on starting UltraEdit for executing a script from the command line when the configuration setting Allow multiple instances is not checked as by default. For an explanation why /fni should be used as the first parameter on the command line on running an UltraEdit macro/script from the command line, read topic Always get error message when running a macro/script via command line parameter (solved) in the UltraEdit forum.

An example script code for reading the parameters from clipboard is:

// Copy content of system (Windows/Mac/Linux) clipboard to a variable.
UltraEdit.selectClipboard(0);
var sParameterList = UltraEdit.clipboardContent;

// For safety check if the first parameter string begins with "parameter1".
if (sParameterList.indexOf("parameter1") == 0)
{
    // Remove trailing withspaces from parameter list
    var sParameterList = sParameterList.replace(/\s+$/,"");

    // Split up the parameters list using comma as delimiter.
    var asParameterList = sParameterList.split(',');

    // For demonstration just open a message box listing the read
    // parameters with their values without splitting them up further.
    var sMessageText = "The parameter";
    if (asParameterList.length > 1)
    {
        sMessageText += "s are:\n";
    }
    else
    {
        sMessageText += " is:\n";
    }
    for (var nParameter = 0; nParameter < asParameterList.length; nParameter++)
    {
        sMessageText += '\n   "' +  asParameterList[nParameter] + '"';
    }
    UltraEdit.messageBox(sMessageText,"List of parameters");
}

2. Pass parameters to UltraEdit/UEStudio script via a text file

In comparison to the first solution the clipboard is not modified by using this solution. However, it must be made sure that the following command lines are not executed at the same time by two processes.

> C:\Temp\ParameterList.tmp echo parameter1=value1,parameter2=value2
start "" /wait uedit64.exe /fni /s="J:\SkyDrive\work\ue-script\newFile.js"
del C:\Temp\ParameterList.tmp

The line output by command ECHO is redirected into text file C:\Temp\ParameterList.tmp, and then UltraEdit is started for running the script in a separate process and batch processing is halted until UltraEdit is exited. Finally, the temporary text file is deleted from the command line.

Example script code for reading the parameters from file with fixed name and path:

// Define the name of the file with the parameters with full path.
// The usage of environment variables in file name is not possible.
var sParameterListFile = "C:\\Temp\\ParameterList.tmp";

// Remember document index of active document which requires UltraEdit for
// Windows v16.00 or UEStudio v10.00. It would be possible to use code to
// get document index of active document on using an even older version of
// UltraEdit or UEStudio.
var nActiveDocIndex = UltraEdit.activeDocumentIdx;

// Open the parameter list file. This file should not be opened already
// before running this script. Otherwise additional code would be needed
// to search first in list of opened files for this file and read the
// parameters from already opened file and keep the file open instead
// of opening it and closing it after reading the first line.
UltraEdit.open(sParameterListFile);

// Test with a case-sensitive string comparison if the file could be really
// opened successfully in which case the parameter list file is the active
// file whose path property is the full name of the file with path.
if (UltraEdit.activeDocument.path == sParameterListFile)
{
    // Define environment for this script.
    UltraEdit.insertMode();
    UltraEdit.columnModeOff();

    // Read from the parameter list file just the first line without
    // the line terminating character(s) and split up the parameters
    // list using comma as delimiter before closing the file.
    UltraEdit.activeDocument.startSelect();
    UltraEdit.activeDocument.key("END");
    UltraEdit.activeDocument.endSelect();
    var asParameterList = UltraEdit.activeDocument.selection.split(',');
    UltraEdit.closeFile(UltraEdit.activeDocument.path,2);

    // For safety check if the first parameter string begins with "parameter1".
    if (asParameterList[0].indexOf("parameter1") == 0)
    {
        // For demonstration just open a message box listing the read
        // parameters with their values without splitting them up further.
        var sMessageText = "The parameter";
        if (asParameterList.length > 1)
        {
            sMessageText += "s are:\n";
        }
        else
        {
            sMessageText += " is:\n";
        }
        for (var nParameter = 0; nParameter < asParameterList.length; nParameter++)
        {
            sMessageText += '\n"' +  asParameterList[nParameter] + '"';
        }
        UltraEdit.messageBox(sMessageText,"List of parameters");
    }

    // Make the previously active document again the active
    // document if there was any document opened before at all.
    if (nActiveDocIndex >= 0)
    {
        UltraEdit.document[nActiveDocIndex].setActive();
    }
}

For usage of a dynamic file name, the file name must be specified as the second parameter to open this file by UltraEdit before running the script and the script reads the parameters from the first opened file.

> "%TEMP%\Any File Name.txt" echo parameter1=value1,parameter2=value2
start "" /wait uedit64.exe /fni "%TEMP%\Any File Name.txt" /s="J:\SkyDrive\work\ue-script\newFile.js"
del "%TEMP%\Any File Name.txt"

The script code for this solution could be something like:

if (UltraEdit.document.length > 0)  // Is any file opened?
{
    // Define environment for this script.
    UltraEdit.insertMode();
    UltraEdit.columnModeOff();

    // Move caret to top of first opened file which should
    // be the file with the parameters for the script.
    UltraEdit.document[0].top();

    // Read from the parameter list file just the first line without the
    // line terminating character(s) and split up the parameters list
    // using comma as delimiter. The parameter list file remains opened.
    UltraEdit.document[0].startSelect();
    UltraEdit.document[0].key("END");
    UltraEdit.document[0].endSelect();
    var asParameterList = UltraEdit.document[0].selection.split(',');

    // For safety check if the first parameter string begins with "parameter1".
    if (asParameterList[0].indexOf("parameter1") == 0)
    {
        // For demonstration just open a message box listing the read
        // parameters with their values without splitting them up further.
        var sMessageText = "The parameter";
        if (asParameterList.length > 1)
        {
            sMessageText += "s are:\n";
        }
        else
        {
            sMessageText += " is:\n";
        }
        for (var nParameter = 0; nParameter < asParameterList.length; nParameter++)
        {
            sMessageText += '\n"' +  asParameterList[nParameter] + '"';
        }
        UltraEdit.messageBox(sMessageText,"List of parameters");
    }
}

3. Modifying the script before execution

An UltraEdit script file is an ANSI text file and therefore it is possible to modify the script before execution.

echo var asParameterList = [ "value1", "value2" ];>"%TEMP%\ParameterList.tmp"
copy /B "%TEMP%\ParameterList.tmp" + "J:\SkyDrive\work\ue-script\newFile.js" "%TEMP%\TempScript.js" >nul
start "" /wait uedit64.exe /fni /s="%TEMP%\TempScript.js"
del "%TEMP%\ParameterList.tmp" "%TEMP%\TempScript.js"

The JavaScript code line which defines an initialized array of strings is first written into a temporary file. This temporary file is copied together with the script file to a new script in folder for temporary files. UltraEdit is executed using the temporary script file with the added parameter list array. Finally both temporary files are deleted from the command line.

An example script code which is extended dynamically with an additional line at the top to define the parameter strings:

// For demonstration just open a message box listing the parameter
// values as defined at top of the script from outside UltraEdit.
var sMessageText = "The parameter value";
if (asParameterList.length > 1)
{
    sMessageText += "s are:\n";
}
else
{
    sMessageText += " is:\n";
}
for (var nParameter = 0; nParameter < asParameterList.length; nParameter++)
{
    sMessageText += '\n"' +  asParameterList[nParameter] + '"';
}
UltraEdit.messageBox(sMessageText,"List of parameter values");

The advantage of this approach is that instead of an array of parameter strings it is also possible to define an array of integer numbers or floating point numbers. Or on the command line, multiple variables of different types are defined which are added to the temporary script file.

A last variant would be using // include parameters.js at top of the script file and the file parameters.js (with complete path or without path) is created dynamically on command line containing the lines to define the parameters in JavaScript language as JavaScript variables.