Using cli to execute scripts meets error while both v1&v2 installed Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
pilgrimlyieu
Posts: 9
Joined: 15 Jul 2023, 03:58

Using cli to execute scripts meets error while both v1&v2 installed

15 Jul 2023, 04:22

For some reason, I have both v1(1.1.37.01) and v2(2.0.4) installed via the second method in https://www.autohotkey.com/docs/v2/Program.htm#install_v1. And it can work normal if I double click scripts requiring different version of AutoHotkey.

However error occurs when I execute scripts using cli with the command autohotkey.exe ‪C:\Users\Administrator\Desktop\test.ahk. The content of test.ahk is as followings.

Code: Select all

#Requires AutoHotkey v2.0+

msgbox Test
And this is the error message.
Error at line 1.
This script requires AutoHotkey v 2.0+.
Current interpreter: AutoHotkey v 1.1.37.01 Unicode 64-bit
C:\Program Files\AutoHotkey\AutoHotkey. exe
The program will exit.
I think that AutoHotkey.exe in AutoHotkey folder is for v1, not v2, which causing the issue.

So is there any way to execute AutoHotkey scripts that can automatically determine the version(or open a window to let the user determine the version) using cli?

I've searched the forum. However since my English is not good, I could only find this viewtopic.php?f=82&t=112096 similar to my issue. But I still don't know how to solve it. Additionally, I've also tried to install two versions via the fisrt method in https://www.autohotkey.com/docs/v2/Program.htm#install_v1 and autohotkey.exe has no effect in this case.

Can somebody help me? Thanks in advance!
User avatar
mikeyww
Posts: 27196
Joined: 09 Sep 2014, 18:38

Re: Using cli to execute scripts meets error while both v1&v2 installed

15 Jul 2023, 05:26

Yes. The answer is here: Command-line usage.

If using an AutoHotkey script, you might also be able to call the Run function directly with your script path, omitting the .exe.
pilgrimlyieu
Posts: 9
Joined: 15 Jul 2023, 03:58

Re: Using cli to execute scripts meets error while both v1&v2 installed

15 Jul 2023, 06:43

mikeyww wrote:
15 Jul 2023, 05:26
Yes. The answer is here: Command-line usage.

If using an AutoHotkey script, you might also be able to call the Run function directly with your script path, omitting the .exe.
Thanks. It works but in an unexpected way(at least for me). The tutorial uses the following command

Code: Select all

AutoHotkeyUX.exe launcher.ahk [Switches] [Script Filename] [Script Parameters]
which in my case raises a warning
'AutoHotkeyUX.exe' is not recognized as an internal or external command, operable program or batch file'.
And if I add AutoHotkey\UX to environment variable PATH, the warning changes
Script file not found.
launcher.ahk
So I have to use full paths, in my circumstance is "C:\Program Files\AutoHotkey\UX\AutoHotkeyUX.exe" "C:\Program Files\AutoHotkey\UX\launcher.ahk" C:\Users\Administrator\Desktop\test.ahk. Then it works.

Is my solution right? Or I've misunderstood you?
User avatar
mikeyww
Posts: 27196
Joined: 09 Sep 2014, 18:38

Re: Using cli to execute scripts meets error while both v1&v2 installed

15 Jul 2023, 08:12

Yep, use full paths. It always works, regardless of the current working directory or environment variables.
pilgrimlyieu
Posts: 9
Joined: 15 Jul 2023, 03:58

Re: Using cli to execute scripts meets error while both v1&v2 installed

15 Jul 2023, 08:41

mikeyww wrote:
15 Jul 2023, 08:12
Yep, use full paths. It always works, regardless of the current working directory or environment variables.
Useful but not pretty. Anyway thank you very much!
User avatar
mikeyww
Posts: 27196
Joined: 09 Sep 2014, 18:38

Re: Using cli to execute scripts meets error while both v1&v2 installed  Topic is solved

15 Jul 2023, 08:51

Can do like this.

Code: Select all

SET uxDir=%ProgramFiles%\AutoHotkey\UX
SET script=%USERPROFILE%\Desktop\test.ahk
START "Script" "%uxDir%\AutoHotkeyUX.exe" "%uxDir%\launcher.ahk" "%script%"
Simpler:

Code: Select all

SET script=%USERPROFILE%\Desktop\test.ahk
START "Script" "%script%"
pilgrimlyieu
Posts: 9
Joined: 15 Jul 2023, 03:58

Re: Using cli to execute scripts meets error while both v1&v2 installed

15 Jul 2023, 09:12

mikeyww wrote:
15 Jul 2023, 08:51
Can do like this.

Code: Select all

SET uxDir=%ProgramFiles%\AutoHotkey\UX
SET script=%USERPROFILE%\Desktop\test.ahk
START "Script" "%uxDir%\AutoHotkeyUX.exe" "%uxDir%\launcher.ahk" "%script%"
Simpler:

Code: Select all

SET script=%USERPROFILE%\Desktop\test.ahk
START "Script" "%script%"
Nice! Thanks a lot!
lexikos
Posts: 9635
Joined: 30 Sep 2013, 04:07
Contact:

Re: Using cli to execute scripts meets error while both v1&v2 installed

16 Jul 2023, 05:54

It is not necessary to use START "Script" to execute an AutoHotkey script (or to open any other kind of file) from within a batch script. You can just execute the script directly, as in

Code: Select all

"%USERPROFILE%\Desktop\test.ahk"
The difference START makes is that the batch script will continue executing immediately, instead of waiting for the program to exit.

The first parameter of START is the title of the console window, but since AutoHotkey isn't a console app, it is ignored. If you need to use START, you can use START "" "...ahk".

(START "...ahk" would just open a new console window with that title.)

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Albireo and 39 guests