【问题标题】:Mac: reloading document in Chrome or Firefox?Mac:在 Chrome 或 Firefox 中重新加载文档?
【发布时间】:2010-01-19 05:13:29
【问题描述】:

如何让 Chrome 或 Firefox 重新加载顶部窗口中的文档?这是我用于 Safari 的内容:

osascript -e '
    tell application "Safari"
      activate
      do JavaScript "history.go(0)" in document 1
    end tell
'

【问题讨论】:

    标签: macos firefox google-chrome applescript


    【解决方案1】:

    这是 Chrome 的代码:

    tell application "Google Chrome"
        tell the active tab of its first window
            reload
        end tell
    end tell
    

    或者更简洁:

    tell application "Google Chrome" to tell the active tab of its first window
        reload
    end tell
    

    【讨论】:

    • 这个脚本值得 IMO 更多的支持,因为它不需要您在重新加载之前将浏览器置于焦点 - 这是关键。
    • 您也可以使用“第一个标签”而不是“活动标签”。
    【解决方案2】:

    我不认为 Firefox 或 Chrome 有特殊的 Applescript 支持,但您可以发送击键 (Cmd-R) 来刷新页面:

    tell application "Firefox"
        activate
        tell application "System Events" to keystroke "r" using command down
    end tell
    

    【讨论】:

    • Firefox 应该可以,但显然它已经被当前的 3.5.x 版本破坏了。
    • 漂亮!这适用于所有三种浏览器,使用“Firefox”、“Google Chrome”和“Safari”作为应用程序字符串。
    【解决方案3】:

    这是在 Safari 中不使用 JavaScript 的另一种方法:

    tell application "Safari"
        tell its first document
            set its URL to (get its URL)
        end tell
    end tell
    

    【讨论】:

      【解决方案4】:

      自动机

      • 打开Automator并选择一个新建文档
      • 选择服务

      • 服务接收设置为无输入
      • 从动作列表中选择 Run AppleScript 动作。
      • 在脚本中粘贴以下代码:

       

      tell application "Google Chrome" to tell the active tab of its first window
      reload
      end tell
      

      • 保存服务,例如,使用名称Chrome Refresh

      系统偏好设置

      • 打开系统偏好设置 > 键盘
      • 快捷方式选项卡中,选择服务
      • 分配新的快捷方式

      【讨论】:

        【解决方案5】:

        上面的以下答案效果很好,但如果它是最后一个焦点选项卡/窗口,则使用它们会导致 DevTools 在新选项卡中刷新。我不希望 DevTools 在新选项卡中刷新,我只想刷新第一个选项卡,而不管最后一个焦点/活动如何,这对我来说效果很好。留给搜索此用例的人。

        tell application "Google Chrome"
          activate
          tell application "System Events"
            tell process "Google Chrome"
              keystroke "r" using {command down, shift down}
            end tell
          end tell
        end tell
        

        【讨论】:

          猜你喜欢
          • 2012-01-02
          • 1970-01-01
          • 2019-10-28
          • 1970-01-01
          • 2016-12-30
          • 2018-01-10
          • 2017-01-15
          • 2012-08-30
          • 2022-08-12
          相关资源
          最近更新 更多