# changing a variable within a variable, How?

I am fairly new to AppleScript. Is there a way to set a variable up (varB) with a calculation which uses another variable (varA) in such a way that when varA is set to a different value, varB changes as well?

This is what I have tried:

–Set up the two variables–
set varA to 3
set varB to 4 + varA

–change varA–
set varA to 6

–This is the result I get–
verB = 7

–This is the result I am looking for–
varB = 10

Obviously, the way I am doing it, varB doesn’t incorporate the changes to varA, Surely there is a way to do this so that the change happens?

This seems to work for me…

``````set varA to 3
set varB to 4 + varA
varA + varB
--> 10
``````

Greggo

The actual problem I am working on is, of course, more complex than what I put out. I am trying to figure out a repeating sequence in which there is a variable that functions as a counter – i.e. set varA to varA + 1 on each repetition.

The idea is to create a mail merge for Eudora with a list of names extracted from another document. The counter would progress through the list. The counter would be part of a function which would do this, but do it inside the context of a variable.

FIRST EXAMPLE

set theNames to {“John”, “Mary”, “Freddie”, “Elizabeth”}
set oneName to item 2 of theNames
result “Mary”

SECOND EXAMPLE

set theNames to {“John”, “Mary”, “Freddie”, “Elizabeth”}
set listCount to 0

repeat
set listCount to listCount + 1
set oneName to item listCount of theNames
if listCount > (number of items in list) then exit repeat
end repeat

results of repetitions:
“John”
“Mary”
“Freddie”
“Elizabeth”

THIRD EXAMPLE

set theNames to {“John”, “Mary”, “Freddie”, “Elizabeth”}
set listCount to 0
set oneName to item listCount of theNames

repeat
set listCount to listCount + 1
set message listCount to oneName
if listCount > (number of items in list) then exit repeat
end repeat

results of repetitions:
“John”
“Mary”
“Freddie”
“Elizabeth”

FOURTH EXAMPLE
–Here is some of the actual script–

set theNames to {“John”, “Mary”, “Freddie”, “Elizabeth”}
set myMessage to “The text for the email message.”
set listCount to 0
set oneName to item listCount of theNames
set wholeMessage to "Dear " & oneName & return & return & myMessage

–When listCount increases by 1 on each repetition, I want the name (item in an aray) to change–
repeat
set listCount to listCount + 1
if listCount > 4 then exit repeat
set body to wholeMessage
end repeat

–The result of, say, the third repetition would be:

"Dear Freddie

The text for the email message."

Maybe this is close to what you need.

``````set theNames to {"John", "Mary", "Freddie", "Elizabeth"}
set myMessage to "The text for the email message."

repeat with name_ in theNames
tell application "Eudora"
set nm to make new message at end of mailbox 2 -- Out
tell nm
set body to "Dear " & name_ & return & return & myMessage
-- do other stuff to nm (new message)
end tell
end tell
end repeat
``````

I hope that I haven’t just assisted a spammer because I have special scripts for them.

– Rob