4

It seems that the concatenation in AppleScript doesn't let you easily append text to a string variable. I have the following line that would work in most other languages:

repeat with thisRecpt in recipients of eachMessage
    set recp to recp & "," & address of thisRecpt
end repeat

From what I can tell, this doesn't seem to work in AppleScript, as you cannot use the recp variable within the same line apparently. Is there any easier way of adding text to the end of a variable without having to use two variables within the loop?

Thanks!

2 Answers 2

3

The code you posted works fine as long as recp is set to something first, say "". However, you'll then get a , as the first character in your string, which is probably not what you want.

Instead, you can do this:

set _delimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set recp to eachMessage's recipient's address as string
set AppleScript's text item delimiters to _delimiters

Yes, it's ugly, but it's more efficient and you'll only get "," between addresses.

Sign up to request clarification or add additional context in comments.

4 Comments

Ah yes, thank you! I didn't realize that "set" was basically the declaration of the variable. Thanks for the help.
@mjdth: If you don't know what set is, then I suggest spending time with the Applescript language guide before going too much further with your project. Applescript is not like most other languages.
It's a very, very minimal project in AppleScript, and it's working now. I will be sure to check out the language guide for future projects, though. Thank you.
set recp to recp & address of thisRecpt & "," should work, shouldn't it? An unnecessary comma at the end (I use linefeed as separator)
0

Shorter and cleaner way to:

# cut off last character in a string
set myString to "foo,bar,baz,"
set myString to text 1 thru -2 of myString
log myString

# cut off first character in a string
set theString to ",foo,bar,baz"
set theString to text 2 thru -1 of theString
log theString

1 Comment

What does this have to do with the question!?!?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.