使用 Scripting Bridge 获取 Safari 文档标题/位置在全屏模式下不起作用

发布于 2024-08-24 18:31:27 字数 1792 浏览 6 评论 0原文

我正在尝试从最上面的 Safari 文档/选项卡获取 URL 和文档标题。我有一个 AppleScript 和一个使用 Apple 脚本桥框架的 Objective-C 版本。

这两个版本都适用于大多数网页,但是当我以全屏模式打开 Youtube 视频时,基于 Scripting Bridge 的版本会失败。 Apple 脚本适用于“普通”全屏 Safari 窗口。

有人能看出下面的 Scripting Bridge 代码有什么问题导致它在全屏 Safari 窗口中失败吗?

这里是代码(为简洁起见,我省略了错误检查):

AppleScript:

tell application "Safari"
 # Give us some time to open video in full-screen mode
 delay 10
 do JavaScript "document.title" in document 0
end tell

Scripting Bridge:

SafariApplication* safari = [SBApplication applicationWithBundleIdentifier:@"com.apple.Safari"];

SBElementArray* windows = [safari windows];
SafariTab* currentTab = [[windows objectAtIndex: 0] currentTab];

// This fails when in full-screen mode:
id result = [safari doJavaScript: @"document.title" in: currentTab];
NSLog(@"title: %@", result);

Scripting Bridge 错误(添加换行符):

  Apple event returned an error.  Event = 'sfri'\'dojs'{
   '----':'utxt'("document.title"),
   'dcnm':'obj '{ 'want':'prop',
                  'from':'obj '{ 'want':'cwin', 
                                 'from':'null'(),
                                 'form':'indx',
                                 'seld':1 },
                  'form':'prop',
                  'seld':'cTab' }
                }
    Error info = {
        ErrorNumber = -1728;
        ErrorOffendingObject = <SBObject @0x175c2de0: 
                   currentTab of SafariWindow 0 of application "Safari" (238)>;
    }

我找不到有关给定错误代码的详细信息。它抱怨“currentTab”,这表明 JavaScript 事件至少一路到达了 Safari。我假设当前选项卡接收到该事件,但拒绝运行JS代码,因为它处于全屏模式。但是,为什么这对 AppleScript 有效呢?他们最终不使用相同的代码路径吗?

非常感谢任何建议。谢谢!

I'm trying to get the URL and document title from the topmost Safari document/tab. I have an AppleScript and an objective-c version using Apple's Scripting Bridge framework.

Both versions work fine for most web pages, however when I open a Youtube video in full-screen mode, the Scripting Bridge based version fails. The Apple Script works fine for "normal" and full-screen Safari windows.

Can anyone see what is wrong with the Scripting Bridge code below to cause it to fail for full-screen Safari windows?

Here the code (I omitted error checking for brevity):

AppleScript:

tell application "Safari"
 # Give us some time to open video in full-screen mode
 delay 10
 do JavaScript "document.title" in document 0
end tell

Scripting Bridge:

SafariApplication* safari = [SBApplication applicationWithBundleIdentifier:@"com.apple.Safari"];

SBElementArray* windows = [safari windows];
SafariTab* currentTab = [[windows objectAtIndex: 0] currentTab];

// This fails when in full-screen mode:
id result = [safari doJavaScript: @"document.title" in: currentTab];
NSLog(@"title: %@", result);

Scripting Bridge error (with added line breaks):

  Apple event returned an error.  Event = 'sfri'\'dojs'{
   '----':'utxt'("document.title"),
   'dcnm':'obj '{ 'want':'prop',
                  'from':'obj '{ 'want':'cwin', 
                                 'from':'null'(),
                                 'form':'indx',
                                 'seld':1 },
                  'form':'prop',
                  'seld':'cTab' }
                }
    Error info = {
        ErrorNumber = -1728;
        ErrorOffendingObject = <SBObject @0x175c2de0: 
                   currentTab of SafariWindow 0 of application "Safari" (238)>;
    }

I could not find details about the given error code. It complains about 'currentTab' which shows that the JavaScript event at least made it all the way to Safari. I assume that the current tab receives the event, but refuses to run the JS code, because it is in full-screen mode. However, why does this work for an AppleScript? Don't they use the same code path eventually?

Any suggestions are greatly appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙与沫 2024-08-31 18:31:27

这似乎是 sdef/sdp 创建的头文件中的一个失败 - 你的 Objective-C 与你工作的 AppleScript 不平行(并且头文件似乎表明没有 doJavaScript:in: 可以在文档上工作,就像在你的AppleScript中一样)。当我使用下面的代码时,它与您的 AppleScript 并行,即使对于全屏 YouTube 视频,它也给出了没有错误的标题。为了使用 doJavaScript:in: 从 SafariDocument*SafariTab* 的转换感觉不对,但似乎有效。

SafariApplication* safari = [SBApplication applicationWithBundleIdentifier:@"com.apple.Safari"];

SBElementArray* documents = [safari documents];
SafariDocument* frontDocument = [documents objectAtIndex: 0];

// This *no longer* fails when in full-screen mode:
id result = [safari doJavaScript: @"document.title" in: (SafariTab *) frontDocument];
NSLog(@"title: %@", result);

This appears to be a failure in the header file created by sdef/sdp--your Objective-C does not parallel your working AppleScript (and the header file seemed to indicate that there is no doJavaScript:in: that would work on a document, as in your AppleScript). When I used the following code, which does parallel your AppleScript, it gave the title without error even for a full-screen YouTube video. The cast from SafariDocument* to SafariTab* in order to use doJavaScript:in: feels wrong, but seems to work.

SafariApplication* safari = [SBApplication applicationWithBundleIdentifier:@"com.apple.Safari"];

SBElementArray* documents = [safari documents];
SafariDocument* frontDocument = [documents objectAtIndex: 0];

// This *no longer* fails when in full-screen mode:
id result = [safari doJavaScript: @"document.title" in: (SafariTab *) frontDocument];
NSLog(@"title: %@", result);
祁梦 2024-08-31 18:31:27
tell application "Safari"
  get URL of current tab of front window
end tell

Objective-C 就很简单了

SafariApplication* safari = [SBApplication 
                            applicationWithBundleIdentifier:@"com.apple.Safari"];
for (SafariWindow *window in safari.windows) {
  if ([window visible]) {
    result = window.currentTab.URL;
    break;
  }
}
tell application "Safari"
  get URL of current tab of front window
end tell

The Objective-C is then simply

SafariApplication* safari = [SBApplication 
                            applicationWithBundleIdentifier:@"com.apple.Safari"];
for (SafariWindow *window in safari.windows) {
  if ([window visible]) {
    result = window.currentTab.URL;
    break;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文