我有使用ScriptingBridge的Objective-C代码,可以让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"到#34; GoogleChrome.h"和" com.apple.Safari"到" com.google.Chrome"。我不确定如何改变最后三行 - 没有定义" GoogleDocument"在GoogleChrome.h中
答案 0 :(得分: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];
答案 1 :(得分:1)
我发现获得所需内容的唯一方法是使用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\"