0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【AutoHotkey v2】#include を自動化する

Last updated at Posted at 2025-05-08

コードが増えてきたのでファイルを分割したのだが、ファイルを増やす度に#includeを追加するのが面倒くさい。

index.ahk
#Requires AutoHotkey v2.0

#include *i C:\Users\smicle\AutoHotkey\src\system.ahk
#include *i C:\Users\smicle\AutoHotkey\src\key\composite.ahk
#include *i C:\Users\smicle\AutoHotkey\src\key\disable.ahk
#include *i C:\Users\smicle\AutoHotkey\src\key\swap.ahk
#include *i C:\Users\smicle\AutoHotkey\src\util\execute.ahk
#include *i C:\Users\smicle\AutoHotkey\src\util\move.ahk

ディレクトリ構造

AutoHotkey/
 └src/
 |└key/
 ||└composite.ahk
 ||└disable.ahk
 ||└swap.ahk
 |└util/
 ||└execute.ahk
 ||└move.ahk
 |└system.ahk
 └index.ahk

ループを用いてincludeするコードを書いてみた

index.ahk
#Requires AutoHotkey v2.0

Loop Files, A_WorkingDir "\src\*.ahk", "R" {
  #include *i A_LoopFilePath
}

うまく読み込まれない…

index.ahk
#Requires AutoHotkey v2.0

Loop Files, A_WorkingDir "\src\*.ahk", "R" {
  MsgBox A_LoopFilePath
}

MsgBoxではちゃんと表示されるのにincludeしようとすると上手く読み込まれない…

#include はスクリプトの実行前に実行されるため、ループなどの一部に使用することはできません

とのことです。

includeするファイルを自動的に作成し、それを読み込む事で擬似的に再現できるらしい

予めinclude.ahkを設置します。

index.ahk
#Requires AutoHotkey v2.0

Create_Includes_File

#include *i include.ahk


Create_Includes_File() {
	path := A_WorkingDir "\include.ahk"

	currentIncludes := FileRead(path, "`n UTF-8")
	newIncludes := Create_Includes_List()

	if (currentIncludes != newIncludes) {
		Write_Includes_File(path, newIncludes)
	}
}

Create_Includes_List() {
	includes := "#Requires AutoHotkey v2.0`n`n"
	Loop Files, A_WorkingDir "\src\*.ahk", "R" {
		includes .= "#include *i " A_LoopFilePath "`n"
	}
	return includes
}

Write_Includes_File(path, includes) {
	_file := FileOpen(path, "w`n")
	_file.Write(includes)
	_file.Close()
	Reload()
	Exit()
}
include.ahk
#Requires AutoHotkey v2.0

#include *i C:\Users\smicle\AutoHotkey\src\system.ahk
#include *i C:\Users\smicle\AutoHotkey\src\key\composite.ahk
#include *i C:\Users\smicle\AutoHotkey\src\key\disable.ahk
#include *i C:\Users\smicle\AutoHotkey\src\key\swap.ahk
#include *i C:\Users\smicle\AutoHotkey\src\util\execute.ahk
#include *i C:\Users\smicle\AutoHotkey\src\util\move.ahk

内容としては冒頭のindex.ahkと同様のコードをinclude.ahkという名前で作成しincludeするようにしています。
無限に再帰しないようinclude.ahkの中身とループで作成したincludeを比較し、値が一致していない場合のみinclude.ahkの中身を書き換えて再度実行します。
ファイルが増えたり名前が変わったりした場合だけ2回実行される感じです。

まとめ

#include が動的に実行されないので、include用ファイルの作成と読み込みを1つにまとめることになりました。
多少ごちゃついてますが基本的には変更しないので問題ないかなという感じです。

参考文献

How to batch import ahk scripts using 'Loop Files'? - AutoHotkey Community
Loop - 構文と使用法|AutoHotkey v2
#include / #IncludeAgain - 構文と使用法|AutoHotkey v2

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?