简体   繁体   中英

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

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM