如何使用 Objective-c 创建 Numbers 电子表格?
我正在编写一个 Cocoa 应用程序,我想使用 Scripting Bridge 从我的应用程序生成一个 Numbers 电子表格。我已经生成了 Numbers.h 文件,并按照 Apple 文档。下面是我用来尝试简单地创建 Numbers 文档并保存它的代码。
NSString *path = @"/Users/username/Desktop/Test.numbers";
NumbersApplication *numbers = [SBApplication applicationWithBundleIdentifier:@"com.apple.iWork.Numbers"];
[numbers activate];
NumbersDocument *document = [[[numbers classForScriptingClass:@"document"] alloc] initWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:project.title, @"name", nil]];
[[numbers documents] addObject:document];
[document saveAs:nil in:[NSURL URLWithString:path]];
代码编译并运行,当我尝试 saveAs:in: 方法时,出现以下错误:
-[SBProxyByClass saveAs:in:]: 对象尚未添加到容器中;选择器无法识别 [self = 0x2005912e0]
除了将文档添加到 [numbers Documents] 数组之外,我还需要做其他事情吗?
我愿意使用 AppleScript,但如果可以的话我更愿意使用脚本桥。
I'm writing a Cocoa application and I'd like to generate a Numbers spreadsheet from my application using Scripting Bridge. I've generated the Numbers.h file and linked the ScriptingBridge.framework per the directions in Apple's Documentation. Below is the code I'm using to try to simply create a Numbers document and save it.
NSString *path = @"/Users/username/Desktop/Test.numbers";
NumbersApplication *numbers = [SBApplication applicationWithBundleIdentifier:@"com.apple.iWork.Numbers"];
[numbers activate];
NumbersDocument *document = [[[numbers classForScriptingClass:@"document"] alloc] initWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:project.title, @"name", nil]];
[[numbers documents] addObject:document];
[document saveAs:nil in:[NSURL URLWithString:path]];
The code compiles and runs and when I try the saveAs:in: method I get the following error:
-[SBProxyByClass saveAs:in:]: object has not been added to a container yet; selector not recognized [self = 0x2005912e0]
Is there something else I have to do besides adding the document to the [numbers documents] array?
I'm open to using AppleScript, but I'd prefer to using the Scripting Bridge if I can.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
呃,用 SB 编写 Numbers 脚本;一件黑术的代价就是两件黑术。我建议首先尝试在 AppleScript 中执行此操作,以便稍微缩小问题范围。
如果它在 AS 中也出现问题,那么要么是您的命令措辞错误,要么是 Numbers 中存在问题。由于大多数应用程序脚本编写者都使用 AppleScript,因此如果您可以提供他们可以识别的代码,您会发现更容易获得帮助。
如果它有效,那么要么你对 ObjC 的命令翻译不正确,要么 SB 中有问题。在 AS 中拥有一个工作示例将为找出问题所在提供一个起点。
您还可以查看 objc-appscript,它提供了 SB 的更可靠、更少混淆的替代方案。它的 ASTranslate 工具可以轻松地将工作 AS 命令转换为 ObjC 语法。
Ehh, Numbers scripting with SB; two black arts for the price of one. I would suggest trying to do it in AppleScript first, in order to narrow down the problem a bit.
If it breaks in AS too, then either you've phrased the commands wrongly or there's a problem in Numbers. Since most application scripters use AppleScript, you'll find it easier to get help if you can present code they'll recognise.
If it works, then either your translation of the commands to ObjC is incorrect or there's a problem in SB. Having a working example in AS will provide a starting point for figuring out where things are going wrong.
You might also look into objc-appscript, which provides a more reliable, less obfuscated alternative to SB. Its ASTranslate tool makes it easy to translate working AS commands to ObjC syntax.
Numbers 尚不支持通过 Applescript 创建文档。您必须使用 GUI 脚本。新版本的 Numbers 预计将于 2011 年 1 月 6 日发布,并且(希望)能够修复其严重受限的 Applescript 支持问题。
Numbers doesn't yet support creation of documents via Applescript. You have to use GUI scripting. The new version of Numbers is supposed to be out Jan 6, 2011 and (hopefully) will fix its severely limited Applescript support.