【问题标题】:Open URL and Activate Google Chrome via Applescript通过 Applescript 打开 URL 并激活 Google Chrome
【发布时间】:2014-02-28 22:57:42
【问题描述】:

我有以下多年前编写的 AppleScript。我使用此代码对 Harmony One 通用遥控器上的按钮进行编程,以通过 Google Chrome 访问在线视频服务。代码不起作用。谷歌浏览器不启动。我正在通过 RemoteBuddy 运行代码。代码可以正常运行,但不起作用。

有人对可能是什么问题有任何想法,或者我可以如何改进脚本以使其正常工作?

tell application "System Events" to set open_applications to (name of everyprocess)
if (open_applications contains "Google Chrome") is true then
        tell application "Google Chrome" to quit
else
        tell application "Google Chrome"
                activate
                open location "http://xfinitytv.comcast.net"
        end tell
        delay 1
        tell application "Google Chrome" to activate
end if

【问题讨论】:

  • 我刚刚测试了你的脚本。我将“(每个进程的名称)”更改为“(每个进程的名称)”并且它有效。无论如何,您仍然可以使用我的答案中的代码,因为不使用“系统事件”会更快。

标签: applescript


【解决方案1】:

试试这个方法:

tell application "Google Chrome"
    if it is running then
        quit
    else
        activate
        open location "http://xfinitytv.comcast.net"
        delay 1
        activate
    end if
end tell



注意:它使用较新的“增强型应用程序模型”(第二行),更多信息在这里:
How to check in AppleScript if an app is running, without launching it - via osascript utility

【讨论】:

  • ...与书签不同,您可以在激活后执行javascript:execute front window's active tab javascript "alert('example');"
【解决方案2】:
tell application "Google Chrome"
    open location "http://WEBSITEHERE.com"
end tell

【讨论】:

  • 您能否在您的答案周围添加更多上下文。仅代码或仅链接的答案很难理解。如果您可以在帖子中添加更多信息,这将对提问者和未来的读者都有帮助。
  • 虽然优雅而简单,但这总是会为该 URL 打开一个 new 选项卡并为其提供焦点。如果您多次执行此操作,您最终会为同一个 URL 提供多个选项卡。上面的其他答案通过先关闭浏览器然后重新打开它(没有标签)来避免这种情况。这还有其他缺点(例如,我的电子邮件和日历标签去哪儿了?)。必须存在更好的解决方案。有人吗?
【解决方案3】:

osascript -e '告诉应用程序“Google Chrome”打开位置“http://yourlink.com.html”'

【讨论】:

    猜你喜欢
    • 2012-05-09
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 2022-08-03
    • 2012-06-27
    • 2019-03-29
    • 1970-01-01
    相关资源
    最近更新 更多