Code: Select all
Rename()
{
NewName := A_ScriptName ;-- Grabs current script name (script.ahk)
StringTrimRight, NewName, NewName, 3 ;-- Removes the AHK extension
NewName .= "exe" ;-- Appends EXE extension
StringSplit, AHKName, A_AHKPath, \ ;-- Takes current AHK executable path and splits it by \
Loopy := AHKName0 - 1 ;-- Sets the loop iteration count to one less than the parts of the file path
Loop, %Loopy% ;-- Loops through file path parts
AHKDir .= AHKName%A_Index% . "\" ;-- Rebuilds AHK path. Leaves off executable name
AHK_CB_Dir := AHKDir . NewName ;--Appends the current script's name with EXE extension to the AHK path
AHK_AH_Dir := AHKDir . "AutoHotkey.exe" ;-- Build default AHK path
ScriptDir := A_ScriptFullPath ;-- Saves current script path including file name
Name := AHKName%AHKName0% ;-- Grabs the last portion of the split AHK path
If (Name != NewName) ;-- Tests to see if script name matches AHK name
{
FileMove, %AHK_AH_Dir%, %AHK_CB_Dir% ;-- Renames AutoHotkey.exe to current script.exe
If ErrorLevel ;-- Unable to rename?
{
MsgBox, Unable to rename AutoHotkey.exe
Run, %AHKDir%
ExitApp
}
IfExist, %AHK_CB_Dir% ;-- Verifies new AHK executable is in place
Run, %AHK_CB_Dir% %A_ScriptFullPath%, UseErrorLevel ;-- Reloads script via new AHK name
If ErrorLevel ;-- Unable to run new ahk name?
{
MsgBox, Error running %A_ScriptFullPath%
FileMove, %AHK_CB_Dir%, %AHK_AH_Dir%
ExitApp
}
ExitApp ;-- Ensures rest of code does not run after
}
else ;-- AHK path matches script name
{
FileMove, %A_AHKPath%, %AHKDir%AutoHotkey.exe ;-- Renames AHK back to AutoHotkey.exe
If ErrorLevel ;-- Unable to rename?
{
MsgBox, Unable to rename %NewName%
Run, %AHKDir%
ExitApp
}
}
}