编辑剪贴板文本而不丢失格式样式并连续运行脚本
我正在使用这个 AppleScript 来编辑我的剪贴板数据。但有一些事情我不知道该怎么做:
此脚本删除剪贴板中文本的整个格式样式。有没有办法保留所有格式?
我尝试将此脚本作为应用程序运行(在保存时选中保持打开状态),但它在重新启动后只能运行一次,并且不会编辑任何新复制的文本。我怎样才能让这个脚本连续运行?
这是我的脚本:
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
I'm using this AppleScript to edit my clipboard data. But there are some things I couldn't figure out how to do:
This script removes the whole format-style of the text in the clipboard. Is there a way to preserve all the formats?
I tried to run this script as an application (with stay open checked on saving) but it just works once after a fresh start and doesn't edit any new copied text. How can I get this script to run continuously?
Here's my script:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
坦率地说,维护格式的最简单方法是使用文本编辑器或页面等样式文本编辑器作为中间阶段并在那里进行操作。即在 Pages 中打开一个新文档,粘贴文本,执行查找和替换来修改文本,使用 GUI 脚本选择所有内容,然后将其复制回剪贴板。
您也可以使用此技术来设置样式模板,填写数据库信息,然后打印它或将其放在剪贴板上。我经常使用这个。我只是希望你能用数字来做到这一点。 (Number 的查找和替换没有键盘选项)
编辑:这是一个快速但肮脏的示例脚本,使用 Pages 作为中间位置来查找和替换将保持格式的文本。
The easiest way to maintain formatting is frankly to use a styled text editor like Text Edit or Pages as an intermediate stage and manipulate it there. i.e. open a new document in Pages, paste your text, do a find and replace to modify the text, select all using GUI scripting and then copy it back to the clipboard.
You can use this technique as well to setup a styled template, fill in database information, and then either print it or put it on the clipboard. I use this quite a lot. I just wish you could do it with Numbers. (Number's find and replace has no keyboard option)
Edit: Here's a quick and dirty sample script to use Pages as an intermediary place to find and replace text that will maintain formatting.
首先,applescript 只适用于文本,不适用于格式化文本。因此,一旦您将剪贴板带入 applescript,您就丢失了所有格式。你对此无能为力。其次,为了使“空闲”处理程序正常工作,您需要返回一个时间值,即处理程序再次运行的时间。因此,在“endidle”语句之前添加“return 10”,这意味着每 10 秒运行一次脚本。
First, applescript only works with text, not formatted text. So once you bring the clipboard into applescript you have lost all formatting. Nothing you can do about that. Second, in order for the "on idle" handler to work you need to return a time value, which is the time when the handler will run again. So just before the "end idle" statement add "return 10" which means run the script every 10 seconds.