XcodeTemplate-SketchPlugin icon indicating copy to clipboard operation
XcodeTemplate-SketchPlugin copied to clipboard

Question: How to call Cocoascript functions from Controller.m

Open jkusachi opened this issue 7 years ago • 2 comments

Firstly, thank you and great work for this plugin, it works very well!

I had a question regarding calling Cocoascript functions from for example,User Subclasses/DemoPanelController.m

the cocoascript piece im looking to write is:

id fill = selection.style().fills().firstObject();
fill.setFillType(4);
fill.setImage(MSImageData.alloc().initWithImage_convertColorSpace(image, false));
fill.setPatternFillType(1);

because im in the .m file, i assume i'll have to convert this to native Objective-C? or is there a way to call a cocoascript function from within the Objective-C method?

Thank you! keep up the good work!

jkusachi avatar Jul 19 '17 17:07 jkusachi

The objective-c equivalent of this code looks more or less like this

id fill = [[[selection style] fills] firstObject];
[fill setFillType: 4];
[fill setImage:[[MSImageData alloc] initWithImage:image convertColorSpace:NO]];
[fill setPatternFillType: 1];

eldos-dl avatar Jul 25 '17 09:07 eldos-dl

thank you @eldos-dl

I'm an Objective-C newbie, so I was curious how to get around this error

For your line id fill = [[[selection style] fills] firstObject];

I get a Build Error:

Bad receiver type 'NSPersonNameComponentsFormatterStyle' (aka 'enum NSPersonNameComponentsFormatterStyle')`

Multiple methods named 'style' found with mismatched result, parameter type or attributes

I think this is because selection is of unknown type (MSShapeGroup) , and because I haven't importedMSShapeGroup, the compiler is complaining because it sees multiple style methods and doesn't know which class to use. I need to add the specific type (MSShapeGroup), but I don't have access to the library :/ hmmmm.....

Is there any way around this? I can't just import the Sketch library, as thats private, but I need to access certain methods at runtime.

Stuck at this point....

Thank you!

jkusachi avatar Jul 31 '17 17:07 jkusachi