Include external script files

Help with writing and running scripts

Include external script files

Postby yots » Wed Oct 29, 2008 8:11 pm

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/viewtopic.php?f=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
yots
Newbie
 
Posts: 1
Joined: Wed Oct 29, 2008 7:45 pm

Re: Include external script files

Postby jorrasdk » Thu Oct 30, 2008 4:47 am

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;
}
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

#include for scripting?

Postby andrel » Thu Dec 10, 2009 2:18 am

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.
andrel
Newbie
 
Posts: 4
Joined: Thu Apr 16, 2009 2:50 am

Re: #include for scripting?

Postby jorrasdk » Thu Dec 10, 2009 2:34 am

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).
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Include external script files

Postby andrel » Thu Dec 10, 2009 8:26 am

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.
andrel
Newbie
 
Posts: 4
Joined: Thu Apr 16, 2009 2:50 am

Re: Include external script files

Postby rhapdog » Thu Dec 10, 2009 9:31 am

andrel wrote:I wanted to trigger other people to request this feature also.

Ok, andrel, I've been "triggered." :mrgreen:
User avatar
rhapdog
Master
Master
 
Posts: 265
Joined: Tue Apr 01, 2008 10:02 am
Location: Mississippi, USA

Re: Include external script files

Postby jorrasdk » Wed Jan 27, 2010 2:30 am

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 :-)
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Include external script files

Postby rhapdog » Thu Jan 28, 2010 8:47 am

Awesome news indeed! Can't wait to get my hands on it.
User avatar
rhapdog
Master
Master
 
Posts: 265
Joined: Tue Apr 01, 2008 10:02 am
Location: Mississippi, USA


Return to Scripts