mjdth
mjdth

Reputation: 6536

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!

Upvotes: 4

Views: 8786

Answers (2)

TunaFFish
TunaFFish

Reputation: 11302

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

Upvotes: 0

Nicholas Riley
Nicholas Riley

Reputation: 44321

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.

Upvotes: 3

Related Questions