Clipboard --UTF-8-->to file

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
knn
Posts: 30
Joined: 30 Mar 2020, 20:35

Clipboard --UTF-8-->to file

27 Apr 2020, 14:22

OK, I've been looking around for a day now and I cannot find the solution.

I want to
  • select a text in Notepad++, e.g. фирменно
  • create a UTF-8 file and save the text into it
I managed to get the text to display correctly in a MsgBox, so I know that AHK got фирменно correctly. But when I save it into a file it's all messed up,
??????????, consisting of only #3F (?).

This is the code I use so far

Code: Select all

file := FileOpen(path, "w")
file.Encoding := UTF-8
file.write(vText)
file.close()
knn
Posts: 30
Joined: 30 Mar 2020, 20:35

Re: Clipboard --UTF-8-->to file

27 Apr 2020, 14:49

Thank you, I can confirm that the file is being saved correctly. But now the reading back doesn't work.

Code: Select all

file := FileOpen(path, "r-d")
file.Encoding := "UTF-8"
vText := file.read()
MsgBox % vText <--- Shows �ннемЀѸЄ�
file.close()
I tried also with file := FileOpen(path, "r-d", "UTF-8")
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Clipboard --UTF-8-->to file

27 Apr 2020, 15:13

For me it works correctly like this:

Code: Select all

filePath := A_Desktop . "\test.txt"

File := FileOpen(filePath, "w")
File.Encoding := "UTF-8"
File.Write("фирменно")
File.Close()

File := FileOpen(filePath, "r-d")
File.Encoding := "UTF-8"
text := File.Read()
File.Close()

MsgBox, % text
More correct way:

Code: Select all

filePath := A_Desktop . "\test.txt"

File := FileOpen(filePath, "w", "UTF-8")
File.Write("фирменно")
File.Close()

File := FileOpen(filePath, "r") ; don't need to specify encoding since BOM presents in the file
text := File.Read()
File.Close()

MsgBox, % text
knn
Posts: 30
Joined: 30 Mar 2020, 20:35

Re: Clipboard --UTF-8-->to file

27 Apr 2020, 16:28

Thank you very much for your help. I found out the reason why it didn't work.

I use PHP to process the UTF-8 file and it turned out that PHP does not automatically handle UTF-8 files with BOM, i.e. the file that was created by AHK's file.write().

I needed to write my own UTF-8 BOM read/write function in PHP to fix that issue.
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Clipboard --UTF-8-->to file

27 Apr 2020, 16:42

If you need you could use UTF-8 without BOM like this:

Code: Select all

filePath := A_Desktop . "\test.txt"

File := FileOpen(filePath, "w", "UTF-8-RAW")
File.Write("фирменно")
File.Close()

File := FileOpen(filePath, "r", "UTF-8")
text := File.Read()
File.Close()

MsgBox, % text
knn
Posts: 30
Joined: 30 Mar 2020, 20:35

Re: Clipboard --UTF-8-->to file

27 Apr 2020, 17:03

Does File := FileOpen(filePath, "r", "UTF-8") handle UTF-8 with BOM automatically?
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Clipboard --UTF-8-->to file

27 Apr 2020, 17:11

If BOM presents, you don't need to specify encoding here: File := FileOpen(filePath, "r")

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Rohwedder and 374 guests