使用 Scripting Bridge 获取 Safari 文档标题/位置在全屏模式下不起作用
我正在尝试从最上面的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎是 sdef/sdp 创建的头文件中的一个失败 - 你的 Objective-C 与你工作的 AppleScript 不平行(并且头文件似乎表明没有 doJavaScript:in: 可以在文档上工作,就像在你的AppleScript中一样)。当我使用下面的代码时,它与您的 AppleScript 并行,即使对于全屏 YouTube 视频,它也给出了没有错误的标题。为了使用 doJavaScript:in: 从
SafariDocument*
到SafariTab*
的转换感觉不对,但似乎有效。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*
toSafariTab*
in order to use doJavaScript:in: feels wrong, but seems to work.Objective-C 就很简单了
The Objective-C is then simply