How to refer to a variable using a string?

Is there a way to refer to a variable using a string?

I tried to do this by coercing a string into a variable, but AppleScript does not allow it. Is there another way?

The below is what I’m trying to do.

set variable_choice to "b" -- Need this line to be before all other lines

set a to 12
set b to 45
set c to 78

set output to variable_choice as variable -- Attempted coercion

log output -- Need this to print "45", the value of variable b

Not really! Try a different approach.

Hi @jasonscript. Welcome to MacScripter.

While it’s not sensibly possible to refer to a variable using a string, it is possible to refer to it using a ‘reference’ — as long as the variable’s not one that’s only used in a handler and isn’t explicitly declared ‘local’.

set variable_choice to a reference to b --> b of «script»

set a to 12
set b to 45
set c to 78

set output to variable_choice's contents

log output --> (*45*)

But depending on what you’re ultimately trying to do, it may be simpler to use a different approach as Otto suggests.

Rather something like

set variable_choice to (display dialog "Pick any of a, b, c" default answer "")'s text returned
set output to `value of variable named variable_choice`

They’re trying to do something that’s possible in (for example) LISP. It’s discouraged in other languages though, e.g. JavaScript and Perl, because it basically involves executing arbitrary strings as code.

Not sure if
my valueForKey:”b” would work?
It’s how you can in objective:C

Or try placing it in a script object whose parent is NSObject

Also declare your property

property b : 45
etc

Another possibility which actually works:

-- set variable_choice to "b" -- Need this line to be before all other lines
set variable_choice to (choose from list {"a", "b", "c"} with prompt "Pick a variable:")
if (variable_choice is false) then error number -128
set variable_choice to beginning of variable_choice

set a to 12
set b to 45
set c to 78

set output to (run script ("on run {a, b, c}
	return " & variable_choice & "
end") with parameters {a, b, c})

log output

Interesting (does set variable_choice to beginning of variable_choice make a difference at all?)

Even more interesting if the user is instead asked to input some text. Then whatever they type will be executed in the run script code – for example
do shell script \"cat /etc/passwd\"
I know that sounds like paranoia. But it is one of the ways to gain access to machines. And one of the reasons why these kinds of tricks are discouraged in other languages. Just saying.

Not really in this case. But since the OP’s a newcomer of unknown knowledge and ability, I decided to post code showing the item being extracted from the list returned by ‘choose from list’ (“Aha! So ‘choose from list’ either returns ‘false’ or a list of the chosen value(s).”) rather than just let the list be silently coerced to text when it’s concatenated into the ‘run script’ text.

1 Like

Thank you @Nigel_Garvey ! I can apply this syntax.

Yes @Fredrik71 that’s the general idea, though I’m trying to avoid the lines of “if” statements, which will get much longer.

@Nigel_Garvey I will investigate this method further.

@technomorph Will need flexibility to update variable values later in script, that’s why am currently using set variable.

The question of how to turn a string into a variable is not the first time on the forums.

Usually, in such cases, there is a standard AppleScript solution, without the need for such clumsy conversion. It is usually less bulky and more efficient in speed as well.

You’d better create another topic, show your real script and explain what it should do and what doesn’t work. There are several ways to avoid multiple IF statements.

@chrillek Yes that’s correct, to use a variable with name indicated by a string.

With a defined property works just the same
It’s globabky available to the script or to other objects that access the script
or a property in a script object you can still set it.

set b to 67
set scriptObject’s b to 67
set aObject’s valueForKey:”b” to 67

Use copy (deep object copy).

set variable_choice to "b"

set a to 12
set b to 45
set c to 78

copy b to output

log output -- Need this to print "45", the value of variable b

@technomorph Yes of course, you’re right.

@Piyomaru Unfortunately a direct copy does not include the variability of the “variable_choice”

I think Nigel_Garvey’s run script approach is better or multiple if…clause or multiple cast.

set output to b as {string, real , number, anything}–“anything” have to be “any” on Script Debugger

This is not variable but reasonable.

Just store the values with keys in a record or NSMutableDictionary

With the NSMutableDictionary it’s now a NSObject subclass so valueForKey:
Will now work.
And setValue:forKey:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions


-- classes, constants, and enums used
property NSMutableDictionary : a reference to current application's NSMutableDictionary

set aRecord to {a:12, b:45, c:78}
set aMutableDict to NSMutableDictionary's dictionaryWithDictionary:aRecord

set aChoice to (choose from list {"a", "b", "c"} with prompt "Pick a variable:")
if (aChoice is false) then error number -128
set aChoice to beginning of aChoice

set aChoiceValue to aMutableDict's valueForKey:aChoice