Convert 16bit to utf8 characters? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Convert 16bit to utf8 characters?

Post by braunbaer » 01 Aug 2021, 11:38

I get this text by copying an error message dialog to the clipboard and then assigning text:=clipboard

Code: Select all

        ---------------------------
        Myscript.ahk
        ---------------------------
        Error at line 489 in #include file "C:\Bat\Debug.ahk".
        
        Line Text: abc
        Error: This line does not contain a recognized action.
        
        The script was not reloaded; the old version will remain in effect.
        ---------------------------
        OK   
        ---------------------------
However, instr or regexmatch won't work on this text. The only reason I can imagine is that maybe text is made of 16 bit characters, while regexmatch expects UTF8 Unicode strings. How can I convert the variable text to something compatible with regexmatch? Or is there another reason this does not work?

User avatar
boiler
Posts: 16913
Joined: 21 Dec 2014, 02:44

Re: Convert 16bit to utf8 characters?

Post by boiler » 01 Aug 2021, 13:01

If that error message is just the text that is being operated on, then I think you need to show the code that is not doing what you expect. What part are you trying to match? The script below works when I use RegExMatch to extract the file name from that text. Does it do something different for you than display the file name?

Code: Select all

Text =
(
        ---------------------------
        Myscript.ahk
        ---------------------------
        Error at line 489 in #include file "C:\Bat\Debug.ahk".
        
        Line Text: abc
        Error: This line does not contain a recognized action.
        
        The script was not reloaded; the old version will remain in effect.
        ---------------------------
        OK   
        ---------------------------
)

RegExMatch(Text, "(?<="").*(?="")", Match)
MsgBox, % Match
Last edited by boiler on 01 Aug 2021, 13:07, edited 1 time in total.

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Convert 16bit to utf8 characters?

Post by mikeyww » 01 Aug 2021, 13:07

I also had no trouble with it.

Code: Select all

Clipboard =
MsgBox,,, % "Error at line 489 in #include file ""C:\Bat\Debug.ahk"".`n`nLine Text: abc`n"
          . "Error: This line does not contain a recognized action.`n`n"
          . "The script was not reloaded; the old version will remain in effect."
If Instr(Clipboard, "script was not")
     MsgBox, 64, Success, Found!
Else MsgBox, 48, Failure, Not found!

braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Convert 16bit to utf8 characters?

Post by braunbaer » 01 Aug 2021, 13:38

With the text that I posted , everything works fine.

But when I copy this text via clipboard from the dialog box (it's the AHK error message dialog when there is a syntax error when (re)loading the file) and try to regexmatch the variable that I got from the the clipboard, both regexmatch and instr fail, no matter what I search for. I can display the text in a msgbox or write it to a file, but there is no match with anything

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Convert 16bit to utf8 characters?

Post by mikeyww » 01 Aug 2021, 14:08

Show your code.

braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Convert 16bit to utf8 characters?  Topic is solved

Post by braunbaer » 01 Aug 2021, 14:19

I found the solution: It's enough to append something (like A_Space) to the variable read from the clipboard, then ahk automatically converts the data to UTF-8.

This regexmatch is successful,

Code: Select all

   txt:=clipboard . " "
   rgx:="i)Error at line (\d+) in #include file ""([^""]+)"
   if !regexmatch(txt, rgx, x)
       debug("Error text: ", txt), abort("Can't locate the error")    
   else msgbox RegExMatch successful
while this one, without appended space, fails. The regex itself does not require a space at the end.

Code: Select all

   txt:=clipboard
   rgx:="i)Error at line (\d+) in #include file ""([^""]+)"
   if !regexmatch(txt, rgx, x)
       debug("Error text: ", txt), abort("Can't locate the error")    
   else msgbox RegExMatch successful

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Convert 16bit to utf8 characters?

Post by mikeyww » 01 Aug 2021, 14:42

The following worked for me.

Code: Select all

Clipboard =
MsgBox,,, % "Error at line 489 in #include file ""C:\Bat\Debug.ahk"".`n`nLine Text: abc`n"
          . "Error: This line does not contain a recognized action.`n`n"
          . "The script was not reloaded; the old version will remain in effect."
MsgBox % RegExMatch(Clipboard, "i)Error at line (\d+) in #include file ""([^""]+)")
image210801-1542-001.png
image210801-1542-001.png (5.56 KiB) Viewed 439 times

braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Convert 16bit to utf8 characters?

Post by braunbaer » 01 Aug 2021, 14:53

Sure, you fill the clipboard from ahk, not from an actual error message, so the clipboard contains text in UTF format.

To reproduce the error, you would have to
1. start the script
2. make a change to the script with a syntax error and save it
3. try to reload the script
4. copy the content of the error message to the clipboard with ^c send ^c
5. read the clipbard content and try to find something with instr or regexmatch. It wont work.

But if you append a space to the string that you have read from the clipboard, AHK converts the data and it works again.

P.S. I don't see how you get data into the clipboard. Do you hit ^c manually?

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Convert 16bit to utf8 characters?

Post by mikeyww » 01 Aug 2021, 15:01

It worked when I tried it, but perhaps you have a different sort of message box there.

Yes, I hit ^c manually when the message box appeared. I also tried it with a message box generated from a syntax error, and it worked.

braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Convert 16bit to utf8 characters?

Post by braunbaer » 01 Aug 2021, 17:37

Strange indeed. Maybe it depends on the windows version and on the locale

Post Reply

Return to “Ask for Help (v1)”