【问题标题】:AppleScript error when trying to get URL from Opera browser尝试从 Opera 浏览器获取 URL 时出现 AppleScript 错误
【发布时间】:2022-08-11 19:32:54
【问题描述】:

我在 Opera 浏览器中打开了一个页面,我试图从终端获取它的 URL。

我设法这样做:

osascript -e  \'tell application \"System Events\" to return value of text field \"Address field\" of first group of text field \"Address bar\" of toolbar \"Navigation\" of group \"Browser contents\" of second group of first group of front window of application process \"Opera\"\'

它可以工作,但它需要访问系统事件和可访问性权限集。我想像this solution for Google Chrome 一样直接访问 Opera。 所以我尝试像这样重写它:

osascript -e  \'tell application \"Opera\" to return value of text field \"Address field\" of first group of text field \"Address bar\" of toolbar \"Navigation\" of group \"Browser contents\" of second group of first group of front window\'

但出现错误:

49:56: syntax error: A “\"” can’t go after this identifier. (-2740)

任何想法如何解决它?

  • 您只能从系统事件中执行 GUI 脚本; Opera 不理解这些术语中的任何一个。如果 Opera 有自己的脚本字典(我没有 Opera,所以我不知道)那将是最好的方法,否则就无法避免系统事件。

标签: applescript ui-automation


【解决方案1】:

可以在不使用“系统事件”的情况下与 Opera(或基于 Safari 和 Chromium 的浏览器)进行交互。

打开“Script Editor.app”: 文件 > 打开字典 > 并从字典列表中选择“Opera.app”,您可以浏览它的字典。

基于此,使用以下 sn-p 您可以获取活动选项卡的 URL:

tell application "Opera"
    return URL of active tab of front window
end tell

【讨论】:

    猜你喜欢
    • 2014-03-24
    • 1970-01-01
    • 2013-02-06
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 2023-04-02
    • 2020-10-05
    相关资源
    最近更新 更多