Advertisement
Najeebsk

Environment-Variables.ahk

Jul 7th, 2022
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #SingleInstance, Force
  2. #NoEnv ; somewhat ironic...
  3.    
  4.     ; this won't work in vista/7 if it's not run as an administrator.
  5.     ; i'm too lazy to request it manually so the easiest way is to
  6.     ; compile this script, and then under Compatibility tab in the
  7.     ; compiled exe's Properties select "Run as Administrator"
  8.    
  9.     RegRead, P, HKLM, SYSTEM\CurrentControlSet\Control\Session Manager\Environment, PATH
  10.    
  11.     Gui, +Delimiter`;
  12.    
  13.     width := 400
  14.     Gui, Add, Text, w%width%, Double Click an entry to modify it. Click Add to make a new entry and Delete to remove the selected entry.
  15.     Gui, Add, ListBox, vSysPath w%width% r8 +0x100 gEditEntry AltSubmit, %P%
  16.     Gui, Font, s12
  17.     Gui, Add, Button, gNew    w70 section  , New
  18.     Gui, Add, Button, gDelete w70 ys xs+74 , Delete
  19.     Gui, Add, Button, gExit   w70 ys xs+330, Cancel
  20.     Gui, Add, Button, gSubmit w70 ys xp-74, Submit
  21.     Gui, Show, , SysEnv -- Change System Environment Variable
  22. return
  23.  
  24. EditEntry:
  25.     if (a_guievent == "DoubleClick" && a_eventinfo) {
  26.         Gui +OwnDialogs
  27.         RegExMatch(P, "P)^(?:(?<E>[^;]*)(?:;|$)){" a_eventinfo "}", _)
  28.         _En := IB( "Edit PATH entry", substr(P, _PosE, _LenE))
  29.         if (ErrorLevel == 0)
  30.         {
  31.             if (InStr(FileExist(ExpEnv(_En)),"D"))
  32.             {
  33.                 rP := substr(P, 1, _PosE-1) _En (_PosE+_LenE+1 < strlen(P) ? ";" substr(P, _PosE+_LenE+1) : "")
  34.                 P := rP
  35.                 GuiControl, , SysPath, `;%P%
  36.             }
  37.             else
  38.                 MsgBox, , SysEnv, Path is not a directory.
  39.         }
  40.     }
  41. return
  42.  
  43. New:
  44.     add := IB( "Add PATH entry" )
  45.     if (instr(fileexist(ExpEnv(add)),"D")) {
  46.         P .= (strlen(P) > 0 ? ";" : "") add
  47.         GuiControl, , SysPath, `;%P%
  48.     }
  49. return
  50.  
  51. Delete:
  52.     GuiControlGet, entry, , SysPath
  53.     if (entry) {
  54.         Gui, +OwnDialogs
  55.         MsgBox, 4, SysEnv -- Confirm, Remove entry #%entry% from PATH?
  56.         IfMsgBox Yes
  57.         {
  58.             RegExMatch(P, "P)^(?:(?<E>[^;]*)(?:;|$)){" entry "}", _)
  59.             P := SubStr(P, 1, max(_PosE-2,0)) (_PosE+_LenE+1 < strlen(P) ? ";" substr(P, _PosE+_LenE+1) : "")
  60.             GuiControl, , SysPath, `;%P%
  61.         }
  62.     }
  63. return
  64.  
  65. Submit:
  66.     Gui, +OwnDialogs
  67.     MsgBox, 1, SysEnv -- Save Changes, Change system PATH to:`n`n%P%
  68.     IfMsgBox, OK
  69.     {
  70.         RegWrite, REG_EXPAND_SZ, HKLM, SYSTEM\CurrentControlSet\Control\Session Manager\Environment, PATH, %P%
  71.         If !ErrorLevel
  72.             MsgBox, 0, SysEnv -- Success!, Modifying the PATH variable was successful!
  73.         Else
  74.             MsgBox,  , SysEnv, Error has occurred and new PATH variable was not saved.
  75.     }
  76.     Else
  77.         MsgBox, , SysEnv -- Cancelled, Exiting now.
  78.  
  79. Exit:
  80. GuiClose:
  81. GuiEscape:
  82. GuiEsc:
  83. ExitApp
  84.  
  85. IB( prompt, default="" ) {
  86.     InputBox, out, SysEnv -- %prompt%, %prompt%:, , , , , , , , %default%
  87.     return out
  88. }
  89.  
  90. max( a, b ) {
  91.     return a > b ? a : b
  92. }
  93.  
  94. ExpEnv(str) {
  95.     ; by Lexikos: http://www.autohotkey.com/forum/viewtopic.php?p=327849#327849
  96.     if sz:=DllCall("ExpandEnvironmentStrings", "uint", &str, "uint", 0, "uint", 0)
  97.     {
  98.         VarSetCapacity(dst, A_IsUnicode ? sz*2:sz)
  99.         if DllCall("ExpandEnvironmentStrings", "uint", &str, "str", dst, "uint", sz)
  100.             return dst
  101.     }
  102.     return ""
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement