【问题标题】:Replacing Text Online with Applescript用 Applescript 在线替换文本
【发布时间】:2020-02-26 03:50:27
【问题描述】:

这里是 Applescript 的新手。我正在尝试制作一个脚本,可以在线扫描文本编辑器中的文本并用其他词替换单词。

在我的具体情况下,它用 "" 替换了一些 html sn-ps — 即删除它们。

我当前的脚本收到以下错误消息... “Brave Browser 出现错误:窗口 1 不理解“执行”消息。”来自窗口 1 的数字 -1708

这是下面的脚本。有问题的行似乎是 EditorField 变量的来源。

有什么建议或想法可以让它发挥作用吗?谢谢。

    tell application "Brave Browser"
        activate
        my replaceWordWithStringInBodyText(" ", "")
        my replaceWordWithStringInBodyText("<span class=\"Apple-converted-space\">", "")
        my replaceWordWithStringInBodyText("<b>", "<strong>")
    end tell

    on replaceWordWithStringInBodyText(searchWord, replacementString)
        tell application "Brave Browser"
            activate
            tell window 1
                set EditorField to (execute javascript "document.getElementById('\"wp-content-editor-container\"')")
                tell EditorField
                    -- start at the end and go to the beginning
                    repeat with i from the (count of paragraphs) to 1 by -1
                        tell paragraph i
                            repeat
                                try
                                    if exists searchWord then
                                        set (last word where it is searchWord) to replacementString
                                    else
                                        exit repeat
                                    end if
                                on error errorMessage
                                    exit repeat
                                end try
                            end repeat
                        end tell
                    end repeat
                end tell
            end tell
            return true
        end tell
    end replaceWordWithStringInBodyText

【问题讨论】:

  • 顺便说一句,这不是导致错误的原因,但是将 ('\"wp-content-editor-container\"') 更改为 ('wp-content-editor-container') 因为不需要在单引号内使用转义的双引号.此外,包含目标页面的 URL 始终是一个好主意,假设每个人都可以访问它,这样我们就可以针对与您相同的 代码 进行测试。跨度>

标签: javascript applescript


【解决方案1】:

我假设您在 Brave 浏览器中点击了 Allow JavaScript from Apple Events,在 View 上进行检查> 开发者菜单。

话虽如此,并且由于您没有包含 URL 来测试您的 代码,所以让我举一个示例来说明如何单击 按钮,在右上角,如果我在 Brave 浏览器中遇到这个问题:

tell application "Brave Browser" to tell active tab of front window to execute javascript ¬
    "document.getElementsByClassName('ws-nowrap s-btn s-btn__primary')[0].click();"

如您所见,还必须与目标选项卡以及目标窗口进行通信。所以你需要相应地调整你的代码

【讨论】:

  • 帮了大忙,谢谢!该脚本现在运行而不会抛出错误消息,唯一的问题是该脚本实际上并没有替换文本中的任何内容。我无法分享直接链接,因为它是私有的,但它是使用 WordPress 帖子编辑器中内置 HTML 编辑器的 WordPress 草稿帖子 - 不知道是否有人熟悉使用此链接并可能有任何想法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
相关资源
最近更新 更多