Include statement to refer to library of functions

Include statement to refer to library of functions

681
Advanced UserAdvanced User
681

    Apr 01, 2018#1

    Hello Comrades

    Regarding this mini library of functions
    https://www.ultraedit.com/resources/scr ... nctions.js (compliments of who else Mofi)
    Located here:
    https://www.ultraedit.com/downloads/ext ... ripts.html:

    I have sucessfully tested GetFileName  (get file name without extension from name of a file)
    By pasting it into my existing script and then calling the function.

    My question is:
    Is it possible to have some sort of include statement pointing at FileNameFunctions.js on my laptop instead of having to have the entire function pasted into my script?

    With thanks as always

    6,616548
    Grand MasterGrand Master
    6,616548

      Apr 01, 2018#2

      Open Help of UltraEdit, switch to tab Index, enter scr in edit field, double click on Scripting commands in list and read in opened help page the chapter Including scripts in scripts. This chapter explains the special // include statement.
      UltraEdit Help wrote:Including scripts in scripts
      If desired, users may include an external script in a script by reference as shown below:

      // include externalScript.js

      or

      // include C:\full path\to external\script\externalScript.js

      The include command must be preceded by a line comment. If a users wishes to exclude the included script for debugging purposes, the include should be preceded by a doubled line comment, i.e.:

      // // include externalScript.js

      Please note that scripts are executed as soon as the inclusion is processed, but inclusions are processed prior to the active script. If an include is inserted into the middle of a script file, it will actually execute prior to the script it is included in. Where users desire to build complex scripts in a modular fashion from smaller scripts, the best practice would be to create a master script file that calls the included scripts, i.e.:

      // include script1.js
      // include script2.js
      // include script3.js
      Note: The name of the file, best with full path, must be specified always without double quotes and without escaping the backslash. The special include comment line is parsed by UltraEdit and not by JavaScript interpreter.

      But FileNameFunctions.js cannot be included as is because it contains at bottom code outside any function to demonstrate the usage of the functions on opening this script file and simply run it. This code would be also executed on including the script in another script. Therefore everything from beginning of line

      // === File Name Functions Demonstration =====================================

      to end of file must be first deleted from (a copy of) this script file. And I suggest to execute the Perl regular expression replace as posted at General file name evaluating functions to remove all block and line comments for faster processing the functions in the script file by JavaScript interpreter.

      It is not possible to include just a specific function stored in a script file. But it is of course possible to save each function of FileNameFunctions.js into a file like GetFileExt.js, GetFileName.js, ... to include only the function respectively script file needed in own script.

      But be careful on using // include script file name.js in a script in development. The include is not done by the JavaScript interpreter. It is done by UltraEdit which in memory includes all referenced script files into script file to execute and then lets the JavaScript interpreter parse the entire file as built by UltraEdit in memory. This can result in a line number in an error message of no use for script writer as the script file in memory has more lines than the script file opened in UltraEdit. If the included script files contain only entire functions, it is better to include them with // include script file name.js at bottom of the script in development because it does not matter for JavaScript interpreter if the functions are defined at top or at bottom of a script file. Then the line number in an error message of self-written script match with the line numbers of script opened in UltraEdit.
      Best regards from an UC/UE/UES for Windows user from Austria

      681
      Advanced UserAdvanced User
      681

        Apr 01, 2018#3

        Thank you Mofi. I am working on it... :)