【问题标题】:Get the URL of the frontmost tab from Chrome on OS X?从 OS X 上的 Chrome 获取最前面选项卡的 URL?
【发布时间】:2010-03-20 12:46:48
【问题描述】:

有没有办法对 Chrome 执行此操作?

tell application "Google Chrome"
 return URL of front document as string
end tell

...不起作用。

【问题讨论】:

    标签: google-chrome applescript


    【解决方案1】:

    现在 Chrome (7+) 支持 AppleScript,您可以这样做:

    tell application "Google Chrome"
        get URL of active tab of first window
    end tell
    

    注意:“第一个窗口”是指最前面的窗口。

    【讨论】:

    • 是否有针对 Chrome 特定 AppleScript 命令的文档?例如,如果我想做一些事情,比如获取活动选项卡的 HTML 内容,我在哪里寻找方法呢?
    • 除了在脚本编辑器中打开 Chrome 应用程序可以看到的字典之外,我找不到太多文档。
    【解决方案2】:

    有一种更简单的方法来检索 URL。

    'tell application "Google Chrome" to return URL of active tab of front window'

    经过测试并像魅力一样工作。

    【讨论】:

      【解决方案3】:

      由于没有applescript 支持,唯一的办法就是通过GUI 脚本,我不确定您对applescript 的熟悉程度,但是gui 脚本是一个不好的做法。所以请谨慎使用此代码。同样重要您可以访问在您的通用访问首选项中启用的辅助设备。

        tell application "Google Chrome"
          activate
          tell application "System Events"
              tell application process "Google Chrome"
                  get value of text field 1 of tool bar 1 of window 1
              end tell
          end tell
        end tell
      

      【讨论】:

      • 哇!我刚刚编写了以下脚本,因此我可以更轻松地将 PasswordMaker 与 Chrome 一起使用,感谢您的回答!告诉应用程序“系统事件”告诉应用程序进程“谷歌浏览器”设置 URL 以获取窗口 1 工具栏 1 的文本字段 1 的值 end 告诉 end 告诉设置域部分为 splitText(".", item 3 in splitText("/" , theURL)) 将topdom 设置为joinText(".", {item ((count domainparts) - 1) of domainparts, item ((count domainparts)) of domainparts}) 执行shell脚本“/Users/dmd/bin/pmo” & 顶级
      • 你不能把代码放在 cmets 他们只是 cmets 你可以编辑你的问题并将你的最终代码放在底部确保你标记它
      【解决方案4】:
      【解决方案5】:

      我认为您可以使用此脚本获得最前面的选项卡:

      tell application "Google Chrome"
          set frontIndex to active tab index of front window
          get URL of tab frontIndex of front window
      end tell
      

      希望我做对了。试试看。

      【讨论】:

      • 或者只是tell app "Google Chrome" to tell window 1 to URL of tab active tab index。或tell app "Google Chrome" to URL of active tab of window 1.
      猜你喜欢
      • 2016-07-30
      • 1970-01-01
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      • 2019-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多