I would think this is a simple question, but it doesn't work.
I would like to use a line of this kind:
Run, %Viewer% "%A_ScriptDir%/MyFile.htm"
Viewer is the path of a specific program which should be used if it exists, or Viewer := "" if this program is not available. So if the program is available, MyFile.htm should be displayed with this program, and if the program is not available, MyFile.html should be displayed with the standard browser.
But the line only works if the program is available. If it is not available, the Run command seems to have a problem with the empty string. Why?
Thank you in advance!
How to run a file optionally with a program if exists
-
- Posts: 493
- Joined: 24 Aug 2016, 03:34
Re: How to run a file optionally with a program if exists
Hi newbieforever,
Spaces and tabs are meaningful:
Spaces and tabs are meaningful:
Code: Select all
Viewer := ""
run % LTrim(Viewer . A_Space . """" . A_ScriptDir . "/MyFile.htm""") ; LTrim trims spaces and tabs - if any - from the beginning of the string.
; run % LTrim(Viewer . A_Space . Chr(34) . A_ScriptDir . "/MyFile.htm" . Chr(34)) ; alternative
-
- Posts: 493
- Joined: 24 Aug 2016, 03:34
Re: How to run a file optionally with a program if exists
Hm ... I see that this is the problem. But why then a Run command like thisA_AhkUser wrote:Spaces [...] are meaningful [...]
Run, __________ "%A_ScriptDir%/MyFile.htm"
(here _ stands for a space) works?
Re: How to run a file optionally with a program if exists
Not sure but I guess your script is parsed the moment it is loaded and at this time spaces are optional after each comma (the [edit]first[edit] comma itself too) to make ahk syntax more flexible so that the three lines below are correct and ahk can adjust all that:
Which is different from your space which is part of your parameter:
I mean:
vs
[EDIT] compare:
Code: Select all
MsgBox,test
MsgBox, test
MsgBox test
I mean:
Code: Select all
Run, "%A_ScriptDir%/MyFile.html"
Code: Select all
Run, " %A_ScriptDir%/MyFile.html" ; will not work
[EDIT] compare:
Code: Select all
MsgBox, test
MsgBox % " test"
Last edited by A_AhkUser on 24 Sep 2017, 10:23, edited 1 time in total.
-
- Posts: 493
- Joined: 24 Aug 2016, 03:34
Re: How to run a file optionally with a program if exists
@A_AhkUser:
Of course, this is plausible. Thank you very much!
Of course, this is plausible. Thank you very much!
-
- Posts: 493
- Joined: 24 Aug 2016, 03:34
Re: How to run a file optionally with a program if exists
Back again!
On the other hand: Why we are speacking of spaces here? Viewer := "" is an empty string, isn't it?
Why should an empty-string variable be replaced by a space when the script is parsed?
On the other hand: Why we are speacking of spaces here? Viewer := "" is an empty string, isn't it?
Code: Select all
Viewer := ""
String := "One" . Viewer . "Two"
MsgBox %String% ; Shows 'OneTwo', not 'One Two'!
Re: How to run a file optionally with a program if exists
Beeing so interested in how it works I doubt you'll stay newbie... forever Thanks for asking; I found that AutoTrim can be used to determine whether Var1 = %Var2% statements omit spaces and tabs from the beginning and end of Var2 (this does not affect expression (:=;%) assignments).
[EDIT]
It appears that run command always fails no matter the autotrim setting... Besides AutoTrim defaults to On. [edit]In fact it concerns only Var1 = %Var2% statements.[edit]
Code: Select all
AutoTrim, Off
var = %Viewer% "%A_ScriptDir%/MyFile.htm"
MsgBox % StrReplace(var, A_Space, "@")
var := Viewer . A_Space . """" . A_ScriptDir . "/MyFile.htm"""
MsgBox % StrReplace(var, A_Space, "@")
AutoTrim, On
var = %Viewer% "%A_ScriptDir%/MyFile.htm"
MsgBox % StrReplace(var, A_Space, "@")
[EDIT]
It appears that run command always fails no matter the autotrim setting... Besides AutoTrim defaults to On. [edit]In fact it concerns only Var1 = %Var2% statements.[edit]
Code: Select all
AutoTrim, Off
AutoTrim, On
Run, %Viewer% "%A_ScriptDir%/MyFile.html" ; always fails no matter the AutoTrim setting
; solution
; AutoTrim, On ; <-- this is the default
;...
var = %Viewer% "%A_ScriptDir%/MyFile.html"
Run, %var%
Last edited by A_AhkUser on 24 Sep 2017, 10:39, edited 3 times in total.
-
- Posts: 493
- Joined: 24 Aug 2016, 03:34
Re: How to run a file optionally with a program if exists
@A_AhkUser:
Thank you, A_AhkUser, for your comments and suggestions!
I think in my case the most simple solution for this problem will be:
But you are right, I am very interested in how it works! So I would like to focus the further discussion here to the question:
Why should an empty-string variable (Viewer) be replaced by a space when the following command line is parsed by AHK?
Run, %Viewer% "%A_ScriptDir%/MyFile.htm"
Thank you, A_AhkUser, for your comments and suggestions!
I think in my case the most simple solution for this problem will be:
Code: Select all
If Viewer
Run, %Viewer% "%A_ScriptDir%/MyFile.htm"
Else
Run, "%A_ScriptDir%/MyFile.htm"
Why should an empty-string variable (Viewer) be replaced by a space when the following command line is parsed by AHK?
Run, %Viewer% "%A_ScriptDir%/MyFile.htm"
Re: How to run a file optionally with a program if exists
It is not replaced by a space the space in question, I think, it is the one between Viewer and the path of your file that remains.
The var must be set down if it can generate any trailing space:
The var must be set down if it can generate any trailing space:
Code: Select all
var = %Viewer% "%A_ScriptDir%/MyFile.html" ; this will omit spaces and tabs from the beginning and end of var (AutoTrim defaults to On)
Run, %var%
-
- Posts: 493
- Joined: 24 Aug 2016, 03:34
Re: How to run a file optionally with a program if exists
... That is way I will stay newbie forever! It is one of the aspects I probably newer will understand. I am simply unable to catch on why these two alternatives are not the same ...
Code: Select all
var = %Viewer% "%A_ScriptDir%/MyFile.html"
Run, %var%
Code: Select all
Run, %Viewer% "%A_ScriptDir%/MyFile.html"
Re: How to run a file optionally with a program if exists
That's how, IMO, AHK will read this line:
First it parses your code and especially it will remove any trailing spaces and tabs (if AutoTrim is set to On which is the default setting) and since it does not know the value of Viewer (especially: if it is empty) because it only parses (i.e. judges if it is correct from syntax point of view) it judge that Viewer is a part of your argument that now starts with the first percent sign - so that a space will remains once the line executed.
Btw, you can use the ternary operator:
Code: Select all
Run, %Viewer% "%A_ScriptDir%/MyFile.html"
Btw, you can use the ternary operator:
Code: Select all
run % ((Viewer <> "") ? Viewer . A_Space : "") . """" . A_ScriptDir . "/MyFile.html"""