如何让Chrome在Objective-C中使用ScriptingBridge打开URL?

如何让Chrome在Objective-C中使用ScriptingBridge打开URL?,objective-c,google-chrome,safari,scripting-bridge,Objective C,Google Chrome,Safari,Scripting Bridge,我有工作的Objective-C代码,它使用ScriptingBridge使Safari打开URL。比如: #import "Safari.h" /* created by executing "sdef /Applications/Google\ Chrome.app | sdp -fh --basename GoogleChrome" */ if ((safariApp = [SBApplication applicationWithBundleIdentifier:@"com.apple

我有工作的Objective-C代码,它使用ScriptingBridge使Safari打开URL。比如:

#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”的定义。我发现获得所需内容的唯一方法是使用AppleScript

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

这同样适用于Safari和Firefox(当然,您需要将
\“Google Chrome\”
更改为
\“Safari\”
\“Firefox\”
)。

谢谢!AppleScript解决方案有效。我可以在我的生产代码中使用它。当然,如果有人知道正确的咒语,我仍然想要一个脚本桥解决方案。顺便说一句,我的程序需要能够控制Safari、Chrome或Firefox。它现在有一个用于Safari的ScriptingBridge解决方案和一个用于Chrome的AppleScript解决方案。我没有Firefox的有效解决方案。我以为我可以在您提供的AppleScript解决方案中将“Google Chrome”改为“Firefox”,但这行不通。啊!我用Firefox和Safari的版本更新了代码。谢谢,肖恩!这很好,所以我取消了基于AppleScript的缓慢解决方案。别忘了将ScriptingBridge框架添加到您的项目中。如果你不这样做,你会得到神秘的编译器错误。
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];