Reputation: 41
I'm fairly new to autohotkey and ran into a problem recently when trying to pass variables as parameters for the "run" command in autohotkey. Can anyone show me what I'm missing or is this a bug?
Gui, Setup:Add, Edit, w100 vProgram
Gui, Setup:Add, Button, Default w100 gSubmit, OK
Gui, Setup:Show,, Setup
WinWaitClose, Setup
ExitApp
Submit:
Gui, Setup:Submit
RunStuff()
RunStuff() {
Run, %Program%
}
I've even tried changing the line to this:
RunStuff() {
run, Program
}
I would really appreciate the help, thanks in advance.
Upvotes: 2
Views: 4393
Reputation: 1
RunStuff() {
Run % start "" "C:\blabla\prog.exe" -windowstyle minimized
}
Upvotes: 0
Reputation: 6489
First things first, while
WinWaitClose, Setup
ExitApp
Is maybe smart thinking, you're supposed to do this with the GuiClose
event by just defining a function (or a label) with the name GuiClose
.
GuiClose()
{
ExitApp
}
Also, giving the gui a name is redundant, although if you're planning on adding more guis, fair enough.
And to run cmd commands, you'd start off cmd with the /c
(docs) switch.
Your example netsh wlan connect ssid=%networkname% name=%networkname%
would be done like this:
networkname := "name"
Run, %A_ComSpec% /c netsh wlan connect ssid=%networkname% name=%networkname%
Run, % A_ComSpec " /c netsh wlan connect ssid=" networkname " name=" networkname
The first line is in legacy syntax, I wouldn't really recommend it.
The second line is in expression syntax.
The built in variable A_ComSpec
(docs) contains the path to cmd.exe.
Example program based on your code to open a file in notepad:
Gui, Setup:Add, Edit, w300 vFilePath, % "C:\Users\User\Desktop\this is a text file.txt"
Gui, Setup:Add, Button, Default w100 gSubmit, OK
Gui, Setup:Show, , Setup
return
Submit:
Gui, Setup:Submit
Run, % "notepad.exe """ FilePath """"
return
GuiClose()
{
ExitApp
}
Note how the file path includes spaces, so the argument needs to wrapped in quotes.
""""
may seem weird, but a quote is escaped with another quote in AHK. So outer quotes specify that you're writing the string, and inner ""
is just one quote escaped. So this produces "
.
Legacy syntax vs modern expression syntax can be a bit confusing when you're learning AHK. You'll see a lot of legacy syntax when you look up stuff. This is mainly because AHK was much more popular years and years ago (when legacy syntax was the thing to use).
To get started off on the legacy vs modern expression differences, here's a pretty good documentation page:
https://www.autohotkey.com/docs/Language.htm
EDIT:
Answer to the new problem that as added in via an edit to the OP.
RunStuff() {
Run, %Program%
}
The variable Program
is not defined in the function's scope.
Lets consider this example code
var1 := 1
var2 := 2
global var3 := 3
function()
return
function()
{
global var2
MsgBox, % "var1: " var1 "`nvar2: " var2 "`nvar3: " var3 "`n"
}
var1
is not defined in the function's scope, so nothing will be printed in the message box.
var2
will be used from outside the function's scope because of the line global var2
.
var3
would also be used from outside of the function scope because var3 was defined as super global. Super global isn't really recommended, because anything anywhere will use that variable then. Quite easy to run into problems on larger scripts, but it's convenient for smaller scripts where you know what you'll be doing and don't have any external libraries for example.
You can read the related documentation from here:
https://www.autohotkey.com/docs/Functions.htm#Locals
Upvotes: 2
Reputation: 405
It seems that you think your problem is with the gui output.
I think you might want to check things in a different way:
You could create a script to analyze the your run inputs.
For example:
Script #1, let's call it "1.ahk"
Run, 2.ahk param1 param 2 "param 3"
Script #2, let's call it "2.ahk"
txt := "Params as seen by ahk:`n"
for i, param in A_Args
txt .= i " = " param "`n"
MsgBox % txt
By running script "1.ahk" you can see the message that "2.ahk" creates:
Params as seen by ahk:
1 = param1
2 = param
3 = 2
4 = param 3
Which I think it's quite telling about how it understands spaces, and how quotes prevent splitting arguments.
Now if you run your gui outputs against "2.ahk" you might see something different to what you saw before.
Upvotes: 2