evaluating a string to the variable it represents?

Hi,

I want to evaluate a string that represents a variable name, simple example:

set varName to {1,2,3,4}

is it possible in Applescript to use the string “varName” to get access to the actual variable?

Assuming an evaluate function existed, I want the following to occur
evaluate(“varName”)
→ {1,2,3,4}

count evaluate(“varName”)
→ 4

is there also some way to get the type of the variable based on a string containing the variable name?
type evaluate(“varName”)
→ list of integer (or maybe just ‘list’)

Why? In Smile I can get a list of variable names with:
set varNames to name of every variable of context
The I want to go through it to findout how big and what type each variable is.

Thanks in advance,
Brad

Hi Brad,

In Smile, can you:

set varNames to every variable of context

In which case you could find the info you want.

set varName to {1, 2, 3, 4}
{varName, count varName, class of varName}

Although, I’m lost as to what you are hoping to obtain.

Best wishes

John M

John,

You missed the key point, I want to use a string that contains the variable name to access the variable.

Why? In Matlab (the language I use daily) a simple ‘who’ (or ‘whos’) will list all the variables (with type/size etc) currently defined in the workspace. I want to emulate this in Smile. Maybe this functionality already exists in Smile but I can’t find it.

I can get a text list of the variables in Smile’s context (name of every variable of context). I just need to figure out how to use those text names to get information about the variables themselves.

“class of varnames”, was helpful, now I know how to get what I thought was the type.

Regards,
Brad

Perhaps you mean:

set varName to {1, 2, 3, 4}
set TheVar to {"empty"}
set myString to "this text contains VarName"

if myString contains "varName" then
	set TheVar to varName
end if
return TheVar

But I think you want to coerce a string to a variable name. If it’s possibe to do this in Applescript, it’s not going to be an easy thing to do.

Maybe a different scripting language would be a better tool for the job.

John M

John,

Yes, exactly:

Unfortunately, I know how to do this in other languages (e.g. in Matlab use the eval(‘stringToBeEvaluated’) function) but I want to be able to do it with variables in Smile’s workspace and hence need to use Applescript.

So now that we’ve landed on the correct terminology for my problem, any ideas? Anyone?

Thanks for your interest,
Brad

Thanks to Emmanuel at Satimage I now know how to use a string containing a variable name to access the variable in Smile.

set testVar to {1, 2, 3, 4}
do script “get " & “testVar” & " of context”
→ {1,2,3,4}

do script is part of Smile’s dictionary.

Brad

And this is the vanilla version:

set varName to {1, 2, 3, 4}

set theThing to "varName"

run script "on run {x}
	return x's " & theThing & "
end" with parameters {me}