Posted Fri, 23 Jun 2023 09:41:50 GMT by Law, Norman
Hello there,

I am trying to get a 2612A to print out the name of all the user scripts.  Then I want to check "autorun" status of each file.  I saw the following from user manual:

for name in script.user.catalog() do
print(name)
end

This seems to work fine, but I get an error "-286 TSP Runtime error at line3: attempt to index local 'name'(a string value),0,1" if I do the following:

for name in script.user.catalog() do
print(name)
print(name.autorun)
end

Please help and advise.  Thank you.
Norman
 
Posted Fri, 23 Jun 2023 16:30:41 GMT by Odhner, Bradley

The issue you're having is that the iterator of script.user.catalog() is a string, not the actual scriptVar. You can confirm this by running

for name in script.user.catalog() do
   print(name, type(name))
end

which will report each name as having the type 'string'.

So, we need to interpret the string 'name' as a variable name (i.e. ScriptVar). This is fairly simple to do but is not in the standard TSP documentation. It is part of the standard Lua documentation though.

for name in script.user.catalog() do
 print(name, _G[name].autorun)
end

_G is a table that holds the global variables of the instrument. _G[name] will return the object with variable name 'name'. So _G["ScriptVar"].autorun is equivalent to ScriptVar.autorun. You can read more here: https://www.lua.org/pil/14.1.html

You must be signed in to post in this forum.