I am not pro in javascript but I want to implement shadow on the object from my predefine shadows but the code is not working:
var selection = context.selection;
for (var i=0; i < selection.count(); i++){
var layer = selection.objectAtIndex(i)
if (layer.isEmpty) UI.message('No layers selected!');
else {
var sha_0 = [];
var sha_1 = [
color: '#00000024',
x: 0,
y: 1,
blur: 1,
spread: 0
]
var sha_2 = [
color: '#00036024',
x: 0,
y: 1,
blur: 1,
spread: 0
]
var options = ['0dp', '1dp', '2dp'];
var selectDialog = sketch.UI.getSelectionFromUser("Please select shadow depth:", options);
if (selected == 0) {
layer.forEach(function (e) {
e.style.shadows = sha_0;
});
} else if (selected == 1) {
layer.forEach(function (e) {
e.style.shadows = sha_1;
});
} else if (selected == 2) {
layer.forEach(function (e) {
e.style.shadows = sha_2;
});
}
}
}
The methods you need to call belonging to the UI library are asynchronous. They rely on the user performing some action before the code can continue. In Javascript this requires either a callback function or the use of
async/awaitwith Promises. The specific method you're using:sketch.UI.getSelectionFromUseris depricated in favor of the newUI.getInputFromUsermethod as you can find here.For example:
Thus, your code will look something like this:
Your code could possibly be simplified more, but that should get you moving again.