AHK v2 question: Running a file with chinese characters throws an error in v2, not in v1 Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

AHK v2 question: Running a file with chinese characters throws an error in v2, not in v1

Post by Maestr0 » 09 Apr 2019, 08:32

1. make a text file with chinese chars in it, like 你好.txt
2. make a v1 script that does this (and then test it):

Code: Select all

file := A_Scriptdir "\你好.txt"
run, % file
3. make a v2 script that does this (and then test it):

Code: Select all

file := A_Scriptdir "\你好.txt"
run file
Now, the weird thing is, the code works for me in v1, but not in v2.... Why would that be?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: AHK v2 question: Running a file with chinese characters throws an error in v2, not in v1

Post by jeeswg » 09 Apr 2019, 09:01

Both worked OK for me, did you save the file as UTF-8/UTF-16 with a BOM?
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
HakitoJin
Posts: 17
Joined: 02 Oct 2016, 17:20

Re: AHK v2 question: Running a file with chinese characters throws an error in v2, not in v1  Topic is solved

Post by HakitoJin » 09 Apr 2019, 09:05

When notepad++ saves a file as UTF-8, there is literally no difference between that file, and the same file saved as ANSI.

So autohotkey loads the UTF-8 file as ANSI, because there is no way to distinguish it (no idea why your ahk1 loads it as UTF-8 though).
Also, when you save the same file as UTF-8 in normal notepad, it actually uses UTF-8-BOM, which adds a character at the very start of the file.

Here is a comparison between the same file in different formats: i.imgur.com/kpYuK3A.jpg
All you need to test this is: msgbox "你好"


Update:


I think your autohotkey v1 registry file association cmd might have /CP65001 as a parameter, which tells it to use UTF-8 by default.

Go check if the default value in HKEY_CLASSES_ROOT\AutoHotkeyScript\Shell\Open\Command has it.
It might look a little something like this "C:\Program Files\AutoHotkey\AutoHotkey.exe" /CP65001 "%1" %*
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Re: AHK v2 question: Running a file with chinese characters throws an error in v2, not in v1

Post by Maestr0 » 09 Apr 2019, 11:46

jeeswg wrote:
09 Apr 2019, 09:01
Both worked OK for me, did you save the file as UTF-8/UTF-16 with a BOM?
Saving as UTF-8 and UTF-8-BOM made no difference in the result. It worked in V1 and not in V2.
HakitoJin wrote:
09 Apr 2019, 09:05
When notepad++ saves a file as UTF-8, there is literally no difference between that file, and the same file saved as ANSI.

So autohotkey loads the UTF-8 file as ANSI, because there is no way to distinguish it (no idea why your ahk1 loads it as UTF-8 though).
Also, when you save the same file as UTF-8 in normal notepad, it actually uses UTF-8-BOM, which adds a character at the very start of the file.

Here is a comparison between the same file in different formats: i.imgur.com/kpYuK3A.jpg
All you need to test this is: msgbox "你好"
It shows a msgbox in v2 with this:
---------------------------
v2.ahk2
---------------------------
你好
---------------------------
OK
---------------------------
HakitoJin wrote:
09 Apr 2019, 09:05

Update:


I think your autohotkey v1 registry file association cmd might have /CP65001 as a parameter, which tells it to use UTF-8 by default.

Go check if the default value in HKEY_CLASSES_ROOT\AutoHotkeyScript\Shell\Open\Command has it.
It might look a little something like this "C:\Program Files\AutoHotkey\AutoHotkey.exe" /CP65001 "%1" %*
This is my registry value there:
"C:\Program Files\AutoHotkey\AutoHotkey.exe" "%1" %*

The exact error my v2 gives is this one:
---------------------------
v2.ahk2
---------------------------
Error: Failed attempt to launch program or document:
Action: <C:\ahk\你好.txt>
Params: <>

Specifically: The system cannot find the file specified.



Line#
002: file := A_ScriptDir "\你好.txt"
---> 003: run(A_ScriptDir "\你好.txt")
004: Exit
005: Exit
005: Exit

The current thread will exit.
---------------------------
OK
---------------------------
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Re: AHK v2 question: Running a file with chinese characters throws an error in v2, not in v1

Post by Maestr0 » 09 Apr 2019, 12:01

Right, so, the issue was with the encoding of the script and not the txt file. For some reason, my v1.ahk script was encoded as BOM already (don't recall having done that, it's an old file I keep re-using), but my v2.ahk2 script was not.

Special thanks to Ħakito to finally make me understand :)
Thanks to everyone who has helped and tried to help!
Post Reply

Return to “Ask for Help (v2)”