majo0od Where on earth did you get the function .parentGroup() from?
I do as you do: look at what others are doing.
parentGroup() in particular was pointed out to me by a fellow traveler. I also rely heavily on the Sketch Headers (which you'll find in the resources link mathieudutour provided).
There are several layers of confusion that I have yet to deal with myself. Especially since I only occasionally play with Sketch plugin development. (The ball moves and I forget things in the pauses.)
Anyway, from one beginner to another, these are some things to keep in mind:
- There are two API:s:
- The ”unofficial” Sketch Headers (even if the Sketch devs do help us use it in a most awesome way). Most stuff you find others doing will be using this.
- Cocoascript is used for two things:
- Bridging to give you access to the Cocoa framework powering Sketch and macos. It's why we can use the ”unofficial” API.
What I do is that I have downloaded the Sketch Headers and have added the folder with all those files to the project in my IDE. (I use Visual Studio Code, something I can truly recommend.) So then I can do a project search for things, by guessing what the Sketch devs might have named it. That's how I found
insertLayers_afterLayer. I found it in
- (void)insertLayers:(id)arg1 afterLayer:(id)arg2;
I don't know how familiar you are with Objective-C, but the starting dash means it is an instance method (a starting plus means it is a static/class method). With that I guessed that
parentGroup() would be something implementing the interface found in
_MSLayerGroup.h which turned out to be a correct guess. The header files also doesn't reveal the types of he arguments or return values. (
id just means it is some subclass of
NSObject, which isn't very precise But most often it is possible to guess either directly, or by looking at the errors logged when you guess wrongly.
Another tips I can give is to use
skpm log -f to tail the logging from your plugin. I use it in the integrated terminal in Code which gives me a pretty tight feedback loop. As I mentioned above, inspecting the error logging is a path to learning about the API.