AppleScript:当我在其他桌面空间中时,如何重新加载 Safari 窗口而不将窗口带到前台

Bar*_*son 2 applescript

我想编写一个 AppleScript 来完成一个简单的任务。也就是说,我在桌面 1 上打开一个 Safari 窗口,并在桌面 2 上进行日常工作时每 30 分钟自动重新加载浏览器。

这是我的脚本:

tell application "Safari"
    activate
    tell application "System Events"
        tell process "Safari"
            keystroke "r" using {command down}
        end tell
    end tell
end tell
Run Code Online (Sandbox Code Playgroud)

这个脚本有效。但是,每当执行该脚本时,我当前的桌面 2 都会返回到桌面 1。这会分散我的工作流程。

有没有办法让 Safari 在后台重新加载,而不将 Safari 窗口带到桌面 1 的前台?

我做了几次搜索;他们中的许多人说我不应该在脚本中使用“activate”。我尝试过,但脚本什么也不做。

mar*_*nte 5

您可以简单地获取文档 1URL ,然后告诉 Safari 将文档 1URL设置为该 URL。

效果是页面将重新加载。当您在另一个空间时,无需将 Safari 带到前面或跳转到 Safari。

    tell application "Safari"
    set docUrl to URL of document 1

    set URL of document 1 to docUrl
end tell
Run Code Online (Sandbox Code Playgroud)

还有一个简单的空闲处理程序,并保存为应用程序(保持打开状态选中),该应用程序将每 1 分钟运行一次,但当 Safari 位于最前面时不会运行。即当你实际使用它时。

on idle

    set frontApp to name of application (path to frontmost application as text)

    if frontApp is not equal to "Safari" then
        tell application "Safari"
            set docUrl to URL of document 1

            set URL of document 1 to docUrl
        end tell

    end if
    return 60
end idle
Run Code Online (Sandbox Code Playgroud)