覆盖 Windows 个内置变量

Overwriting of Windows built-in variables

%path%是Windows内置环境变量。出于某种原因,当它在 AHK 代码中用作自定义输出变量时,有时 会被覆盖,有时 - 不会。为什么?

; Launch it in Windows Explorer (i.e. default file manager)

foo() {
    winGetText, path, a
    msgbox %path% ; will be overwrited with window text
    return
}

bar() {
    winGetText, winText, a
    regExMatch(winText, "Address: .*[^\r\n]", path)
    msgbox %path% ; Will not be overwrited
    return
}

f1:: foo()
f2:: bar()

我不确定这是否是它应该设计的方式,但看起来有关于如何检索 env 变量的建议。 AHK Wiki EnvGet

根据文档,解决方法是仅使用 EnvGet, OutputVar, Path 方法从环境变量中提取数据。

修复:

foo() {
    winGetText, path, a
    EnvGet, OutputVar, path
    msgbox %OutputVar% 
    return
}
f1:: foo()

目前没有关于"why"的答案,但可以使用#noEnv and envGet:

消除覆盖的歧义
#noEnv

foo() {
    winGetText, path, a
    msgBox, %path% ; Will be overwrited with window text
    return
}

bar() {
    winGetText, winText, a
    regExMatch(winText, "Address: .*[^\r\n]", path)
    msgBox, %path% ; Will be overwrited with window text
    return
}

baz() {
    ; Nothing will be here, since we disabled environment variables:
    msgBox, %path%
    ; So, if we need some environment variable, we need to invoke it:
    envGet, a_path, path
    msgBox, %a_path%
    return
}

f1:: foo()
f2:: bar()
f3:: baz()

The main point of this answer was suggested by Boiler at AHK forums.