Function GetClipboardText to get clipboard text into string

Help with writing and running scripts

Function GetClipboardText to get clipboard text into string

Postby Mofi » Fri Feb 15, 2008 10:43 am

Hello script writers!

Since UE v14.20 this function is an obsolete method to get the clipboard content into a variable - see the posts below!

Here is a function which you can use to get the text in the current clipboard into a string variable for manipulation before writing the string into a file. For further details see the comments in the script.

Code: Select all
//== GetClipboardText ======================================================

/* The function   GetClipboardText   returns the text in the active
   clipboard as string for a variable of type string. It does this
   by opening a new file, convert it to ASCII in case of the setting
   >Always create new files as UNICODE< is checked in the configuration,
   pastes the text, re-selects it and returns the selection as string.
   This routine can be also used to get a selection in a Unicode file
   into a string variable if you first copy the selection to one of
   the 10 clipboards and the selected Unicode string can be converted
   to ANSI without a change in the characters like for UTF-8 XML files
   which do not really contain Unicode characters.

The function requires UltraEdit >= v13.20 or UEStudio >= v6.40.

The function has following optional input parameters:

  1) nDocIndex - is the document index number of the ASCII/ANSI file
     which should be used for the clipboard to string conversion. The
     default value is -1 which means that the function should create a
     new ASCII file by itself. Use -1 on first call of this function. But
     if you need this function more than once, use the second parameter to
     avoid closing the temp file and call this function with the correct
     document index number of the temp file to avoid permanent opening
     and closing temp files during script execution which would make the
     script very slow.

  2) bAutoClose - boolean variable to specify if the temp file should be
     automatically closed after the function has loaded the clipboard text
     into a string variable. The default is TRUE which means that the temp
     file is automatically closed.
     
This function is copyrighted by Mofi for free usage by UE/UES users.
The author cannot be responsible for any damage caused by this function.
You use it at your own risk. */

function GetClipboardText (nDocIndex, bAutoClose)
{
   // First check the optional input parameters.
   if (typeof(nDocIndex) != "number") nDocIndex = -1;
   if (nDocIndex >= 0)    // Check for invalid document index.
   {
      if (nDocIndex >= UltraEdit.document.length) nDocIndex = -1;
   }
   if (typeof(bAutoClose) != "boolean") bAutoClose = true;

   /* Remember document index of active file in case it must be made
      active again after the function has loaded the string from the
      temp file. This small routine was copied from
      http://www.ultraedit.com/forums/viewtopic.php?t=4571 */
   var nActiveFileIndex = -1
   for (var i = 0; i < UltraEdit.document.length; i++)
   {
      if (UltraEdit.activeDocument.path == UltraEdit.document[i].path)
      {
         nActiveFileIndex = i;
         break;
      }
   }

   var nActLineNo = -1;
   var nTempFileIndex = nDocIndex;

   if (nDocIndex < 0)     // Must a new temp file be created?
   {
      UltraEdit.newFile();
      nTempFileIndex = UltraEdit.document.length - 1;
      UltraEdit.document[nTempFileIndex].unicodeToASCII();
   }
   else
   {
   /* Just in case the user wants to use the active file as temp
      file remember the current position in the temp file to
      restore the position later. */
      nActLineNo = UltraEdit.document[nTempFileIndex].currentLineNum;
      var nActColNo = UltraEdit.document[nTempFileIndex].currentColumnNum;
      if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nActColNo++;
   }

   /* Go to top of the temp file, paste the active clipboard content
      and re-select it. */
   UltraEdit.document[nTempFileIndex].top();
   UltraEdit.document[nTempFileIndex].paste();
   UltraEdit.document[nTempFileIndex].selectToTop();

   // Get the selection into a string variable if there is one.
   var sClipboardText = "";
   if (UltraEdit.document[nTempFileIndex].isSel())
   {
      sClipboardText = UltraEdit.document[nTempFileIndex].selection;
      UltraEdit.document[nTempFileIndex].deleteText();
   }

   // Restore previous file position in the temp file if necessary.
   if (nActLineNo >= 0) UltraEdit.document[nTempFileIndex].gotoLine(nActLineNo,nActColNo);

   // Close the temp file if it should be done.
   if (bAutoClose) UltraEdit.closeFile(UltraEdit.document[nTempFileIndex].path,2);

   // Set focus back to previous active file if this is necessary.
   if (nActiveFileIndex >= 0 && nActiveFileIndex < UltraEdit.document.length)
   {
      if (UltraEdit.activeDocument.path != UltraEdit.document[nActiveFileIndex].path)
      {
         UltraEdit.document[nActiveFileIndex].setActive();
      }
   }
   return sClipboardText;
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4054
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Function GetClipboardText to get clipboard text into string

Postby dblume » Wed Jun 18, 2008 6:48 pm

Yea! This function made my desired script possible! Thank you so much.

FWIW, The documentation says that outputWindow is an array object. So instead of using GetClipboardText like I had to do:

Code: Select all
UltraEdit.runTool("MyToolWhichOutputsToTheOutputWindow");
// Find unused clipboard, then...
UltraEdit.outputWindow.copy();
var lines = GetClipboardText().split('\r\n');


It would have been far more handy to iterate across the outputWindow instead.

Code: Select all
UltraEdit.runTool("MyToolWhichOutputsToTheOutputWindow");
for( line in UltraEdit.outputWindow ) { ...
dblume
Newbie
 
Posts: 4
Joined: Wed Jun 18, 2008 6:39 pm

Re: Function GetClipboardText to get clipboard text into string

Postby Mofi » Thu Jun 19, 2008 2:17 am

You know that you can setup to capture the output of a user tool to an edit window instead of the output window in the tool configuration?
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4054
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Function GetClipboardText to get clipboard text into string

Postby dblume » Thu Jun 19, 2008 4:10 pm

Mofi wrote:You know that you can setup to capture the output of a user tool to an edit window instead of the output window in the tool configuration?

Yep. It's safe to assume I knew that, since I had to change the default to even get it to go to the output window. :) In my case, it's appropriate for the tool's output to go to the output window. But I also needed to act on it.
dblume
Newbie
 
Posts: 4
Joined: Wed Jun 18, 2008 6:39 pm

Re: Function GetClipboardText to get clipboard text into string

Postby dblume » Tue Jul 15, 2008 11:49 am

Version 14.10.0.1018 Didn't help this situation.

GetClipboardText() needs to be replaced with UltraEdit.clipboardContent or the ability to enumerate the line items in UltraEdit.outputWindow. (The documentation says that it is a JavaScript array.)

The flashing that GetClipboardText() causes is annoying.
dblume
Newbie
 
Posts: 4
Joined: Wed Jun 18, 2008 6:39 pm

Re: Function GetClipboardText to get clipboard text into string

Postby Mofi » Tue Oct 14, 2008 3:45 am

UltraEdit v14.20.0.1033 is released and with this (or any later version) my function GetClipboardText() is not needed anymore.


To get the clipboard content of the active clipboard use:

var ClipboardText = UltraEdit.clipboardContent;


To set the clipboard content of the active clipboard use:

UltraEdit.clipboardContent = "text copied to clipboard replacing existing content";


To append a text to the clipboard content of the active clipboard use:

UltraEdit.clipboardContent += "text appended to existing clipboard content";

See also the power tip UltraEdit/UEStudio Scripting Access to the Clipboard Contents.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4054
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Function GetClipboardText to get clipboard text into string

Postby Mythobeast » Wed Feb 04, 2009 1:18 am

This one feature is easily worth upgrading from the previous version all by itself. Thanks, Ian & Co. :D
Mythobeast
Newbie
 
Posts: 1
Joined: Fri Sep 19, 2008 2:21 pm


Return to Scripts