Need help with "UltraEdit.activeDocument.endSelect();

Help with writing and running scripts

Need help with "UltraEdit.activeDocument.endSelect();

Postby JoernH » Tue Apr 28, 2009 6:22 am

Dear Form readers,

As a newbie I again try putting my embarrassing questions out there :(

I would like to select two characters at the time from a text string of hex values "3C3F786D" and find the corresponding ASCII char.
For the first part of this, I just want to read out two characters at the time using this one :

Code: Select all
   UltraEdit.activeDocument.top();
   while (!UltraEdit.activeDocument.isEof()) {
      UltraEdit.activeDocument.startSelect();
      UltraEdit.activeDocument.key("RIGHT ARROW");
      UltraEdit.activeDocument.key("RIGHT ARROW");
      var CurrentChar = "";
      CurrentChar = UltraEdit.activeDocument.selection;
      UltraEdit.activeDocument.endSelect();
      UltraEdit.outputWindow.write(CurrentChar);
   }


However, using this on the string "3C3F786D" gives me the output :

3C
3C3F
3C3F78
3C3F786D

While I wanted

3C
3F
78
6D

Can someone put me on the right track here please ?

Kind regards

Jørn
User avatar
JoernH
Basic User
Basic User
 
Posts: 15
Joined: Tue Mar 10, 2009 10:41 am
Location: Belgium

Re: Need help with "UltraEdit.activeDocument.endSelect();

Postby JoernH » Tue Apr 28, 2009 7:46 am

OK. Did a "workaround" ?

Code: Select all
   UltraEdit.activeDocument.top();
   while (!UltraEdit.activeDocument.isEof()) {
      UltraEdit.activeDocument.startSelect();
      UltraEdit.activeDocument.key("RIGHT ARROW");
      UltraEdit.activeDocument.key("RIGHT ARROW");
      var CurrentChar = UltraEdit.activeDocument.selection;
      UltraEdit.activeDocument.endSelect();
      UltraEdit.activeDocument.key("LEFT ARROW");
      UltraEdit.activeDocument.key("RIGHT ARROW");
      UltraEdit.outputWindow.write(CurrentChar);
   }


... and now I get the output I need.

Kind regards

Jørn
User avatar
JoernH
Basic User
Basic User
 
Posts: 15
Joined: Tue Mar 10, 2009 10:41 am
Location: Belgium

Re: Need help with "UltraEdit.activeDocument.endSelect();

Postby rhapdog » Tue Apr 28, 2009 8:03 am

Glad you found the work-around.

I noticed by your script you are trying to use endSelect() to "unselect" the selected text. This is not what this is for.

The startSelect and endSelect According to the UE Help file:
startSelect: Start selection. This turns the selection mode on. Any cursor movement or positioning will be with selection on and the text is selected. endSelect will stop the selection mode. The selected text will remain selected until another command causes it not to be selected as with normal editing.


This means the correct way of writing that script should be:
Code: Select all
UltraEdit.activeDocument.top();
   while (!UltraEdit.activeDocument.isEof()) {
      UltraEdit.activeDocument.startSelect();
      UltraEdit.activeDocument.key("RIGHT ARROW");
      UltraEdit.activeDocument.key("RIGHT ARROW");
      UltraEdit.activeDocument.endSelect();
      var CurrentChar = UltraEdit.activeDocument.selection;
      UltraEdit.activeDocument.key("LEFT ARROW");
      UltraEdit.activeDocument.key("RIGHT ARROW");
      UltraEdit.outputWindow.write(CurrentChar);
   }


Since the selected text will remain selected until another normal editing command causes it to not be selected, and there is no "cancel selection" command in the script or macro languages, then the above would be the way to do it.

I do believe I will write IDM and request "cancel selection" macro and script language commands. I grow rather tired of using LEFT ARROW / RIGHT ARROW types of workarounds myself.
User avatar
rhapdog
Master
Master
 
Posts: 265
Joined: Tue Apr 01, 2008 10:02 am
Location: Mississippi, USA

Re: Need help with "UltraEdit.activeDocument.endSelect();

Postby rhapdog » Tue Apr 28, 2009 8:36 am

If you need a "cancelSelect" function, I have written one.

Code: Select all
function cancelSelect(x) {
  if (x.isSel()){
    var lineNum = x.currentLineNum;
    var colNum = x.currentColumnNum;
    x.key("LEFT ARROW");
    x.gotoLineSelect(lineNum,colNum);
  }
}


It should be placed in your script, and called in the following ways:
Code: Select all
cancelSelect(UltraEdit.activeDocument);

or
Code: Select all
cancelSelect(UltraEdit.document[1]);


It's not as efficient as just plain LEFT ARROW / RIGHT ARROW, but it is consistent if you plan on needing this functionality in numerous scripts and it will always end up in the proper cursor position no matter your starting point.
User avatar
rhapdog
Master
Master
 
Posts: 265
Joined: Tue Apr 01, 2008 10:02 am
Location: Mississippi, USA

Re: Need help with "UltraEdit.activeDocument.endSelect();

Postby Mofi » Tue Apr 28, 2009 11:07 am

Nice idea, but the cursor position changes 1 column to left. Here is your script a little bit modified.

Code: Select all
function cancelSelect(x) {
  if (x.isSel()){
    x.endSelect();
    var lineNum = x.currentLineNum;
    var colNum = x.currentColumnNum;
    if (typeof(UltraEdit.activeDocumentIdx) == "undefined") colNum++;
    x.gotoLine(lineNum,colNum);
  }
}

I would be very happy if such a simple general workaround for the missing command unselect would exist also for macros. In June 2005 a wrote a feature request email to IDM about a macro command to unselect the current selection without needing any cursor movements. Looks like I'm the only one which would need such a macro/script command.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Need help with "UltraEdit.activeDocument.endSelect();

Postby rhapdog » Tue Apr 28, 2009 11:38 am

Thank you for fixing my oversight of forgetting to add one. I forgot (momentarily) that the column number starts at zero for UltraEdit prior v16.00 when reading it.

As far as others needing it, I do, and apparently so does the person who started the thread.

Also, I have sent in a request to IDM for the feature, and have already received a response which I will share with you.

I understand, and I see where this could be useful. We will look into including this in a future release.

Thanks,

Ben

Perhaps we'll eventually get this after all. :mrgreen:
User avatar
rhapdog
Master
Master
 
Posts: 265
Joined: Tue Apr 01, 2008 10:02 am
Location: Mississippi, USA


Return to Scripts