How to get a checkbox input in a Sketch plugin with Cocoascript?

806 Views Asked by At

I have a Sketch plugin that generates three different files based on the user's selection within an artboard. I want to allow the user to select which of the three files they actually want to have generated via checkboxes (instead of always generating all three).

I'm looking for any reference/help with the Cocoascript function (if one exists) on how to build checkboxes in an alert message so when the plugin is trigger, the alert will pop up and offer the three options for the user to select from.

2

There are 2 best solutions below

0
On

Ok this might not be exactly what you want, but I managed to get what I want by using a selection input from the user.

Try using this snippet:

var sketch = context.api()
var inputs = ["Turn on", "Turn off"];

var gotInput = sketch.getSelectionFromUser("Turn something on?", inputs, 0);

var chosenIndex = gotInput[1]
sketch.alert(inputs[chosenIndex], "You chose");

And yeah I know, ugly as hell. But this is all I could came up with right now, I'll be happy when a real checkbox is made.

1
On

Maybe you solved it already but just in case here is how you can do it:

var dialogWindow = COSAlertWindow.new();

var checkbox = NSButton.alloc().initWithFrame(NSMakeRect(0,0,200,23))
checkbox.setButtonType(NSSwitchButton)
checkbox.setBezelStyle(0)
checkbox.setTitle("A fancy copy here")
checkbox.setState(NSOffState) // or NSOnState

dialogWindow.addAccessoryView(checkbox)

You can get the value like this:

checkbox.stringValue() // Returns 0 or 1