Version 3 of Windows Environment Path

Updated 2006-03-01 08:29:45

This is a little script that allows you to edit the Windows environment path.

I HATE the built in windows method of editing the path so I whipped up this script to make it easy.

I have ONLY tested this under windows XP.

PSW


 #
 #
 #   Simple editor for fixing paths on Windows
 #
 package require Tk
 package require tile

 ##############################################################################

 namespace eval ::pathedit {
    variable list
    variable pathlist
 }

 proc ::pathedit::updateList { } {
    variable list
    variable pathlist

    $list delete 0 end
    foreach path $pathlist {
       $list insert end $path
    }
 }

 proc ::pathedit::read { } {
    variable list
    variable pathlist

    puts "pathedit::read"
    set regPath {HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment}
    set curPath [registry get $regPath "Path"]
    set pathlist [ split $curPath ";" ]
    ::pathedit::updateList
 }

 proc ::pathedit::save {  } {
    variable pathlist

    puts "::pathedit::save"
    set newPath ""
    foreach path $pathlist {
      append newPath "$path;"
   }
   puts $newPath
   set regPath {HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment}
   registry set $regPath "Path" "$newPath"
   registry broadcast "Environment"
 }

 proc ::pathedit::moveup { } {
    variable list
    variable pathlist

    set selIndex [ $list curselection ]
    if { ( [ string length $selIndex ] > 0 ) &&
         ( $selIndex > 0 ) } {
       set swapIndex [ expr { $selIndex - 1 } ]
       set select  [ lindex $pathlist $selIndex ]
       set swap [ lindex $pathlist $swapIndex ]
       lset pathlist $swapIndex $select
       lset pathlist $selIndex $swap
       ::pathedit::updateList
       $list selection set $swapIndex
       $list see $swapIndex
    }
 }

 proc ::pathedit::movedown { } {
    variable list
    variable pathlist
    set selIndex [ $list curselection ]
    set lastIndex [ $list index end ]
    if { ( [ string length $selIndex ] > 0 ) &&
         ( $selIndex < ( $lastIndex -  1)  ) } {
       set swapIndex [ expr { $selIndex + 1 } ]
       set select  [ lindex $pathlist $selIndex ]
       set swap [ lindex $pathlist $swapIndex ]
       lset pathlist $swapIndex $select
       lset pathlist $selIndex $swap
       ::pathedit::updateList
       $list selection set $swapIndex
       $list see $swapIndex
    } 
 }

 proc ::pathedit::add { }  {
    variable pathlist
    variable list

    set file [ tk_chooseDirectory -mustexist true  ]
    if { $file ne "" } {
       set selIndex [ $list curselection ]
       if { $selIndex eq "" } {
          set selIndex 0
       }
       set pathlist [ linsert $pathlist $selIndex [ file native $file ] ]
       ::pathedit::updateList
    }
 }

 proc ::pathedit::remove { } {
    variable pathlist
    variable list
    set selIndex [ $list curselection ]
    if { $selIndex ne "" } {
       set pathlist [ lreplace $pathlist $selIndex $selIndex ]
       ::pathedit::updateList
    }
 }

 proc ::pathedit::makeGui { } {
    variable list
    frame .f
    frame .b
    frame .r
    ttk::button .b.exit -text "Exit" -command exit
    ttk::button .b.read -text "Re-read" -command ::pathedit::read
    ttk::button .b.save -text "Save"    -command ::pathedit::save
    ttk::button .r.up -text "Move Up"      -command ::pathedit::moveup
    ttk::button .r.down -text "Move Down"  -command ::pathedit::movedown
    ttk::button .r.add -text "Add path"    -command ::pathedit::add
    ttk::button .r.remove -text "Remove Path" -command ::pathedit::remove
    set list [ listbox .f.list -yscrollcommand { .f.scroll set } ]
    ttk::scrollbar .f.scroll -command { .f.list yview }
    pack .r -side right -fill y
    pack .b -side bottom -fill x
    pack .f -side left -fill both -expand true
    pack .f.list -side left -fill both -expand true
    pack .f.scroll -side right -fill y
    pack .b.save .b.read .b.exit -side left
    pack .r.up .r.down  .r.add .r.remove -side top
 }

 # For Debugging 
 bind . <Alt-c> { console show }

 ######################################################3
 pathedit::makeGui
 pathedit::read

MEd 2006/03/01: I get an error dialog when I try to save: "unable to open key: access is denied" (Win XP, Service Pack 1). I guess this is because you try change the system variable "path" and I do not have administrator rights. Actually there are two path variables on my system, one "user variable" and one "system variable", as normal user I'm only allowed to change the "user variable". The real path (when you type "echo %PATH% on cmd.exe") consists of user variable + system variable


Category Windows