#Include / #IncludeAgain

指定されたファイルの内容がこの位置に存在するかのようにスクリプトを動作させます。

#Include FileOrDirName
#Include <LibName>
#IncludeAgain FileOrDirName

パラメータ

FileOrDirName

型:文字列

以下に説明する、ファイルまたはディレクトリのパス。This must not contain double quotes (except for an optional pair of double quotes surrounding the parameter), wildcards or escape sequences other than semicolon (`;).

組込変数は、パーセント記号で囲んで使用することができます(例えば、#Include "%A_ScriptDir%")。有効な変数参照の一部でないパーセント記号は、文字通りに解釈されます。A_Argsと、組み込みクラスを除く、すべての組込変数が有効です。

既知の制限:スクリプトをコンパイルする際、変数はコンパイラによって評価され、最終的に実行されたときにスクリプトが返す値とは異なる場合があります。以下の変数に対応:A_AhkPath, A_AppData, A_AppDataCommon, A_ComputerName, A_ComSpec, A_Desktop, A_DesktopCommon, A_IsCompiled, A_LineFile, A_MyDocuments, A_ProgramFiles, A_Programs, A_ProgramsCommon, A_ScriptDir, A_ScriptFullPath, A_ScriptName, A_Space, A_StartMenu, A_StartMenuCommon, A_Startup, A_StartupCommon, A_Tab, A_Temp, A_UserName, A_WinDir.

ファイル:インクルードするファイル名。デフォルトでは、相対パスは#Includeディレクティブを含むファイルのディレクトリからの相対パスとなります。このデフォルトは、後述の#Include Dirを使用することで上書きすることができます。注:SetWorkingDirは、スクリプトの実行を開始する前に#Includeが処理されるため、#Includeに影響を与えません。

ディレクトリ:ファイルではなくディレクトリを指定することで、それ以降に出現する現在のファイル内の#IncludeとFileInstallが使用する作業ディレクトリを変更します。注:この方法で作業ディレクトリを変更しても、スクリプトの実行開始時の初期作業ディレクトリ(A_WorkingDir)には影響しない。これを変更するには、スクリプトの先頭でSetWorkingDirを使用します。

注:このパラメータは式ではなく、引用符(「シングル」または「ダブル」のいずれか)で囲むことができます。

<LibName>

型:文字列

ライブラリファイル名または関数名です。For example, #Include <lib> and #Include <lib_func> would both include lib.ahk from one of the Lib folders. Variable references are not allowed.

備考

スクリプトは、#Includeディレクティブの正確な位置に、インクルードファイルのコンテンツが物理的に存在するかのように動作します(まるでインクルードファイルからコピー&ペーストが行われたかのようです)。そのため、一般的に、孤立した2つのスクリプトを1つの機能するスクリプトに統合することはできません。

#Include ensures that the specified file is included only once, even if multiple inclusions are encountered for it. これに対して、#IncludeAgainは、他のすべての点で#Includeと同じでありながら、同じファイルを複数回インクルードすることができます。

The file path may optionally be preceded by *i and a single space, which causes the program to ignore any failure to read the file. 事例:#Include "*i SpecialOptions.ahk". This option should be used only when the file's contents are not essential to the main script's operation.

Lines displayed in the main window via ListLines or the menu View->Lines are always numbered according to their physical order within their own files. In other words, including a new file will change the line numbering of the main script file by only one line, namely that of the #Include line itself (except for compiled scripts, which merge their included files into one big script at the time of compilation).

#Includeは、外部ファイルで定義された関数を読み込むためによく使われます。

他のディレクティブと同様に、#Includeは条件付きで実行することはできません。つまり、この例では期待通りに動作しないことになります。

if (x = 1)
    #Include "SomeFile.ahk"  ; この行は、xの値に関係なく有効になります。

Script Library Folders, Functions, FileInstall

指定されたファイルの内容を、現在のスクリプトに取り込みます。

#Include "C:\My Documents\Scripts\Utility Subroutines.ahk"

以降の#IncludesとFileInstallsの作業ディレクトリを変更します。

#Include "%A_ScriptDir%"

上記と同じですが、明示的に名前を付けたディレクトリの場合です。

#Include "C:\My Scripts"