我将很多源代码从不同的项目拷贝到其他项目,而且我总是需要更改相同的条款。是否可以使用applescript来检查剪贴板的文本内容并替换几个关键字?我是新来的applescript,所以我不知道苹果的功能有多强大......使用os中的applescript编辑剪贴板内容x
3
A
回答
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"
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的原始代码的荣誉。我对编码几乎一无所知。我只是尝试了这种修改工作。
相关问题
- 1. Linux - 用于编辑剪贴板内容的脚本
- 2. 语言C的OS X剪贴板C
- 3. 编译内容的剪贴板
- 4. 如何使用AppleScript的从剪贴板
- 5. 在AppleScript中设置剪贴板的内容
- 6. 如何使用Applescript显示剪贴板
- 7. 在Vim中使用Mac OS剪贴板
- 8. 使用Greasemonkey获取剪贴板内容
- 9. 设置剪贴板内容
- 10. 检查剪贴板内容
- 11. Applescript:从文本文件中读取内容,将内容复制到剪贴板
- 12. 读剪贴板中的内容在FF
- 13. 查看剪贴板中的内容
- 14. HTML内容从剪贴板中的JavaScript
- 15. VI编辑器:将整个文件放到剪贴板(特定于OS X)
- 16. Powershell 5剪贴板 - 手动粘贴“复杂”剪贴板内容不起作用
- 17. 获取RTF数据从Mac OS X的纸板(剪贴板)
- 18. 从OS X中的剪贴板获取文件的路径
- 19. AppleScript的:不能设置剪贴板
- 20. 使用javascript将内容复制/粘贴到剪贴板
- 21. 在Mac OS X上操作C语言中的剪贴板
- 22. 如何从OS X粘贴板/剪贴板中获取HTML数据?
- 23. 复制TinyMCE的编辑器的选择的内容复制到剪贴板
- 24. AppleScript剪贴板竞赛条件
- 25. applescript剪贴板图像浏览目录
- 26. 如何从AppleScript访问剪贴板?
- 27. 崩溃OS X粘贴板
- 28. 如何检查用AppleScript粘贴到剪贴板的值
- 29. 使用剪贴板
- 30. 将DataGridView内容复制到剪贴板
非常感谢您的快速回复!该脚本工作正常,但我有一堆进一步的问题:我如何实现保持字符串格式(目前它以纯文本形式返回,每种格式都被删除)。我也无法让脚本替换多于一个字符串并在后台连续运行?我尝试在“闲置”和“结束闲置”之间设置“get/set剪贴板...”代码,但它只是在我启动应用程序时第一次运行(我将它保存为应用程序并且保持打开状态) – TabulaRasa 2011-03-24 12:49:38