将文本附加到 Applescript 中的字符串

发布于 2024-11-14 22:29:24 字数 318 浏览 1 评论 0原文

AppleScript 中的串联似乎不允许您轻松地将文本附加到字符串变量。我有以下行可以在大多数其他语言中使用:

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

据我所知,这似乎在 AppleScript 中不起作用,因为您显然不能在同一行中使用 recp 变量。有没有更简单的方法可以将文本添加到变量的末尾,而不必在循环中使用两个变量?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲鸠 2024-11-21 22:29:24

只要先将 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

是的,它很难看,但它更有效,并且你只会在地址之间得到“,”。

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.

和影子一齐双人舞 2024-11-21 22:29:24

更短、更干净的方式:

# 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文