Is there any way to run v1 and v2 scripts separately? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
william_ahk
Posts: 481
Joined: 03 Dec 2018, 20:02

Is there any way to run v1 and v2 scripts separately?

12 May 2020, 04:40

Many existing scripts were written in AHK v1 and it would be a huge amount of effort to translate them into v2. So some scripts will have to run with v1 while others with v2. Is there any way to make this switching easier? For example, scripts in a particular folder will run with AHK v1 by default while the rest of the scripts will run with v2? Currently I'm using a shortcut to AHK v1 program with startup script params, which is not so convenient or native.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Is there any way to run v1 and v2 scripts separately?  Topic is solved

12 May 2020, 08:32

  • u can define another .ahk extension(eg .ahk2) and associate it with the v2 interpreter
  • u can make a compiled launcher exe and associate ur .ahk files with it instead, eg:

    Code: Select all

    v1exe := EnvGet('AHK_V1_EXE') || 'C:\Program Files\AutoHotkey\AutoHotkeyU64.exe'
    v2exe := EnvGet('AHK_V2_EXE') || 'C:\Program Files\AutoHotkey v2\AutoHotkeyU64.exe'
    
    scriptPath := A_Args[1]
    SplitPath(scriptPath, , dir)
    ahkexe := dir ~= 'my_v1_exclusive_scripts_folder' ? v1exe : v2exe
    Run(ahkexe ' ' scriptPath)
  • u can add a right click context menu item command to run a script with v1/v2
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Is there any way to run v1 and v2 scripts separately?

12 May 2020, 19:17

@swagfag nice, I like your example launcher
william_ahk
Posts: 481
Joined: 03 Dec 2018, 20:02

Re: Is there any way to run v1 and v2 scripts separately?

13 May 2020, 07:16

@swagfag Thanks! The launcher method was well thought and implemented.
lexikos
Posts: 9552
Joined: 30 Sep 2013, 04:07
Contact:

Re: Is there any way to run v1 and v2 scripts separately?

29 May 2020, 19:16

Since each v2 release may require changes to each script, my preferred approach is to use a shortcut to launch the script with a compatible exe.

Some other options:
  • Copy the latest known compatible exe, name it the same as the script, and run the exe directly.
  • Compile scripts.
  • Open the script in SciTE4AutoHotkey first, and
    • use the platform selector to choose which version to run with.
    • use SciTEDirectory.properties to set the exe for a given project.
  • Register additional shell verbs (explorer context menu options).
I only have one v2 script that I use daily, and for it I would have created a shortcut anyway. For every other v2 script, I generally open it in SciTE4AutoHotkey first. I don't bother with compiling.

Shell verbs:
See the Open and uiAccess keys under HKEY_CLASSES_ROOT\AutoHotkeyScript\Shell for example. If you break something in the AutoHotkeyScript key, you can just reinstall AutoHotkey (maybe delete the key first). You may need to delete the following key if you've used "open with", "change program", "default apps", etc. to change the program associated with .ahk files:
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ahk\UserChoice
make a compiled launcher exe
Although compiling a script makes it easier to associate files with the script, it is not necessary - you can register a file type to run "%PROGRAMFILES%\AutoHotkey\AutoHotkey.exe" "Path\of\your\script.ahk" "%1" (%1 gets replaced with the file path). The easiest way for a new file type is probably with the FTYPE and ASSOC commands at a command prompt. You can type FTYPE /? for help. Some people might find it easier to recompile every time they edit, but I prefer not to compile.
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: Is there any way to run v1 and v2 scripts separately?

04 Jun 2020, 02:15

My AutoHotkey Portable Installer script does this as well.

This tool is meant for running several versions of AHK side by side, with just a 1-line comment, like ; AHK v2 or ; AHK v1, as the first line in a script to determine the version. If no such comment exists, then the installed version of AHK is run as the fallback.

It also handles writing to the registry to associate the .ahk extension in the system. Install and uninstall is easy with this program.

I recommend running it compiled.
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Is there any way to run v1 and v2 scripts separately?

04 Jun 2020, 08:15

I posted a script that lets you run v2 scripts with one click while v1.1 is your primarily installation called Run Alt AHK Version (RAAV), and there is also a version written in v2. No other changes to your system or scripts are necessary other than to have the RAAV script running.
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Is there any way to run v1 and v2 scripts separately?

27 Jul 2021, 15:45

Great Idea!!! And usefull now Beta is out :D .

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.)

The Launcher:

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%
}
Mini example Scripts:

Code: Select all

;ahk v1
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
MsgBox,  % A_AhkVersion

Code: Select all

#Requires AutoHotKey v2.0-a
MsgBox( A_AhkVersion)
20170201225639
Posts: 144
Joined: 01 Feb 2017, 22:57

Re: Is there any way to run v1 and v2 scripts separately?

08 Oct 2021, 02:18

> Copy the latest known compatible exe, name it the same as the script, and run the exe directly.


This is the method that I found most convenient. Just drop an .ahk file, name it AutoHotkey64.ahk and put it in the same folder as the v2 exe, and double click the exe (not the .ahk). No need to mess around with system settings whatever.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: CastleChou, Descolada, Noitalommi_2, Rohwedder and 29 guests