如何使用ScriptingBridge告诉Safari在新窗口中打开URL?

时间:2012-04-04 04:26:53

标签: scripting-bridge

我有一个Objective-C应用程序,它使用ScriptingBridge来控制Safari。它有效,但我在编写新功能的代码时遇到问题 - 告诉Safari在新窗口中打开一个URL。这是完成我想要的AppleScript:

tell application "Safari"
make new document at end of documents
set URL of document 1 to "http://www.apple.com/"
end tell

这就是我希望使用ScriptingBridge的等效代码:

NSString *appName = @"com.apple.Safari";
safariApp = [SBApplication applicationWithBundleIdentifier:appName];

SafariDocument *doc = [[[safariApp classForScriptingClass:@"document"] alloc] init];
[[safariApp documents] addObject:doc];
doc.path = @"http://www.ford.com";

当我执行后面的代码时,Safari会打开一个新窗口,但窗口会显示我的主页,而不是www.ford.com。

怎么了?

1 个答案:

答案 0 :(得分:0)

以下是解决方案:

NSDictionary *theProperties = [NSDictionary dictionaryWithObject:@"http://www.ford.com" forKey:@"URL"];
SafariDocument *doc = [[[safariApp classForScriptingClass:@"document"] alloc] initWithProperties:theProperties];
[[safariApp documents] addObject:doc];
[doc release];