编辑剪贴板文本而不丢失格式样式并连续运行脚本

发布于 2024-10-27 04:46:09 字数 923 浏览 7 评论 0原文

我正在使用这个 AppleScript 来编辑我的剪贴板数据。但有一些事情我不知道该怎么做:

  1. 此脚本删除剪贴板中文本的整个格式样式。有没有办法保留所有格式?

  2. 我尝试将此脚本作为应用程序运行(在保存时选中保持打开状态),但它在重新启动后只能运行一次,并且不会编辑任何新复制的文本。我怎样才能让这个脚本连续运行?

这是我的脚本:

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:

  1. This script removes the whole format-style of the text in the clipboard. Is there a way to preserve all the formats?

  2. 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 技术交流群。

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

发布评论

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

评论(2

烙印 2024-11-03 04:46:09

坦率地说,维护格式的最简单方法是使用文本编辑器或页面等样式文本编辑器作为中间阶段并在那里进行操作。即在 Pages 中打开一个新文档,粘贴文本,执行查找和替换来修改文本,使用 GUI 脚本选择所有内容,然后将其复制回剪贴板。

您也可以使用此技术来设置样式模板,填写数据库信息,然后打印它或将其放在剪贴板上。我经常使用这个。我只是希望你能用数字来做到这一点。 (Number 的查找和替换没有键盘选项)

编辑:这是一个快速但肮脏的示例脚本,使用 Pages 作为中间位置来查找和替换将保持格式的文本。

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

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.

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
抚你发端 2024-11-03 04:46:09

首先,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.

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