AppleScript有替换功能吗?

时间:2016-06-26 18:37:09

标签: applescript

在VBA中,使用不同的子字符串极其容易替换子字符串,例如objMsg.Body = Replace(objMsg.Body, "<Month\", Format(dtDate1, "mmmm"))"<Month>"替换为电子邮件正文中的当前月份。我之前已经看过类似这样的问题,但他们已经有几年了,并且往往会有疯狂的解决方法,几乎​​不值得我花时间。

2 个答案:

答案 0 :(得分:3)

不,但你可以使用这个(source):

on replace_chars(this_text, search_string, replacement_string)
 set AppleScript's text item delimiters to the search_string
 set the item_list to every text item of this_text
 set AppleScript's text item delimiters to the replacement_string
 set this_text to the item_list as string
 set AppleScript's text item delimiters to ""
 return this_text
end replace_chars

用法:

replace_chars(message_string, "string_to_be_replaced", "replacement_string")

(顺便说一下,你现在也可以使用JavaScript而不是AppleScript,搜索“用于OS X Automation的JavaScript”)

答案 1 :(得分:1)

查找/替换的一个非常简单的方法是调用shell脚本并将文本作为字符串传递给它:

set inputText to "My name is Fred."
set findText to "Fred"
set replaceText to "Sally"

set newText to do shell script "sed 's|" & quoted form of findText & "|" & quoted form of replaceText & "|g' <<< " & quoted form of inputText