覆盖Windows内置变量

时间:2018-02-20 14:38:49

标签: autohotkey

%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()

2 个答案:

答案 0 :(得分:0)

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

根据文档,修复将仅使用EnvGet, OutputVar, Path方法从环境变量中提取数据。

修复:

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

答案 1 :(得分:0)

目前没有关于"为什么"的答案,但使用#noEnvenvGet可以消除覆盖的模糊性:

#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.