How to run a file optionally with a program if exists

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

How to run a file optionally with a program if exists

24 Sep 2017, 07:47

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!
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: How to run a file optionally with a program if exists

24 Sep 2017, 08:21

Hi newbieforever,


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
my scripts
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to run a file optionally with a program if exists

24 Sep 2017, 08:30

A_AhkUser wrote:Spaces [...] are meaningful [...]
Hm ... I see that this is the problem. But why then a Run command like this
Run, __________ "%A_ScriptDir%/MyFile.htm"
(here _ stands for a space) works?
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: How to run a file optionally with a program if exists

24 Sep 2017, 08:44

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:

Code: Select all

MsgBox,test
MsgBox, test
MsgBox test
Which is different from your space which is part of your parameter:

I mean:

Code: Select all

Run,                      "%A_ScriptDir%/MyFile.html"
vs

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.
my scripts
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to run a file optionally with a program if exists

24 Sep 2017, 08:47

@A_AhkUser:

Of course, this is plausible. Thank you very much!
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to run a file optionally with a program if exists

24 Sep 2017, 09:13

Back again!

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'!
Why should an empty-string variable be replaced by a space when the script is parsed?
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: How to run a file optionally with a program if exists

24 Sep 2017, 10:09

Beeing so interested in how it works I doubt you'll stay newbie... forever :lol: 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).

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.
my scripts
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to run a file optionally with a program if exists

24 Sep 2017, 10:25

@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:

Code: Select all

If Viewer
   Run, %Viewer% "%A_ScriptDir%/MyFile.htm"
Else
   Run, "%A_ScriptDir%/MyFile.htm"
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"
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: How to run a file optionally with a program if exists

24 Sep 2017, 10:36

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:

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%
my scripts
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to run a file optionally with a program if exists

24 Sep 2017, 11:08

... That is way I will stay newbie forever! :crazy: 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"
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: How to run a file optionally with a program if exists

24 Sep 2017, 11:32

That's how, IMO, AHK will read this line:

Code: Select all

Run, %Viewer% "%A_ScriptDir%/MyFile.html"
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 <> "") ? Viewer . A_Space : "") . """" . A_ScriptDir . "/MyFile.html"""
my scripts

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, mikeyww and 182 guests