Run ahk scripts and exe files using variable paths Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
submeg
Posts: 328
Joined: 14 Apr 2017, 20:39
Contact:

Run ahk scripts and exe files using variable paths

07 Oct 2023, 04:00

Hi all,

After some tinkering, I was able to get this to run, but I would like to understand why the file path requires quotation marks, but the path to the AHK exe (which is also a variable) does not?

Edit -

Answer (thanks @andymbody for the link):

In most cases, quote marks only have the effect of toggling the interpretation of spaces.

Code: Select all


;===================================
;SETUP
;===================================

#SingleInstance Force

;-----------------------------------
;PATHS
;-----------------------------------

AHKRootPath := "D:\AutoHotkey\"
AHKPathv1 := "D:\AutoHotkey\AutoHotkeyU64.exe"
AHKPathv2 := "D:\AutoHotkey\v2\AutoHotkey64.exe"

V1TestPath := "D:\AutoHotkey\Compiler\!TESTING\!V2\Test V1.ahk"
V2TestPath := "D:\AutoHotkey\Compiler\!TESTING\!V2\Test V2.ahk"

;===================================

;===================================
;CODE
;===================================

;-----------------------------------
;PART ONE - Version 1
;-----------------------------------
;#Requires AutoHotkey v1.1.33+

;-----------------------------------

;V1 run - full paths (works)
;run, "D:\AutoHotkey\AutoHotkeyU64.exe" "D:\AutoHotkey\Compiler\!TESTING\!V2\Test V1.ahk"
;run, "D:\AutoHotkey\v2\AutoHotkey64.exe" "D:\AutoHotkey\Compiler\!TESTING\!V2\Test V2.ahk"

;-----------------------------------

;V1 run - semi variable (works)
;-----------------------------------

;run, %AHKRootPath%\AutoHotkeyU64.exe "D:\AutoHotkey\Compiler\!TESTING\!V2\Test V1.ahk"
;run, %AHKPathv1% "D:\AutoHotkey\Compiler\!TESTING\!V2\Test V1.ahk"

;-----------------------------------

;V1 run - full variables (works)
;-----------------------------------

;run, %AHKPathv1% "%V1TestPath%"
;run, %AHKPathv2% "%V2TestPath%"

;-----------------------------------


;-----------------------------------
;PART TWO - Version 2
;-----------------------------------
#Requires Autohotkey v2.0+


Run(AHKPathv1 " `"" V1TestPath "`"")
Run(AHKPathv2 " `"" V2TestPath "`"")

Last edited by submeg on 07 Oct 2023, 05:10, edited 1 time in total.
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
User avatar
andymbody
Posts: 950
Joined: 02 Jul 2017, 23:47

Re: Run ahk scripts and exe files using variable paths

07 Oct 2023, 04:27

submeg wrote:
07 Oct 2023, 04:00
I would like to understand why the file path requires quotation marks, but the path to the AHK exe (which is also a variable) does not?
The simple explanation would be...

These do not have spaces (quotes not needed)
AHKRootPath := "D:\AutoHotkey\"
AHKPathv1 := "D:\AutoHotkey\AutoHotkeyU64.exe"
AHKPathv2 := "D:\AutoHotkey\v2\AutoHotkey64.exe"


These have spaces (quotes are needed)
V1TestPath := "D:\AutoHotkey\Compiler\!TESTING\!V2\Test V1.ahk"
V2TestPath := "D:\AutoHotkey\Compiler\!TESTING\!V2\Test V2.ahk"


A nearly identical question was asked earlier, and the developer provided an explanation in more depth

viewtopic.php?f=76&t=122069&start=20#p542180
User avatar
boiler
Posts: 17242
Joined: 21 Dec 2014, 02:44

Re: Run ahk scripts and exe files using variable paths  Topic is solved

07 Oct 2023, 04:36

By the way, there is a somewhat easier way in v2 to add quotes around the paths, which you should do for both by habit in case either may contain spaces. It uses single quotes to mark the strings so that double quotes become just normal characters and do not require escaping.

Around just the second:

Code: Select all

Run AHKPathv1 ' "' V1TestPath '"'

Around both:

Code: Select all

Run '"' AHKPathv1 '" "' V1TestPath '"'
User avatar
submeg
Posts: 328
Joined: 14 Apr 2017, 20:39
Contact:

Re: Run ahk scripts and exe files using variable paths

07 Oct 2023, 04:45

andymbody wrote:
07 Oct 2023, 04:27
submeg wrote:
07 Oct 2023, 04:00
I would like to understand why the file path requires quotation marks, but the path to the AHK exe (which is also a variable) does not?
The simple explanation would be...

These do not have spaces (quotes not needed)
AHKRootPath := "D:\AutoHotkey\"
AHKPathv1 := "D:\AutoHotkey\AutoHotkeyU64.exe"
AHKPathv2 := "D:\AutoHotkey\v2\AutoHotkey64.exe"
Yet, when I remove the spaces:

Code: Select all

AHKRootPath := D:\AutoHotkey\
AHKPathv1 := D:\AutoHotkey\AutoHotkeyU64.exe
AHKPathv2 := D:\AutoHotkey\v2\AutoHotkey64.exe
I receive the following error -
D:\AutoHotkey\Compiler\!TESTING\!V2\MainTest.ahk (11) : ==> Illegal character in expression.
Specifically: \AutoHotkey\
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
User avatar
andymbody
Posts: 950
Joined: 02 Jul 2017, 23:47

Re: Run ahk scripts and exe files using variable paths

07 Oct 2023, 04:54

The variable assignments do need quotes
AHKRootPath := "D:\AutoHotkey\"

but the quotes are not needed in the Run command for paths that look like this (no spaces)
D:\AutoHotkey\

Sorry, I guess I should not have used your assignment code to explain what I meant
afaik, this should work
run, D:\AutoHotkey\AutoHotkeyU64.exe "D:\AutoHotkey\Compiler\!TESTING\!V2\Test V1.ahk"
Last edited by andymbody on 07 Oct 2023, 05:02, edited 1 time in total.
User avatar
submeg
Posts: 328
Joined: 14 Apr 2017, 20:39
Contact:

Re: Run ahk scripts and exe files using variable paths

07 Oct 2023, 05:01

andymbody wrote:
07 Oct 2023, 04:54
The variable assignments do need quotes
AHKRootPath := "D:\AutoHotkey\"

but the quotes are not needed in the Run command for paths that look like this (no spaces)
D:\AutoHotkey\
Oh, yep, got what you mean now!
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
User avatar
submeg
Posts: 328
Joined: 14 Apr 2017, 20:39
Contact:

Re: Run ahk scripts and exe files using variable paths

07 Oct 2023, 05:07

boiler wrote:
07 Oct 2023, 04:36
By the way, there is a somewhat easier way in v2 to add quotes around the paths, which you should do for both by habit in case either may contain spaces. It uses single quotes to mark the strings so that double quotes become just normal characters and do not require escaping.
When written like that, it makes a lot more sense. I used mikeyww 's converter and tinkered until I got it to work, but now I can see it. Thanks boiler, appreciated.

Run(AHKPathv1 ' " ' V1TestPath ' " ')
Run(AHKPathv2 ' " ' V2TestPath ' " ')
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
User avatar
boiler
Posts: 17242
Joined: 21 Dec 2014, 02:44

Re: Run ahk scripts and exe files using variable paths

07 Oct 2023, 06:16

submeg wrote: I used mikeyww 's converter
Do you mean this one by mmikeww? I believe that’s a different person.
User avatar
submeg
Posts: 328
Joined: 14 Apr 2017, 20:39
Contact:

Re: Run ahk scripts and exe files using variable paths

07 Oct 2023, 17:20

boiler wrote:
07 Oct 2023, 06:16
submeg wrote: I used mikeyww 's converter
Do you mean this one by mmikeww? I believe that’s a different person.
whoa, mikeyww vs mmikeww...and yep, that's the one. It's actually guest3456 on the AHK forum!
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: kunkel321, ntepa and 40 guests