How does one .ahk / .exe script pass parameters to another script?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
TXShooter
Posts: 165
Joined: 13 Dec 2017, 09:27

How does one .ahk / .exe script pass parameters to another script?

20 Jul 2018, 13:55

I've written several file processing scripts that work great from within a single .exe file. I have a need to break them up so that other calling scripts can access them.

How does one script pass a parameter (or 20) to another script without using the clipboard (like how a Windows Shortcut functions)?

If it were up to using the Command Line, then so be it... I just didn't think it needed to go that far when using a "Run, program.exe" function.

Am I wrong?

Thanks,
TXShooter
TXShooter
Posts: 165
Joined: 13 Dec 2017, 09:27

Re: How does one .ahk / .exe script pass parameters to another script?

20 Jul 2018, 14:14

For example,

Calling script:
RunWait, Test.AHK "c:\test - file number 1.dat"

Callee script, Test.AHK:
tFilename := %1%

My current implementation of this isn't working. I'm getting "Error: The following variable name contains an illegal character: "c:\test - file number 1.dat" "
Variable name -> tFilename:=%1%
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: How does one .ahk / .exe script pass parameters to another script?

20 Jul 2018, 14:20

https://autohotkey.com/docs/commands/Run.htm
WorkingDir
The working directory for the launched item. Do not enclose the name in double quotes even if it contains spaces. If omitted, the script's own working directory (A_WorkingDir) will be used.
As for the passing of parameters, I would have to look it up. Its in the documentation somewhere

Edit: https://autohotkey.com/docs/Scripts.htm#cmd
Passing Command Line Parameters to a Script
Scripts support command line parameters. The format is:

AutoHotkey.exe [Switches] [Script Filename] [Script Parameters]
S↓
And for compiled scripts, the format is:

CompiledScript.exe [Switches] [Script Parameters]
An example I use in my code to send variables to another script

Code: Select all

Run %A_AHKPath% "JsonTest.ahk" %Lat% %Long% 1294 100
TXShooter
Posts: 165
Joined: 13 Dec 2017, 09:27

Re: How does one .ahk / .exe script pass parameters to another script?

20 Jul 2018, 14:33

MannyKSoSo wrote:An example I use in my code to send variables to another script

Code: Select all

Run %A_AHKPath% "JsonTest.ahk" %Lat% %Long% 1294 100
That worked, but still had to use quotes.

Run, Test.ahk %tFilename% "c:\test - file number 1.dat"

tFilename := %0%

Will this also work when compiled? I guess I will have to compile and find out. lol
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: How does one .ahk / .exe script pass parameters to another script?

20 Jul 2018, 14:37

As far as I am aware compiling a script (as long as that script you are trying to run knows what to do with the command you are sending) should work the same.
TXShooter
Posts: 165
Joined: 13 Dec 2017, 09:27

Re: How does one .ahk / .exe script pass parameters to another script?

20 Jul 2018, 18:17

Flipeador wrote:See A_Args. Use tFilename := A_Args[1].
I tried that but all that came up was a blank. I even tried A_Args[0], A_Args[2], and A_Args[3] just to make sure. No joy.
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: How does one .ahk / .exe script pass parameters to another script?

20 Jul 2018, 18:31

Well, A_Args needs AHK v1.1.27+, but just in case there is the still working legacy method:
Legacy: The command line parameters are also stored in the variables %1%, %2%, and so on, as in versions prior to [v1.1.27]. In addition, %0% contains the number of parameters passed (0 if none).
See https://autohotkey.com/docs/Scripts.htm#cmd
TXShooter
Posts: 165
Joined: 13 Dec 2017, 09:27

Re: How does one .ahk / .exe script pass parameters to another script?

20 Jul 2018, 19:06

gregster wrote:Well, A_Args needs AHK v1.1.27+, but just in case there is the still working legacy method:
Legacy: The command line parameters are also stored in the variables %1%, %2%, and so on, as in versions prior to [v1.1.27]. In addition, %0% contains the number of parameters passed (0 if none).
See https://autohotkey.com/docs/Scripts.htm#cmd
In Windows 10, do I have to reboot for the update to take effect? (I wouldn't think that to be necessary.) Because I just upgraded from 1.1.26.01 to 1.1.29.1 and %0% is still working while A_Args[1] is not.

Code: Select all

ProcessCommandLine()
{
	global
	rFilename := %0%
	aFilename := A_Args[1]
	MsgBox % "tFlename is: `n" rFilename "`n`naFilename is: `n"
}
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: How does one .ahk / .exe script pass parameters to another script?

20 Jul 2018, 19:13

No, rebooting is not necessary, but something seems wrong...
Shouldn't %1% the corresponding command line parameter to A_Args[1]? Afaik, %1% = A_Args[1]
%0% contains the number of parameters passed, so it should be the same like A_Args.Length()
TXShooter
Posts: 165
Joined: 13 Dec 2017, 09:27

Re: How does one .ahk / .exe script pass parameters to another script?

20 Jul 2018, 19:26

gregster wrote:No, rebooting is not necessary, but something seems wrong...
Shouldn't %1% the corresponding command line parameter to A_Args[1]? Afaik, %1% = A_Args[1]
%0% contains the number of parameters passed, so it should be the same like A_Args.Length()
Well, something just changed in between tests, and now things are acting like how you described them to be. I didn't even reboot either.

I have asked this before, if there's some kind of hidden working directory that needs to be flushed out from time to time... odd things like this keep happening with subtle minor changes. (Could just be my machine too, which would also strike me as odd.)
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: How does one .ahk / .exe script pass parameters to another script?

20 Jul 2018, 19:27

I think rFilename := %0% should be rFilename = %1%.
And.. MsgBox % "rFilename is: " . rFilename . "`naFilename is: " . aFilename.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AHK_user and 197 guests