Tapatalk

Include external script files

Include external script files

1
NewbieNewbie
1

    Oct 30, 2008#1

    Hi,

    I found no way to include an external script into my scripts.
    So I ended with this workaround.
    Note: This is just a proof of concept. It worked fine for me but it is not really tested.

    For testing I used the "FileNameFunctions.js" from here http://www.ultraedit.com/forums/viewtop ... =52&t=6762 (without the examples at the bottom of file).

    1. Add this in the first line of your script with the full path of the external file

    Code: Select all

    eval(include("D:\\Downloads\\FileNameFunctions.js"));
    2. Add this function to the script that want to include the external file

    Code: Select all

    function include(file)
    {
    		var doc = UltraEdit.activeDocument.path;
    		UltraEdit.open(file);
    		UltraEdit.activeDocument.selectAll();
    		var inc = UltraEdit.activeDocument.selection;		
    		UltraEdit.closeFile(file,2);		
    		
    		for (index = 0; index < UltraEdit.document.length; index++) {
    	  	if (UltraEdit.document[index].path == doc)
    	  	{
    	  		UltraEdit.activeDocument =UltraEdit.document[index]; 	
    	  	}	  	  
    	  }		
    		return inc;		
    }
    With this I was able to use all the functions defined in "FileNameFunctions.js" in my script.

    Is there really no better way to include externals scripts?

    Nicolai

    262
    MasterMaster
    262

      Oct 30, 2008#2

      Hi Nicolai!

      We have earlier discussed the possibility of including "common code" in a script but concluded an enhancement request to IDM must be the answer. No news of this since.

      But lacking this, your proof of concept is really great !! - I tested it with the "FileNameFunctions.js" as well and it worked perfectly!

      But it is not "flicker free" since a new document is opened temporarily. I wanted a "flicker free" and fast way of including my common.js script which I always manually add to new scripts.

      This is what I did:

      a) I installed the small program clip.exe into \WINDOWS\SYSTEM32. Clip.exe can be used a the DOS command line to capture DOS output to the windows clipboard.

      b) Next I configured a user tool in UE with these settings:
      Name=IncludeCommon
      Command Line=TYPE \data\UltraEditScripts\common.js | clip
      Options: DOS Program
      Output: Append to existing, uncheck "show DOS box", uncheck "capture output", no replace

      c) Then I created a includeCommon() function inspired by your trick with eval (my common.js contains the functions from the FileNameFunctions.js script by Mofi and some other basic functions). PS: You need UE version 14.20 since I rely on UltraEdit.clipboardContent.

      Code: Select all

      eval(includeCommon());
      
      UltraEdit.outputWindow.write("Result of the functions for input string \"C:\\Temp\\Test.txt\":");
      UltraEdit.outputWindow.write("");
      UltraEdit.outputWindow.write("GetFilePath:   \""+GetFilePath("C:\\Temp\\Test.txt")+"\"");
      UltraEdit.outputWindow.write("GetNameOfFile: \""+GetNameOfFile("C:\\Temp\\Test.txt")+"\"");
      UltraEdit.outputWindow.write("GetFileName:   \""+GetFileName("C:\\Temp\\Test.txt")+"\"");
      UltraEdit.outputWindow.write("GetFileExt:    \""+GetFileExt("C:\\Temp\\Test.txt")+"\"");
      
      function includeCommon() {
      	/* which UE clipboard is active ? */
      	var clipIdx = UltraEdit.clipboardIdx;
      
      	/* If Windows clipboard is active - then save content on this */
      	var clipboardContent;
      	if(clipIdx==0) clipboardContent = UltraEdit.clipboardContent;
      	
      	/* ensure windows clipboard and clear it */
      	UltraEdit.selectClipboard(0);
      	UltraEdit.clearClipboard();
      
      	/* run tool that gets contents of common script functions file */
      	UltraEdit.runTool("IncludeCommon");
      
      	/* retrieve contents from windows clipboard and clear clipboard */
      	var commonCode = UltraEdit.clipboardContent;
      	UltraEdit.clearClipboard();
      
      	/* restore original active clipboard */
      	UltraEdit.selectClipboard(clipIdx);
      
      	/* if active clipboard was windows clipboard, restore contents */
      	if(clipIdx==0) UltraEdit.clipboardContent = clipboardContent;
      
      	/* return common code */
      	return commonCode;
      }
      And it seems to be quite fast and it is "flicker free" and doesn't remove focus from the active document.

      Here is the condensed version when you do not worry about overwriting the contents of the windows clipboard:

      Code: Select all

      eval(includeCommon());
      function includeCommon() {
      	UltraEdit.selectClipboard(0);
      	UltraEdit.runTool("IncludeCommon");
      	var commonCode = UltraEdit.clipboardContent;
      	UltraEdit.clearClipboard();
      	return commonCode;
      }

      4
      NewbieNewbie
      4

        #include for scripting?

        Dec 10, 2009#3

        I'm using more and more js scripts. Like its common practice in programming, I write generic code. That way my scripts are easy to use and quick to create. Now I have xx.js file including all generic code. I paste the content of this file into the new script I want to make. But I dont like that, I just want to "#include" the .js file a the top of the new script. I already requested this feature, but maybe if more users like this feature it will speed up things.

        262
        MasterMaster
        262

          Re: #include for scripting?

          Dec 10, 2009#4

          Alas no, you still have to use a workaround as discussed in this thread above. And feel free to send IDM a feature request for better support for inclusion of external script libraries (see e-mail address for IDM at the top of the page).

          4
          NewbieNewbie
          4

            Dec 10, 2009#5

            Sorry but this solution doesn't make sense at all! Paste code to fetch "other" code? Than I could have copied and paste the lib into the new script just as easily. With this post (it is moved..) I wanted to trigger other people to request this feature also.

            2362
            MasterMaster
            2362

              Dec 10, 2009#6

              andrel wrote:I wanted to trigger other people to request this feature also.
              Ok, andrel, I've been "triggered." :mrgreen:
              “Don’t document the problem, fix it.” – Atli Björgvin Oddsson

              262
              MasterMaster
              262

                Jan 27, 2010#7

                Good news: Ian wrote in the recent blog entry UltraEdit v16.00 - What to Expect? that 16.00 will have "... Ability to include one script in another...". Good stuff :-)

                2362
                MasterMaster
                2362

                  Jan 28, 2010#8

                  Awesome news indeed! Can't wait to get my hands on it.
                  “Don’t document the problem, fix it.” – Atli Björgvin Oddsson