如何保留 applescript 运行 和重定向网址?

How to keep applescript running and redirecting urls?

我有这段代码,我可以将 运行 保存在脚本编辑器中。但这让我的笔记本电脑粉丝尖叫!所以我不确定这样做是否正确?

repeat until application "Google Chrome" is not running
    if application "Google Chrome" is running then
        tell application "Google Chrome"
            set (URL of every tab of every window where URL is equal to "facebook.com") to "twitter.com"
        end tell
    end if
end repeat

另一个问题是为什么我的代码不起作用?我想用它来从特定的 url 重定向到另一个。但即使这个实现也不起作用..如果我打开 facebook.com 它不会重定向到 twitter.com.

感谢任何帮助。

注意:是的,我想为此使用 applescript,所以请关注它,请 :)

当应用程序似乎可以工作,但似乎不符合其条件时,查看正在检查的实际值总是有帮助的。这种情况下,打开你认为应该匹配的URL,然后把那个URL拿来看。

tell application "Google Chrome"
    get URL of tab 1 of window 1
end tell

当我在Chrome中新建一个window输入“Facebook.com”,然后运行上面的脚本,发现是https://www.facebook.com/?_rdr=p .

当我将您的代码示例中的“Facebook.com”替换为 https://www.facebook.com/?_rdr=p 时,脚本现在产生了影响;它重定向到 Chrome 的关于页面。

从条件中使用完整 URL 的必要性中得到启发,我将“twitter.com”切换为“http://twitter.com/”。

脚本现在按照我的理解执行:每个 window 中与 https://www.facebook.com/?_rdr=p 匹配的每个选项卡都被重定向到 twitter.com。

repeat until application "Google Chrome" is not running
    if application "Google Chrome" is running then
        tell application "Google Chrome"
            set (URL of every tab of every window where URL is equal to "https://www.facebook.com/?_rdr=p") to "http://twitter.com/"
        end tell
    end if
end repeat

Applescript 和 Google Chrome 正在将您的条件文本与完整的 URL 进行匹配,因此您的条件也必须使用完整的 URL。

@vadian 关于使用空闲处理程序的说明非常好。有关使用空闲处理程序的想法,请参阅 this Hourly Pop-up Alert question。这样的事情应该有效:

on idle
    if application "Google Chrome" is running then
        tell application "Google Chrome"
            set (URL of every tab of every window where URL is equal to "https://www.facebook.com/?_rdr=p") to "http://twitter.com/"
        end tell
    end if

    --number of seconds to wait for the next check
    return 1
end idle

另存为应用程序,在 运行 处理程序后保持打开状态。