autohotkey bug

Preventing Japanese Character Encoding Errors When Reading Files with AutoHotKey

How to handle cases where Japanese, kanji characters, and other non-alphabetic characters become garbled when reading files with AutoHotKey, Windows PC automation software.

Shou Arisaka
2 min read
Nov 25, 2025

How to handle cases where Japanese, kanji characters, and other non-alphabetic characters become garbled when reading files with AutoHotKey, Windows PC automation software.

Image

I’ve written several articles about handling character encoding issues in AutoHotKey, but this time it’s a bit more advanced. With my programmer’s intuition, I could solve it immediately, but if you only know AutoHotKey, the solution might be difficult. But don’t worry. This article will solve it.

I had implemented a function in AutoHotKey to execute bash commands and return standard output as follows:

 getexec(command){

   ; Run, bash.exe -c '%command% > /mnt/c/pg/autohotkey/getexec.tmp.txt', ,hide
   RunWait, bash.exe -c '%command% > /mnt/c/pg/autohotkey/getexec.tmp.txt', ,hide

   ; sleep 200
   sleep 50

   FileRead, OutputVar, C:/pg/autohotkey/getexec.tmp.txt

   return OutputVar

 }

Usually there was no Japanese mixed in like this:

:*:isodate ::
  sendText( RegExReplace( getexec("date +%FT%T%Z") , "\s*(\n|\r\n)$", A_Space) )
return

But I needed output containing Japanese, so I tested it:

:*:hoge ::
  sendText( RegExReplace( getexec("bash -c date") , "\s*(\n|\r\n)$", A_Space) )  
return

Image

It was perfectly garbled.

Now for the solution right away.

There are two ways:

  • Place FileEncoding , UTF-8
  • Use FileOpen instead of FileRead

Either way, first check that the character encoding and BOM of the AutoHotKey file are properly set. This is common knowledge among AutoHotKey users.

Image

Solved instantly with the following command:

nkf --overwrite --oc=UTF-8-BOM dev.ahk

First method:

AutoHotKey allows setting a default file encoding, so define this variable.

FileEncoding - Syntax & Usage | AutoHotkey

So it becomes:

 getexec(command){

   ; Run, bash.exe -c '%command% > /mnt/c/pg/autohotkey/getexec.tmp.txt', ,hide
   RunWait, bash.exe -c '%command% > /mnt/c/pg/autohotkey/getexec.tmp.txt', ,hide

   ; sleep 200
   sleep 50

   FileEncoding , UTF-8
   FileRead, OutputVar, C:/pg/autohotkey/getexec.tmp.txt

   return OutputVar

 }

This works, but note: this is meaningless unless defined within a function, so don’t define it globally. I could define it globally. However, it needs to be defined before the function definition, so be careful. Put it around the first line.

Second method:

Using FileOpen.

Force a file to be read as UTF-8? - AutoHotkey Community FileOpen() - Syntax & Usage | AutoHotkey

It becomes:

getexec(command){

  RunWait, bash.exe -c '%command% > /mnt/c/pg/autohotkey/getexec.tmp.txt', ,hide

  ; sleep 200
  sleep 50

  ; FileRead, OutputVar, C:/pg/autohotkey/getexec.tmp.txt
  file := FileOpen("C:/pg/autohotkey/getexec.tmp.txt", "r", "UTF-8")
  content := file.Read()

  return content

}

Either method works, but FileEncoding, which was discovered as a shortcut later, seems more convenient to use.

Share this article

Shou Arisaka Nov 25, 2025

🔗 Copy Links