Macos 在不丢失格式样式的情况下编辑剪贴板文本,并连续运行脚本
我正在使用此AppleScript编辑剪贴板数据。但有些事情我不知道该怎么做:Macos 在不丢失格式样式的情况下编辑剪贴板文本,并连续运行脚本,macos,background,format,applescript,clipboard,Macos,Background,Format,Applescript,Clipboard,我正在使用此AppleScript编辑剪贴板数据。但有些事情我不知道该怎么做: 此脚本将删除剪贴板中文本的整个格式样式。有没有办法保存所有格式 我尝试将此脚本作为应用程序运行(保存时保持打开状态处于选中状态),但它只在重新启动后运行一次,并且不会编辑任何新复制的文本。如何使此脚本连续运行 这是我的剧本: on idle get the clipboard replacement of "SqlConnection" by "OleDbConnection" for the re
on idle
get the clipboard
replacement of "SqlConnection" by "OleDbConnection" for the result
replacement of "SqlDataAdapter" by "OleDbDataAdapter" for the result
set the clipboard to (replacement of "SqlCommand" by "OleDbCommand" for the result)
end idle
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
首先,applescript仅适用于文本,而不适用于格式化文本。因此,一旦您将剪贴板放入applescript,您将丢失所有格式。对此你无能为力。其次,为了让“on idle”处理程序工作,您需要返回一个时间值,即处理程序将再次运行的时间。因此,在“end idle”语句之前添加“return 10”,这意味着每10秒运行一次脚本。坦率地说,维护格式的最简单方法是使用样式化的文本编辑器(如文本编辑或页面)作为中间阶段,并在那里对其进行操作。i、 e.在页面中打开新文档,粘贴文本,进行查找和替换以修改文本,使用GUI脚本选择“全部”,然后将其复制回剪贴板 您还可以使用此技术设置样式化模板,填写数据库信息,然后将其打印或放在剪贴板上。我经常用这个。我只希望你能用数字来做。(数字的查找和替换没有键盘选项) 编辑:下面是一个快速而肮脏的示例脚本,它使用页面作为中间位置来查找和替换将保持格式的文本
tell application "Pages"
activate
make new document
end tell
tell application "System Events"
tell process "Pages"
-- paste clipboard
keystroke "v" using (command down)
-- go to top of document
key code 126 using (command down)
-- open find window
keystroke "f" using (command down)
-- set word to replace
keystroke "original"
-- tab to replace field
keystroke tab
-- set word to replace with
keystroke "newword"
-- press replace all button
click button "Replace All" of tab group 1 of window "Find & Replace"
-- close find window
keystroke "w" using (command down)
-- select all text
keystroke "a" using (command down)
-- copy back to clipboard
keystroke "c" using (command down)
end tell
end tell
如果我使用页面或类似的东西,那么我又回到了开始,这意味着手动替换所有内容(当然我使用查找/替换,但我必须自己做,这正是我开始使用applescript的原因-为了防止这种情况。在快照文本上一次又一次地替换相同的3-4个单词时有点奇怪)不,那正是你不必做的。您可以通过编程方式执行查找和替换。只要使用GUI脚本就可以了——我会看看今晚晚些时候是否能编写一个脚本来演示这一点。