【问题标题】:How do I pass parameters to an UltraEdit script from the command line?如何从命令行将参数传递给 UltraEdit 脚本?
【发布时间】:2017-01-02 04:23:02
【问题描述】:

现在从命令行执行 UltraEdit 脚本:

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

是否可以从命令行向 UltraEdit 脚本传递参数?以及如何在脚本中获取它们?

可能是这样的:

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

然后在newFile.js中得到parameter1=value1和parameter2=value2。

【问题讨论】:

    标签: javascript command-line parameters ultraedit


    【解决方案1】:

    UltraEdit 脚本通常从命令行执行,以完全重新格式化一个或多个文本文件,无需用户交互且不依赖于参数。或者,用户在 UltraEdit 中手动启动 UltraEdit 脚本,而无需使用getString 和/或getValue 进行一些最少的用户交互。有许多脚本语言和脚本解释器可以根据 VBScript、PowerShell、Perl、Python 等参数来执行某些操作。

    无法在 UltraEdit 的命令行上为 UltraEdit 宏/脚本指定额外的自定义参数。命令行参数由uedit64.exe 或uedit32.exe 解释,UltraEdit 宏和脚本无权访问可执行文件的参数列表。

    在启动 UltraEdit 并执行脚本之前,我知道将字符串(参数)从另一个进程传递给 UltraEdit 脚本的三种可能性:

    1. 通过剪贴板,或
    2. 通过文本文件,或
    3. 通过在执行前修改脚本。

    1。通过剪贴板将参数传递给 UltraEdit/UEStudio 脚本

    第一个解决方案很容易实现。但它有一个很大的缺点,即 Windows 剪贴板内容在启动时会被修改,并且在脚本读取参数及其值之前,其他进程不应将某些内容复制到剪贴板。如果 UltraEdit 应该在后台执行以执行脚本,这些缺点是非常有问题的。

    以下两条命令必须在命令行或批处理文件中执行:

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

    clip.exe 从 Windows Vista 和 Windows Server 2003 开始​​可用。但在 Windows XP 上没有clip.exe。但是,Windows Server 2003 中的clip.exe 也可以在 Windows XP 上使用。

    强烈建议在启动 UltraEdit 时使用/fni(强制新实例)在默认情况下未选中配置设置允许多个实例时从命令行执行脚本。有关从命令行运行 UltraEdit 宏/脚本时为何应将 /fni 用作命令行上的第一个参数的解释,请阅读 UltraEdit 论坛中的主题 Always get error message when running a macro/script via command line parameter (solved)。

    从剪贴板读取参数的示例脚本代码是:

    // 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。通过文本文件将参数传递给 UltraEdit/UEStudio 脚本

    与第一个解决方案相比,使用此解决方案不会修改剪贴板。但是,必须确保以下命令行不会被两个进程同时执行。

    > 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
    

    命令ECHO 输出的行被重定向到文本文件C:\Temp\ParameterList.tmp,然后启动UltraEdit 以在单独的进程中运行脚本并停止批处理,直到退出UltraEdit。最后,从命令行删除临时文本文件。

    从具有固定名称和路径的文件中读取参数的示例脚本代码:

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

    对于动态文件名的使用,文件名必须指定为第二个参数,UltraEdit在运行脚本之前打开这个文件,脚本从第一个打开的文件中读取参数。

    > "%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"
    

    此解决方案的脚本代码可能类似于:

    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。执行前修改脚本

    UltraEdit 脚本文件是一个 ANSI 文本文件,因此可以在执行前修改脚本。

    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"
    

    定义初始化字符串数组的 JavaScript 代码行首先写入临时文件。此临时文件与脚本文件一起复制到临时文件文件夹中的新脚本中。 UltraEdit 使用带有添加参数列表数组的临时脚本文件执行。最后从命令行删除两个临时文件。

    一个动态扩展的示例脚本代码,顶部有一个附加行来定义参数字符串:

    // 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");
    

    这种方法的优点是,除了参数字符串数组之外,还可以定义整数或浮点数数组。或者在命令行定义多个不同类型的变量,添加到临时脚本文件中。

    最后一个变种是在脚本文件顶部使用// include parameters.js,并在命令行上动态创建文件parameters.js(带有完整路径或不带路径),其中包含将JavaScript语言中的参数定义为JavaScript的行变量。

    【讨论】:

      猜你喜欢
      • 2013-10-11
      • 1970-01-01
      • 2014-03-08
      • 2013-10-22
      • 2016-12-19
      • 1970-01-01
      • 2017-12-08
      • 1970-01-01
      • 2017-03-22
      相关资源
      最近更新 更多