2

我有使用 ScriptingBridge 使 Safari 打开 URL 的 Objective-C 代码。就像是:

#import "Safari.h"  /* created by executing "sdef /Applications/Google\ Chrome.app | sdp -fh --basename GoogleChrome" */
if ((safariApp = [SBApplication applicationWithBundleIdentifier:@"com.apple.Safari"]) == nil) {
    NSLog(@"couldn't access Google Chrome");
} else {
    NSString *theUrl = [NSString stringWithUTF8String:"http://www.ford.com"];
    NSDictionary *theProperties = [NSDictionary dictionaryWithObject:theUrl forKey:@"URL"];
    SafariDocument *doc = [[[safariApp classForScriptingClass:@"document"] alloc] initWithProperties:theProperties];
    [[safariApp documents] addObject:doc];
}

我想创建类似的代码,为 Chrome 而不是 Safari 做同样的事情。显然,我需要将“Safari.h”更改为“GoogleChrome.h”,将“com.apple.Safari”更改为“com.google.Chrome”。我不确定如何更改最后三行 - GoogleChrome.h 中没有“GoogleDocument”的定义

4

2 回答 2

2
GoogleChromeApplication *application = [SBApplication applicationWithBundleIdentifier:@"com.google.Chrome"];
GoogleChromeWindow *window = [[[application classForScriptingClass:@"window"] alloc] initWithProperties:nil];
[application.windows addObject:window];
window.activeTab.URL = @"http://www.example.com";
[window release];
[application activate];
于 2013-01-30T01:22:53.547 回答
1

我发现获得所需内容的唯一方法是使用 AppleScript。

NSString *script = @"tell application \"Google Chrome\" to \
                    open location \"http://www.ford.com\"";
NSAppleScript* appleScript = [[NSAppleScript alloc] initWithSource: script];
[appleScript executeAndReturnError:nil];

这也适用于SafariFirefox(当然您需要\"Google Chrome\"使用\"Safari\"或进行更改\"Firefox\")。

于 2012-11-01T08:02:04.480 回答