gpt4 book ai didi

swift - 使用 Chrome 脚本桥强制解开错误

转载 作者:行者123 更新时间:2023-11-30 12:38:06 24 4
gpt4 key购买 nike

我正在尝试 Apple 的 Scripting Bridge 与 Google Chrome 进行交互。我从 https://stackoverflow.com/a/24147285 中的代码开始

我使用 sdefsdp 命令创建了 .h 文件。

我已经包含了该文件,还创建了桥接 header ,并在桥接 header 中导入了 chrome 的头文件。但问题是在 Swift 中使用 header 。代码很旧,Swift 已经改变了很多。

我的 Swift 文件:

import Cocoa
import ScriptingBridge


var chromeObject = SBApplication.init(bundleIdentifier: "com.google.Chrome")! as AnyObject

print(chromeObject.closeable)

我收到一条错误消息

fatal error: unexpectedly found nil while unwrapping an Optional value

我做错了什么?

最佳答案

SBApplication返回应用程序对象。你可以看到不同的interface位于.h文件。

@interface ChromeApplication : SBApplication
@interface ChromeWindow : SBObject <ChromeGenericMethods>
@interface ChromeApplication (ChromiumSuite)
@interface ChromeTab : SBObject <ChromeGenericMethods>
@interface ChromeBookmarkFolder : SBObject <ChromeGenericMethods>
@interface ChromeBookmarkItem : SBObject <ChromeGenericMethods>

所以,SBApplication返回给你ChromeApplication 。您可以调用 ChromeApplication 中定义的任何属性。自己尝试一下。您不会收到任何错误。

问题是您正在调用 closeable这是 ChromeWindow 的一部分.

来自Apple Documentation :

AppleScript and Objects

AppleScript is an object-oriented language. When you write, compile, and execute scripts, everything you work with is an object. An object is an instantiation of a class definition, which can include properties and actions. AppleScript defines classes for the objects you most commonly work with, starting with the top-level script object, which is the overall script you are working in.

..............

.....................

最重要的是,

What Is in a Script Object

When you enter AppleScript statements in script window in Script Editor, you are working in a top-level script object. All script object definitions follow the same syntax, except that a top-level script object does not have statements marking its beginning and end.

A script object can contain the following:

Property definitions (optional): A property is a labeled container in which to store a value.

An explicit run handler (optional): A run handler contains statements AppleScript executes when the script is run. (For more information, see run Handlers.)

An implicit run handler (optional): An implicit run handler consists of any statements outside of any contained handlers or script objects.

Additional handlers (optional): A handler is the equivalent of a subroutine. (For details, see About Handlers.)

Additional script objects (optional): A script object can contain nested script objects, each of which is defined just like a top-level script object, except that a nested script object is bracketed with statements that mark its beginning and end. (For details, see Script Objects.)

所以,简单来说,Application是一个包含 Window 的对象这是一个包含 Tab 的对象对象......

您需要检索Window来自 Application 的对象/元素使用closeable .

你应该有SBElementArray在每个 interface 。你需要得到它。

示例,

// The application's top-level scripting object.
@interface ChromeApplication : SBApplication

- (SBElementArray<ChromeWindow *> *) windows;

@property (copy, readonly) NSString *name; // The name of the application.
@property (readonly) BOOL frontmost; // Is this the frontmost (active) application?
@property (copy, readonly) NSString *version; // The version of the application.

- (void) open:(NSArray<NSURL *> *)x; // Open a document.
- (void) quit; // Quit the application.
- (BOOL) exists:(id)x; // Verify if an object exists.

@end

您应该检索- (SBElementArray<ChromeWindow *> *) windows;使用closable 。同样,在 Windows 中,您有选项卡数组等。

例如,在 AppleScript 中获取每个选项卡的 URL 和标题:

tell application "Google Chrome"

set a to ""
repeat with w in windows
repeat with t in tab in w // Getting tab object from window
set a to a & linefeed & title of t & " -URL: " & URL of t
end repeat
end repeat

end tell

Swift 中的等价物是:

import Cocoa
import ScriptingBridge


var chromeObject: AnyObject = SBApplication.init(bundleIdentifier: "com.google.Chrome")!

var f = chromeObject.windows() // get the windows from application
for i in f!
{
var t = (i as AnyObject).tabs() // get the tabs from windows
for j in t!
{
print(((j as AnyObject).title as String) + " -URL: " + ((j as AnyObject).url as String))
}
}

希望对你有帮助!

关于swift - 使用 Chrome 脚本桥强制解开错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42613704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com