2

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.

Phil Sinatra
  • 419
  • 3
  • 11
  • Not really an answer (because I'm searching for a checkbox as well), but I found the method `- (id)askForUserInput:(id)arg1 ofType:(long long)arg2 initialValue:(id)arg3;`. I discovered that entering 0, 1 or 2 for `arg2` gives different input controls in the alert. I tried going all the way to 10, but no checkbox sadly... – Gerald Eersteling Dec 29 '16 at 15:31

2 Answers2

3

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
0

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.

Gerald Eersteling
  • 1,244
  • 14
  • 28