我正在将来自不同项目的大量源代码复制到其他项目中,我总是要更改相同的术语。是否可以使用applescript来检查剪贴板的文本内容并替换几个关键字?我是Apple的新手,所以我不知道Applecript有多强大......
答案 0 :(得分:4)
可以使用get clipboard
,set clipboard
和文本项分隔符。
get the clipboard
set the clipboard to (replacement of "this text" by "that text" for the result)
on replacement of oldDelim by newDelim for sourceString
set oldTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to oldDelim
set strtoks to text items of sourceString
set text item delimiters of AppleScript to newDelim
set joinedString to strtoks as string
set text item delimiters of AppleScript to oldTIDs
joinedString
end replacement
对于更复杂的文本操作,我只是调出一个shell脚本。以上变为:
do shell script "pbpaste | sed 's/this text/that text/g' | pbcopy"
答案 1 :(得分:2)
不确定我明白你想做什么。我想你想要在剪贴板内容中替换多个字符串,例如:“PS3在Wallmart上花费200美元”到“XBox在Wallmart花费180美元”。以下代码实现了这一目标:
get the clipboard
set the clipboard to (replacement of "PS3" by "XBox" for the result)
on replacement of oldDelim by newDelim for sourceString
set oldTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to oldDelim
set strtoks to text items of sourceString
set text item delimiters of AppleScript to newDelim
set joinedString to strtoks as string
set text item delimiters of AppleScript to oldTIDs
joinedString
end replacement
get the clipboard
set the clipboard to (replacement of "200" by "180" for the result)
感谢Michael J. Barber的原始代码。我对编码几乎一无所知。我只是尝试了这种修改它的工作原理。