Selection only returns first character

Help with writing and running scripts

Selection only returns first character

Postby Uli2 » Tue Jul 31, 2007 8:56 am

Hello everybody!

The script below is supposed to do the following: read a line number and a tag number. Then it should print all tags in that specific line until the tag number is reached.
Code: Select all
var i_LineNum = UltraEdit.getValue("Goto line number?", 1);
var i_TagNum = UltraEdit.getValue("Goto tag number?", 1);

UltraEdit.ueReOn();

with (UltraEdit.activeDocument) {
   gotoLine(i_LineNum);
   for (i = 0; i < i_TagNum; i++) {
      findReplace.regExp = true;
      findReplace.find("<*>");
      UltraEdit.outputWindow.write(selection);
   }
   findReplace.regExp = false;
}

The loop is working correctly as well as the text selection in the document window. However, the output window only displays the first character of the selection, i.e. the "<".

My first attempt was using matchBrace() instead of findReplace. I also put startSelect() and endSelect() around the find command. The result stays the same.

What am I missing here?

TIA for your help!
Uli
User avatar
Uli2
Newbie
 
Posts: 4
Joined: Sun Jul 29, 2007 11:00 pm
Location: Germany

Re: Selection only returns first character

Postby Mofi » Tue Jul 31, 2007 9:50 am

What about using UltraEdit.activeDocument.selection instead of selection?
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Selection only returns first character

Postby Uli2 » Tue Jul 31, 2007 9:56 am

Hi Mofi,

I tried that but I doesn't change anything. So, what now?

Anyway, I thought that through the with-command it was specified that the selection should be taken from the active document.

Best,
Uli
User avatar
Uli2
Newbie
 
Posts: 4
Joined: Sun Jul 29, 2007 11:00 pm
Location: Germany

Re: Selection only returns first character

Postby Mofi » Tue Jul 31, 2007 10:17 am

Sorry, my mistake. I have not seen the with command. Your code is absolutely right. However, I have now tried following:

Code: Select all
UltraEdit.ueReOn();

/*
<Tag1> <Tag2>
*/

with (UltraEdit.activeDocument) {
   top();
   findReplace.regExp = true;
   findReplace.find("<*>");
   UltraEdit.outputWindow.write(selection);
   findReplace.regExp = false;
}

and it writes <Tag1> into the output window. I have executed this test script with UltraEdit v13.10a+1. And also your script worked.

If you really use v13.10 update and try again.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Selection only returns first character

Postby Uli2 » Tue Jul 31, 2007 10:40 am

Hi Mofi,

I tried your script and everything works fine.

That gave me the idea that my problem may be related to the file encoding. My files are Unicode and then the script does not work no matter if it is UTF-16 or UTF-8. If I convert them to ASCII then the script works.

I would appreciate if you could double check that.

Best,
Uli
User avatar
Uli2
Newbie
 
Posts: 4
Joined: Sun Jul 29, 2007 11:00 pm
Location: Germany

Re: Selection only returns first character

Postby Mofi » Tue Jul 31, 2007 11:28 am

That's the problem. Strings are NULL terminated and a Unicode string contains lots of NULLs (for ASCII characters every second byte). So you need a Unicode to ASCII conversion. So your script works currently for ASCII/ANSI files, but not for a Unicode file. I have verified it.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Selection only returns first character

Postby in0de » Tue Aug 14, 2007 4:53 am

man, i've got thru exactly the same problem,
yet there's 1 more thing to consider if you planning your scripts run more than once on the same document.

Since the UltraEdit.activeDocument.unicodeToASCII() function can be
invoked repeatedly with scripting feature, not likely on Uedit App menu,
you'll get the characters messed up eventually.


Here's the solution.
just call a dummy ASCIIToUnicode() function in a pair.

Code: Select all
UltraEdit.activeDocument.ASCIIToUnicode();
UltraEdit.activeDocument.unicodeToASCII();


It's a bit weird that there's no read-only status variable for files' encoding =$
User avatar
in0de
Newbie
 
Posts: 2
Joined: Sun Apr 01, 2007 11:00 pm

Re: Selection only returns first character

Postby Uli2 » Tue Aug 14, 2007 4:57 am

Thanks a lot for the tip. That's the trick I was looking for!
User avatar
Uli2
Newbie
 
Posts: 4
Joined: Sun Jul 29, 2007 11:00 pm
Location: Germany

Re: Selection only returns first character

Postby Mofi » Tue Aug 14, 2007 6:09 am

in0de wrote:It's a bit weird that there's no read-only status variable for files' encoding =$


I have sent following per email to IDM on Mon, 04 Jun 2007:

Subject: UE1310B2 - Request for additional script document properties

UltraEdit v13.10 Beta 2 has some very important new document properties. But in my point of view the 2 most important document properties are not available for script programers. Since years and after writing dozens of macros for other users the main problem for macros and probably also scripts is the file format (encoding) and the line termination format.

So I would like to see following document object scripting properties:

var file_encoding = UltraEdit.activeDocument.format;

or

var file_encoding = UltraEdit.activeDocument.encoding;

Return values:

0 ... ASCII/ANSI
1 ... EBCDIC
2 ... UTF-8
3 ... UTF-16 LE
4 ... UTF-16 BE
5 ... UTF-16 ASCII Escaped Unicode

Files in the last 4 formats are always opened by UltraEdit in UTF-16 LE format (Unicode) and so I would use in scripts often simply

if (file_encoding >= 2) { ... }

because often the specific Unicode format is not important, only if the file is an ASCII/ANSI file or a Unicode file. Well, with these return values and for most users like I which have never seen an EBCDIC file I could use in the scripts also

if (file_encoding)
{
// excecute Unicode specific commands
}
else
{
// excecute plain text specific commands
}


The second property should be something like:

var lineterm = UltraEdit.activeDocument.lineTermination;

or

var lineterm = UltraEdit.activeDocument.lineEnding;

Return values:

0 ... UNIX
1 ... MAC
2 ... DOS
3 ... UNIX but converted temporarily to DOS
4 ... MAC but converted temporarily to DOS

or if the last 2 items cannot be returned:

0 ... DOS
1 ... UNIX
2 ... MAC


If you want that too, sent also a feature request email to IDM support. The more users require it, the higher the priority for the IDM developers to code it.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Selection only returns first character

Postby jorrasdk » Tue Aug 14, 2007 8:41 am

I have also this moment requested these properties. Good suggestion.

I have a workaround I use every now and then for the line ending stuff. I often need to append line endings to new lines and need to now if these should be DOS or UNIX. So I use this simple function in the top of my script:

Code: Select all
var CRLF = getActiveDocumentLineTerm();

/* write readable line endings to output window */
UltraEdit.outputWindow.write("Line terminators in file: "+CRLF.replace(/\r/,"CR").replace(/\n/,"LF"));

function getActiveDocumentLineTerm() {
   /* Returns line terminators of active file as string */
   UltraEdit.activeDocument.top();
   UltraEdit.activeDocument.selectLine(); /* select line always captures line terminators (if any) */

   return UltraEdit.activeDocument.selection.replace(/^.*([\r\n]*)$/g, "$1");
}


also a variant of this could come close to Mofis suggested property:

Code: Select all
var linetermType = activeDocumentLineTerm();
var lineterms = ["DOS", "UNIX", "MAC"];
UltraEdit.outputWindow.write("File type: "+lineterms[linetermType]);

function activeDocumentLineTerm() {
   /* Returns code for line terminators of active file:
   -1 = Unable to resolve line terminators for file
   0 = DOS
   1 = UNIX
   2 = MAC
   */
   UltraEdit.activeDocument.top();
   UltraEdit.activeDocument.selectLine();

   return UltraEdit.activeDocument.selection.replace(/^.*([\r\n]*)$/g, function($1,$2) {
      switch ($2) {
         case "\r\n": return 0; /* DOS */
         case "\n": return 1; /* UNIX */
         case "\r":  return 2; /* MAC */
         default : return -1;
      }
   });
}


Warning: This doesn't work with UTF files because activeDocument.selection chokes on the 00-escaped strings.
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Selection only returns first character

Postby ReadJohn317 » Fri Aug 31, 2007 12:11 pm

Wouldn't a better solution be to have the <selection> property always convert the document's text to the JavaScript standard of the Unicode codepage? Thus, it would avoid all of this explicit codepage handling.

The opposite conversion should occur when the script puts characters into the document -- It should convert it from the script's Unicode to the document's codepage.

So, you could look at the initial problem (that <selection> was only returning a single character) is not due to embedded NUL characters -- It's due to a missing conversion from the document's codepage to JavaScript's Unicode.

If you add this conversion into UltraEdit, then every script will work with every codepage, and no explicit conversion will ever be necessary.
User avatar
ReadJohn317
Newbie
 
Posts: 1
Joined: Thu Aug 30, 2007 11:00 pm

Re: Selection only returns first character

Postby tulpe » Mon Mar 10, 2008 5:13 am

ReadJohn317 is absolutely right.

I have tried to get the following script to work without success:
http://pastebin.com/f5cd1a8ed

*PLEASE* fix this fast so we can apply scripts to UTF-8 files.

Having to manually switch from UTF-8 to ASCII and back is a real pain in the ass :cry:
User avatar
tulpe
Newbie
 
Posts: 4
Joined: Thu Oct 14, 2004 11:00 pm

Re: Selection only returns first character

Postby jorrasdk » Mon Mar 10, 2008 5:50 am

tulpe wrote:*PLEASE* fix this fast so we can apply scripts to UTF-8 files.

Hello tulpe. We - the users of UE - cannot fix this for you. This is a user-to-user forum. Write to IDM directly. See more at the top of the page.
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Selection only returns first character

Postby tulpe » Mon Mar 10, 2008 8:26 am

But.. you.. I mean.. whoops :)
User avatar
tulpe
Newbie
 
Posts: 4
Joined: Thu Oct 14, 2004 11:00 pm


Return to Scripts