Search code examples
applescript

Append text to a string in Applescript


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!


Solution

  • 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.