Find in the selection

Help with writing and running scripts

Find in the selection

Postby mathmax » Mon Oct 15, 2007 11:24 am

Hello,

Is there a way to make the Find instruction apply only on the selected text ? For example I would like to check if the selected text match a regular expression. How can I do that with a macro?
User avatar
mathmax
Basic User
Basic User
 
Posts: 10
Joined: Thu May 24, 2007 11:00 pm

Re: Find in the selection

Postby Mofi » Mon Oct 15, 2007 1:10 pm

This was so often asked and the answer was always the same: NO (for UE < 14.00).

I have written about a workaround by using Replace All SelectText "..." in macros which can be in some situations used as I have done it several times in my macros - see my sticky forum topic in the macro forum and the documentations in the ZIP archive you can download there.

A workaround is always to copy or cut the selection to a new file and run the finds there.

With the script engine it is now also possible to load a selection into a string variable and run a replace on this variable. I don't know if it is also possible to run a find only on a string variable. Never needed this and so never tried it.

Edit on 2009-10-09: With UltraEdit v14.00 and UEStudio v6.50 and later it is possible to search only in the current selection. But when a string is found in the selection, the selection is discarded and a new selection is made containing only the found string.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4051
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Find in the selection

Postby jorrasdk » Mon Oct 15, 2007 2:26 pm

Just to answer Mofis question regarding scripts and finds on string variables:

You could for example do something like this: The script checks if a simple SQL date pattern can be found anywhere in a selection and shows a message box with the result:

Code: Select all
var selectedText = UltraEdit.activeDocument.selection; /* get selected text */

if(selectedText.length>0) { /* anything selected ? */
   
   /* let's search for a simple SQL date patttern */
   var findResult = selectedText.match(/\d\d-\d\d-\d\d\d\d/); /* javascript use it's own Perl regexp engine */

   if(findResult==null) { /* nothing found returns a null object */
      UltraEdit.messageBox("Date pattern not found");
   }
   else {
      UltraEdit.messageBox("Date pattern found: "+findResult);
   }
}


To mathmax: The above will only work if you are using UE version 13 and above.
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Find in the selection

Postby mathmax » Mon Oct 15, 2007 8:46 pm

The problem is that scripts cannot be executed with shortcuts...
User avatar
mathmax
Basic User
Basic User
 
Posts: 10
Joined: Thu May 24, 2007 11:00 pm

Re: Find in the selection

Postby jorrasdk » Mon Oct 15, 2007 9:49 pm

Sure they can if you by shortcuts mean hotkeys. I use it all the time. Access Scripting - Scripts... menu and notice the Hotkey and chord columns. Just click in the hotkey column beside the script you want to assign a hotkey. Then press the key combination you want.
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark


Return to Scripts