How to pass an "empty parameter"?

Get help with using AutoHotkey and its commands and hotkeys
newbieforever
Posts: 400
Joined: 24 Aug 2016, 03:34

How to pass an "empty parameter"?

30 Apr 2019, 14:12

Hi!

The script MyScript.ahk contains a function like this:
MyFunction(file, path:="", icon:="")
which can be called i.e. like this:
MyFunction("Text.txt", , "Icon.ico")
i.e. the second parameter can be left empty.

The script itself accepts parameters which will be used as parameters for MyFunction. It should be run from another script like this:
P1 := "Text.txt" , P2 := "C:\Test" , P3 := "Icon.ico"
Run, AHK.exe MyScript.ahk %P1% %P2% %P3%


But how to run MyScript with an empty second parameter P2?
Last edited by newbieforever on 30 Apr 2019, 14:46, edited 1 time in total.
Ridwan
Posts: 141
Joined: 17 Oct 2015, 21:06
Location: Indonesia

Re: How to pass an "empty parameter"?

30 Apr 2019, 14:39

How about passing special character for parameter 2, like 0 (zero) and use If to handle it?

Code: Select all

p1 = %1%
p2 = %2%
p3 = %3%

MyFunction(p1, p2, p3)
Return

MyFunction(file, path:="", icon:="")
{
    If (path = 0)
    {
        MsgBox, Blank path
        path :=
    }

    Else MsgBox, Path is exist
}
newbieforever
Posts: 400
Joined: 24 Aug 2016, 03:34

Re: How to pass an "empty parameter"?

30 Apr 2019, 14:45

Thank you, Ridwan! I was afraid that would be the only solution. To pass an "empty parameter" is impossible, right?
Ridwan
Posts: 141
Joined: 17 Oct 2015, 21:06
Location: Indonesia

Re: How to pass an "empty parameter"?

30 Apr 2019, 15:01

newbieforever wrote:
30 Apr 2019, 14:45
Thank you, Ridwan! I was afraid that would be the only solution. To pass an "empty parameter" is impossible, right?
I think it's possible. I see it from some command line program like ImageMagick or FFmpeg which accept optional parameter.
But some trick will needed. :)
just me
Posts: 6464
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to pass an "empty parameter"?

01 May 2019, 04:22

As an example:

Runner.ahk:

Code: Select all

#NoEnv
File := "File"
Path := "Path"
Icon := "Icon"
Script := A_ScriptDir . "\Script.ahk"

RunWait, %A_AhkPath% "%Script%" /File="%File%" /Icon="%icon%"

ExitApp
Script.ahk:

Code: Select all

#NoEnv
; Read script parameters
Vars := {File: "", Icon: "", Path: ""}
For Each, Param In A_Args {
   Part := StrSplit(LTrim(Param, "/"), "=", , 2)
   If Vars.HasKey(Part[1])
      Vars[Part[1]] := Part[2]
}
MsgBox, 0, Passed Variables, % "File: " . Vars["File"] . "`n"
                             . "Icon: " . Vars["Icon"] . "`n"
                             . "Path: " . Vars["Path"]
ExitApp

Store both scripts in the same folder and run Runner.ahk.

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], Rohwedder, woo and 156 guests