1

Setup

I have an AutoHotKey script that shows the value of a Windows Environment Variable, crnt:

#c:: MsgBox, "crnt is " . %crnt%

The Problem

When I change crnt #c still shows the old value.

To Demonstrate:

  • crnt is set to PATH_1
  1. I press #c > "crnt is PATH_1"
  2. I change crnt to "PATH_2"
  3. I press #c > "crnt is PATH_1"

Undesired Outcome

crnt is PATH_1

Desired Outcome

crnt is PATH_2

Things I've Tried

without ahk:

Right-clicking the file and selecting "Run Script" is the only way I have found that successfully reloads the value of crnt.

  • However, this is tedious, and what I'm trying to automate.

Also, of course, exiting out of the script, and other manual operations.

with ahk

From the "Run A Script" section of the Auto Hot Key documentation:

"Call AutoHotkey.exe on the command line and pass the script's filename as a command-line parameter.}

  • This leads me to believe the following command should work:
  • C:\Program Files\AutoHotkey\> AutoHotkey.exe path\to\my\file.ahk.
  • It doesn't (I still get "crnt is PATH_1"). And I DO NOT get an error.

Reload:

  • #^c::Reload

I was hoping adding this to my script would allow the #^c hotkey to make the script reload the value of crnt, it does not, #c still gives PATH_1.

Extra Notes

How I am changing crnt

without ahk:

  1. Open 'Advanced System Settings'
  2. Advanced tab
  3. Environmental Variables
  4. Highlight crnt
  5. Click Edit
  6. Change the value to PATH_2
  7. OK, OK, OK
  • I can confirm this works by opening cmd, running echo %crnt%, seeing PATH_2.
  • Even though cmd shows the updated value, my ahk script does not show PATH_2.

with ahk:

Run, cmd
Send, SET crnt=PATH_2{Enter}
Send, SETX crnt PATH_2{Enter}
  • Again, I have confirmed the value has changed, but the ahk script still shows PATH_1.

1 Answer 1

1

You need to manually reload the value of crnt with EnvSet.

The reason for this behavior is that AutoHotkey scripts import a copy of all system and user environmental variables when the script is first loaded. All processes spawned by this AutoHotkey script inherit the current set of environment variables.

When you use the Reload command, you are reloading the script using the script itself. Therefore the reloaded script inherits the environmental variables of the original script.

MsgBox % "Inherited value:`t" temp "`nActual value:`t" Env_UserRead("temp")

Env_UserNew("temp", "foobar")

q:: reload

; reset enviromental variables
Esc::
Env_UserNew("temp", Env_UserRead("tmp"))
MsgBox % "Restoring Environmental Variables...`nInherited value:`t" temp "`nActual value:`t" Env_UserRead("temp")
ExitApp

; From https://github.com/iseahound/Environment.ahk/blob/master/Environment.ahk
Env_UserNew(name, value := "", type := "", location := ""){
   type := (type) ? type : (value ~= "%") ? "REG_EXPAND_SZ" : "REG_SZ"
   RegWrite % type , % (location == "") ? "HKEY_CURRENT_USER\Environment" : location, % name, % value
   return (ErrorLevel) ? -1 : 0
}

Env_UserRead(name, value := "", location := ""){
   RegRead registry, % (location == "") ? "HKEY_CURRENT_USER\Environment" : location, % name
   if (value) {
      Loop Parse, registry, `;
      {
         if (A_LoopField = value) {
            return A_LoopField
         }
      }
      return ; Value not found
   }
   return registry
}

The above script shows this concept in action. First, the script loads with the "normal" environmental variables. The script then makes a change to the temp variable to read foobar. However, when you press q to reload the script, it still perceives the temp variable as normal. That's because the Reload command inherits env vars from the previous session, and does not check for new environment variables! Press Esc to restore your variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.