0

I’m stuck trying to figure out how to expand any variables in an AutoHotkey string. In my specific case, I read a line from a file and run it. The line may contain one or more variable references which need to be expanded before being passed to Run.

Here’s a couple of tests (that don’t work):

Foo:="%A_MyDocuments%\blah.txt"
Bar=%Foo%
MsgBox %Bar%

a=1
b:="%a%+1=2"
MsgBox % b

I spent the past two hours scouring the docs and the Internet and found nothing that worked. There were a couple of close-calls, but nothing that matches this scenario.

Synetech
  • 9,643
  • 9
  • 64
  • 96
  • 1
    The string in your first example doesn't contain any variable names. I don't understand what the second example is about. Where is the Run command, how is it used? – 2501 Apr 28 '16 at 07:01
  • 1
    what is the problem, what is it that does not work? what errors do you get? etc.... – phil294 Apr 28 '16 at 12:04
  • I think you are looking for the functionality of `eval` in AHK https://en.wikipedia.org/wiki/Eval. There are AutoHotkey implementations of that to be found on the internet. Other than that, I cannot think of an easy way to do it – phil294 Apr 28 '16 at 12:09
  • @2501, `Foo` contains a reference to `A_MyDocuments` which is a *variable* that points to, well, I’ll generously assume you can figure the rest out. And in the second example, `b` contains a reference to `a` which is a *variable* containing the value `1`. These are stripped down examples that show what I am trying to get working. The `Run` command is irrelevant; it is standard practice to use output commands like `MsgBox` for examples. – Synetech Apr 29 '16 at 00:24
  • @Blauhirn, the problem is that AHK complains about an invalid variable name because in the first example, it is trying to use `%A_MyDocuments%\blah.txt` as the name of a variable and in the second one, it is trying to use `%a%+1=2` as the name of a variable. In both cases, it is trying to use the full string as the name of a variable which is not what I want; I want only the variable reference to be expanded. So the first one should print something like `c:\users\foo\documents\blah.txt` and the second should print `1+1=2`. – Synetech Apr 29 '16 at 00:27
  • @Blauhirn, Thanks; I’ll look into that. It seems odd though that there would be no way simpler way, especially considering that AHK has several different ways to assign, dereference, and expand variables. – Synetech Apr 29 '16 at 00:28

3 Answers3

1

It turns out there is a built-in function that can do this. The Transform command can expand variables easily:

Foo:="%A_MyDocuments%\blah.txt"
Bar=%Foo%
Transform, Bar, Deref, %Bar%
MsgBox %Bar% ; Displays something like C:\Users\Foobar\Documents\blah.txt

a=1
b:="%a%+1=2"
Transform, b, Deref, %b%
MsgBox % b ; Displays 1+1=2

Thank you guest3456 for the help.

Synetech
  • 9,643
  • 9
  • 64
  • 96
0

You have some ‘=’ and ‘:=’ confused.

Saying “B:=%A%+1=2" is a string and you are trying to ‘set’ the answer to 2 in the string instead of calculating?

Should be something like:

A = 1
B := A + 1
Msgbox, % B

Or 
A = 1
B = %A% + 1
Msgbox, % B

Or 
Foo = %A_MyDocuments% . “\blah.txt”
Bar := Foo
Msxbox, %Bar%

Variable assignment https://autohotkey.com/board/topic/97097-faq-variables-dynamic-variables-literal-strings-and-stuff-like-that/

Eric King
  • 103
  • 3
  • 18
  • I am familiar with expression and non-expression syntax. Like I said in the question, I am reading the strings from a file, so your examples will not work. – Synetech Apr 29 '16 at 00:29
  • You do not specify what you are reading from a file or how you are reading it, which could impact my original response. Only that you are stuck and trying to ‘expand variables’. It does not makes sense to have %a%+1=2 output as a string showing “1+1=2”, because as a response it would be invalid unless ‘A’ always equaled ‘1’– but I imagine you don’t care about that. The answer of: “Foo = %A_MyDocuments% . “\blah.txt”” should function and a line of ‘Msgbox % Foo’ should display the ‘c:\users\foo\documents\blah.txt’. I was suggesting that maybe your approach was a bit off, and not code. – Eric King Apr 29 '16 at 15:26
  • I’m not sure where the confusion is, very first couple of lines said `I read a line from a file… the line may contain variables which need to be expanded`. It seems like a straight forward question. I used `:=` in the example to make sure that the variables actually contain the un-expanded variables. Reading from a file would have the same effect. – Synetech May 03 '16 at 14:15
0

There is no native way of doing this. I see two options. Use AutoHotkey.dll to evaluate your expression or run another instance of AutoHotkey.exe to the job for you.

For example:

Foo:="%A_MyDocuments%\blah.txt"
Bar := Eval(Foo)
MsgBox %Bar%

Eval(exp) {
    Static tempScript := A_ScriptDir "\tmp_eval.ahk"
    Static tempOutput := A_ScriptDir "\tmp_eval_out.txt"
    FileDelete, %tempScript%
    FileDelete, %tempOutput%
    FileAppend, % "FileAppend, " exp ", " tempOutput , %tempScript%
    RunWait, %A_AhkPath% "%tempScript%"
    FileRead, output, %tempOutput%
    FileDelete, %tempScript%
    FileDelete, %tempOutput%
    Return output
}
Forivin
  • 14,780
  • 27
  • 106
  • 199
  • this will not work if `exp` contains a non-standard variable, like OPs `exp:="%a%"` – phil294 Apr 30 '16 at 20:25
  • 1
    True. But since the "string" comes from a file, I'd assume that there is a certain set of variables that can be used in this string and these variables could simply be passed to the temporary ahk.exe instance. – Forivin Apr 30 '16 at 20:51
  • It might also be possible to parse the output of ListVars and all of them over. – Forivin Apr 30 '16 at 20:53
  • Wow, it seems so strange that there is no way to do this. I don’t think it is an unusual scenario. Maybe I should add it to the wish-list. In the meantime, I will try out your solution (or maybe just end up hard-coding the paths). `:-/` – Synetech May 03 '16 at 14:18