Insert variables

I have variable names in list:

set my_variables to {"main_title", "main_description"} --this has 20 variables

I need to set those variables to “xxxx”. Normal code would look like this:

set main_title to "xxxx"
set main_description to "xxxx"

I tried this and it fails:

set (item 1 in my_variables) to "xxxx"
set (item 2 in my_variables) to "xxxx"

I also tried this:

set (run script (item 1 in my_variables)) to "xxxx"
set (run script (item 2 in my_variables)) to "xxxx"

Hi cirno,
what you need is a record:

set my_variables to {main_title:"aa", main_description:"bb"}
log main_description of my_variables

set main_description of my_variables to "XXXX"
log main_description of my_variables

Greets from
TMA

Thanks.

I think i can’t use your method. I try to decrease amount of rows in some of my scripts. Here is example:

set_variable_to_x({{"main_title", "title here"}, {"main_description", "some data"}}) --this list has 20 variables

on set_variable_to_x(what)
	repeat with counter from 1 to (count items in what)
		set (item 1 in (item counter in what)) to (item 2 in (item counter in what))
	end repeat
end set_variable_to_x

Result should be:

main_title variable value is “title here”
main_description variable value is “some data”

If list has 20 variables, then after this i have 20 separate variables set to correct values.

Hows this?

set my_variables to {{"main_title", "main_description"}, {"main_title", "main_description"}, {"main_title", "main_description"}}

set (item 1 of item 1 of my_variables) to "xxxx"
set (item 2 of item 1 of my_variables) to "xxxx"

set (item 1 of item 2 of my_variables) to "yyyy"
set (item 2 of item 2 of my_variables) to "yyyy"

my_variables

It doesn’t need to be a record nor does it need to be a listed list. You can solve this by setting the contents of a var to a reference. Then when you set a reference to a variable you need set the contents of the reference instead of changing the var. Something like this will work.


set aList to {"aaaa", "bbbb"}
set mainTitle to a reference to item 2 of aList

set contents of mainTitle to "XXXXX"

return aList --result:{"aaaa", "XXXXX"}

The reason I don’t use set mainTitle to “XXXXX” is simply because it would delete the reference mainTitle is holding and replace it with “XXXXX”. When I say set contents of mainTitle to “XXXXX” i’m changing the content of item 2 of aList (the reference) and not the content of mainTitle.