.
I combined the conditions in one script, so if there is a "version hint" in the code (a comment or #requires line in the beginning), in the extension or in the filepath, it will use the correct version. This also let`s you switch between 32 and 64 bit versions by mentioning it the the first lines of your script.
If everybody starts mentioning the #Required version, this script will run the correct AutoHotKey version without problem.
(Default it assumes that you have created a AutoHotKey 2 folder in the Program Files.)
Code: Select all
; Autohotkey V1
; Written by AHK_User
; Version 2021-07-21
; Universal AhkVersionLauncher
; Run correct version of ahk scripts based on hints in the code
; It decides the version based on the following rules:
; 1. Extensions like ah1, ahk1, ah2 & ahk2.
; 2. Mentioned at the first 20 lines.
; 3. Depending on the directory of the script.
exeAhkV1 := IniReadWrite("AhkVersionLauncher.ini", "Paths","exeAhkV1","C:\Program Files\AutoHotkey\AutoHotkeyU64.exe")
exeAhkV1_A32 := IniReadWrite("AhkVersionLauncher.ini", "Paths","exeAhkV1_A32","C:\Program Files\AutoHotkey\AutoHotkeyA32.exe")
exeAhkV1_U32 := IniReadWrite("AhkVersionLauncher.ini", "Paths","exeAhkV1_U32","C:\Program Files\AutoHotkey\AutoHotkeyU32.exe")
exeAhkV1_U64 := IniReadWrite("AhkVersionLauncher.ini", "Paths","exeAhkV1_U64","C:\Program Files\AutoHotkey\AutoHotkeyU64.exe")
exeAhkV2 := IniReadWrite("AhkVersionLauncher.ini", "Paths","exeAhkV2","C:\Program Files\AutoHotkey V2\AutoHotkey64.exe")
exeAhkV2_32 := IniReadWrite("AhkVersionLauncher.ini", "Paths","exeAhkV2_32","C:\Program Files\AutoHotkey V2\AutoHotkey32.exe")
exeAhkV2_64 := IniReadWrite("AhkVersionLauncher.ini", "Paths","exeAhkV2_64","C:\Program Files\AutoHotkey V2\AutoHotkey64.exe")
exeAhkDefault := IniReadWrite("AhkVersionLauncher.ini", "Paths","exeAhkDefault","C:\Program Files\AutoHotkey\AutoHotkeyU64.exe")
versionRun := "Default"
scriptPath := A_Args[1]
if (scriptPath=""){
MsgBox,64,,This script can be used to run a ahk script in the correct ahk version.`n`nIt decides the version based on the following rules:`n1. Extensions like ah1, ahk1, ah2 & ahk2.`n2. Mentioned at the first 5 lines.`n3. Depending on the directory of the script.`n`nCurrently no argument was found. Run a script with this script compiled in a exe or drop a ahk script in this script to test it.`n`nAHK V2 is coming...
}
SplitPath, scriptPath, , scriptDirectory, scriptExtension
if !FileExist(scriptPath){
MsgBox,16,Testing,Error, Could not find the file.`n%scriptPath%
}
If (InStr(scriptPath, "AHK" or InStr(scriptPath, "AutoHotkey"))){
If (InStr(scriptPath, "V2" or InStr(scriptPath, "Version 2") or InStr(scriptPath, "Version2"))){
versionRun := "V2"
}
else If (InStr(scriptPath, "V1" or InStr(scriptPath, "Version 1") or InStr(scriptPath, "Version1"))){
versionRun := "V1"
}
}
Loop, read, %scriptPath%
{
If (InStr(A_LoopReadLine, "AHK") or InStr(A_LoopReadLine, "AutoHotkey")){
If (InStr(A_LoopReadLine, "V2") or InStr(A_LoopReadLine, "Version 2") or InStr(A_LoopReadLine, "Version2")){
versionRun := "V2"
If (InStr(A_LoopReadLine, " 32") or InStr(A_LoopReadLine, "y32") or InStr(A_LoopReadLine, "k32")){
versionRun := "V2_32"
}
else If (InStr(A_LoopReadLine, " 64") or InStr(A_LoopReadLine, "y64") or InStr(A_LoopReadLine, "k64")){
versionRun := "V2_64"
}
else{
versionRun := "V2"
}
Break
}
else If (InStr(A_LoopReadLine, "V1" or InStr(A_LoopReadLine, "Version 1") or InStr(A_LoopReadLine, "Version1"))){
If (InStr(A_LoopReadLine, " A32")){
versionRun := "V1_A32"
}
else If (InStr(A_LoopReadLine, " 32") or InStr(A_LoopReadLine, "y32") or InStr(A_LoopReadLine, "k32") or InStr(A_LoopReadLine, "U32")){
versionRun := "V1_U32"
}
else If (InStr(A_LoopReadLine, " 64") or InStr(A_LoopReadLine, "y64") or InStr(A_LoopReadLine, "k64")){
versionRun := "V1_U64"
}
else{
versionRun := "V1"
}
Break
}
}
if (A_Index=20){
Break
}
}
If (scriptExtension = "ahk2" || scriptExtension = "ah2"){
versionRun := "V2"
}
If (scriptExtension = "ahk1" || scriptExtension = "ah1"){
versionRun := "V1"
}
exeAhkRun := exeAhk%VersionRun%
if !FileExist(exeAhkRun){
FileSelectFile, exeAhkNew, , C:\Programs, Please select the correct AutoHotkey %VersionRun% exe file., exicutable(.exe)
if (exeAhkNew = ""){
}
else{
MsgBox,68,Testing,Are you sure that the following file is the correct exicutable for version %VersionRun%?`n`n%exeAhkNew%
IfMsgBox, No
{
ExitApp
}
IniWrite, %exeAhkNew%, AhkVersionLauncher.ini, Paths, exeAhk%VersionRun%
exeAhkRun := exeAhkNew
}
}
if !FileExist(exeAhkRun){
MsgBox,16,Testing,Error, Could not find the file.`n%exeAhkRun%
ExitApp
}
Loop, % A_Args.length()
{
Arguments .= " """ A_Args[A_Index] """"
}
Run, % exeAhkRun Arguments
ExitApp
IniReadWrite(SettingsFile, Section,Key,DefaultValue){
IniRead, Value, %SettingsFile%, %Section%, %Key%, %DefaultValue%
if (InStr(DefaultValue,"`n") and Value = DefaultValue){
InputBox, Value, Settings - %Section% - %Key%, %DefaultValue%
}
IniWrite, %Value%, %SettingsFile%, %Section%, %Key%
Return %Value%
}