Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 307
Austin Avatar asked Dec 07 '25 10:12

Austin


1 Answers

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.

like image 110
has Avatar answered Dec 10 '25 00:12

has



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!