在Applescript中将文本添加到字符串

4

在AppleScript中,连接字符串似乎不能轻松地将文本附加到字符串变量上。我有以下一行代码,在大多数其他语言中都可以工作:

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

据我所知,这在AppleScript中似乎行不通,因为显然你不能在同一行中使用recp变量。是否有更简单的方法在不必在循环中使用两个变量的情况下将文本添加到变量末尾?
谢谢!
2个回答

3

只要在使用之前将recp设置为某个值,比如"",你所发布的代码就能正常工作。但是,这样会在你的字符串中得到一个,作为第一个字符,这可能不是你想要的。

相反,你可以这样做:

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

是的,它看起来很丑,但是它更高效,你只会在地址之间得到“,”。

啊,是的,谢谢!我没有意识到“set”实际上就是变量声明。感谢您的帮助。 - mjdth
如果你不知道set是什么,那么我建议在继续进行项目之前花时间阅读Applescript语言指南。Applescript与大多数其他语言不同。 - Philip Regan
这是一个非常简单的AppleScript项目,现在已经可以工作了。不过,我将来会仔细阅读语言指南以备将来的项目。谢谢。 - mjdth

0

更短、更简洁的方式:

# 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接