开发者

How do I create a Numbers spreadsheet using objective-c?

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 adde开发者_C百科d 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.


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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜