在 os x 中使用 applescript 编辑剪贴板内容

发布于 2024-10-26 07:20:47 字数 129 浏览 4 评论 0原文

我将大量源代码从不同的项目复制到其他项目,并且我总是必须更改相同的条款。是否可以使用 applescript 检查剪贴板的文本内容并替换多个关键字?我是 applescript 的新手,所以我不知道 applescript 有多强大......

I'm copying a lot of source code from different projects to others and I always have to change the same terms. Is it possible to use an applescript which checks the text-content of the clipboard and replaces several keyword? I'm new to applescript so I'm not aware of how powerful applescript can be...

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

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

发布评论

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

评论(2

天煞孤星 2024-11-02 07:20:47

这可以使用get Clipboardset 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"

This is possible using get clipboard, set clipboard, and the text item delimiters.

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

For more sophisticated text manipulation, I'd just call out to a shell script. The above becomes:

do shell script "pbpaste | sed 's/this text/that text/g' | pbcopy"
缪败 2024-11-02 07:20:47

不确定我是否明白你想做什么。我认为您想要替换剪贴板内容中的多个字符串,例如:“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 的原始代码。我对编码几乎一无所知。我刚刚尝试了这个修改,它有效。

Not sure that I understood what you want to do. I reckon you want to replace multiple strings within the clipboard content, for example: "PS3 costs 200 dollars at Wallmart" to "XBox costs 180 dollars at Wallmart". The following code achieves this:

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)

Kudos to Michael J. Barber for the original code. I know virtually nothing about coding. I just tried this modification it worked.

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