Running a script on active file without adding script to script list

Running a script on active file without adding script to script list

18
Basic UserBasic User
18

    Jul 21, 2012#1

    I know about the scripting > scripts > add, and how things can be displayed in the box on the right of the screen (which I use all the time). However, I was wondering is there is a way to run a script without it being in either location (scripting > scripts or the box on the right side of the screen)? I have a few scripts that I run once-in-awhile, but I don't want to add them to these lists.

    Thanks in advance.

    2362
    MasterMaster
    2362

      Jul 21, 2012#2

      First, let me start by saying it is quite easy to "remove" a script quickly and easily if you want to use it only temporarily.

      There are only 2 ways that I am aware of to run a script that is not in that list.

      #1: Run it from the command line by invoking UE/UES with command line parameter:

      Code: Select all

      Uedit32.exe /fni /s,e"c:\working files\myscript.js"
      
      See UltraEdit help file under Command Line Parameters for more information.

      #2: Have the script loaded in the edit window as the active file, and choose from the menu, Scripting->Run Active Script, or press Alt+Shift+R with default key mapping.

      If you are wanting to run a script against a particular file that you have open, it would be best to Add the script, then Remove it afterwards.

      6,603548
      Grand MasterGrand Master
      6,603548

        Jul 21, 2012#3

        The problem with Scripting - Run Active Script is that the script file itself is the active file. Most often scripts should be executed simply on active file and therefore it is not possible to simply run active script.

        But most often when a script file should be executed on a file there is only 1 file opened and not multiple files. In this case it would be good if the script file could be opened too and executed with Scripting - Run Active Script, and the script simply runs on the other opened file.

        I start the scripts I post in the forums always with the line:

        Code: Select all

        if (UltraEdit.document.length > 0) {
        to make sure that a script which does not open files itself does nothing when no file is opened at all. Of course there is the matching } at end of the file too.

        It is possible to run a script using command Run Active Script on a file opened together with the script file with following code inserted immediately below the line written above.

        Code: Select all

           var sNameOfScript = "Test.js";
        
           // Get index position of last backslash in file name of active file.
           var nLastBackslashIndex = UltraEdit.activeDocument.path.lastIndexOf("\\");
           // A script file must be stored on a local disk or network share and therefore must have
           // a file name with a complete path containing last backslash not within first 3 characters.
           if (nLastBackslashIndex > 3) {
              // Get just the name of the active file. That's not the solution working
              // for all files. But for this purpose it is enough and very quick.
              var sFileName = UltraEdit.activeDocument.path.substr(++nLastBackslashIndex);
              // Is the name of the active file equal the name of the script file containing
              // this code, then the script is executed using Scripting - Run Active Script.
              if (sFileName == sNameOfScript) {
                 // As the script should not run on its own code, another file should become
                 // the active file now. Most likely this is the first file opened (usually
                 // most left on open file tabs bar), except the script file is the first file.
                 // If there is no other file opened in UE/UES, stop script execution.
                 if (UltraEdit.document.length == 1) {
                    UltraEdit.messageBox("There is just the script file opened.","Error on script execution");
                    UltraEdit.outputWindow.showStatus=false;
                    throw true;   // Causes exit of script because of a script error.
                 }
                 UltraEdit.document[UltraEdit.activeDocument.path==UltraEdit.document[0].path ? 1 : 0].setActive();
              }
           }
        Important is the first line. This line must be adapted for every script and must contain the case-sensitive name of the script file itself.

        The other lines below are always the same. Therefore it would be possible to store the other lines in a script file named for example IsScriptActiveFile.js and include this script file in all other scripts used only temporarily. Example for such a just temporarily used script file with name Test.js:

        Code: Select all

        if (UltraEdit.document.length > 0) {
        
           var sNameOfScript = "Test.js";
        
           // include C:\MyUltraEditScripts\IsScriptActiveFile.js
        
           // Code of script Test.js
        }
        To include a script within other scripts is possible since UltraEdit v16.00 and UEStudio v10.00.

        Well, the command throw true results in exiting script execution by an error when script is started with Run Active Script and there is no other file opened. This is a working but not a good coded solution. Better would be to enclose the entire code of a script in a function main and use command return instead of command throw true in the code above. The modified script with name Test.js would then look like:

        Code: Select all

        function main() {
        
           if (UltraEdit.document.length < 1) return;
        
           var sNameOfScript = "Test.js";
        
           // include C:\MyUltraEditScripts\IsScriptActiveFile.js
        
           // Code of script Test.js
        }
        main();
        That has also the advantage that command return can be used also on other places within main code to exit script.

        Please note that in Javascript it makes a difference if a variable is defined within a function with keyword var or if defined outside of any function with keyword var.

        Outside of any function a variable defined with keyword var is a global variable which can be accessed also from within any function at any time in same script. But if a variable is defined with keyword var within a function like main, this variable is only a local variable which can be accessed only within this function.

        Variables defined without keyword var are global variables independent if the variable is defined within a function or outside. But it is in general a bad coding style to define variables without keyword var as it is hard to see that a symbol string is the name of a variable and where this variable is defined the first time. Defining variables without keyword var should be done only if a file must be as small as possible which is definitely never the case for UltraEdit scripts.