Skip to content

Instantly share code, notes, and snippets.

@d4rekanguok
Created October 27, 2018 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d4rekanguok/c98c642a112196953d11a2c73c9de9e9 to your computer and use it in GitHub Desktop.
Save d4rekanguok/c98c642a112196953d11a2c73c9de9e9 to your computer and use it in GitHub Desktop.
Sketch script to scale layers/group individually.
const sketch = require('sketch/dom');
const UI = require('sketch/ui');
const document = sketch.getSelectedDocument();
const selection = document.selectedLayers.layers.slice();
const resize = (layer, options = {}) => {
const { scaleX, scaleY, originX, originY } = Object.assign({
scaleX: 1,
scaleY: 1,
originX: 0,
originY: 0,
}, options);
const { x, y, width, height } = layer.frame;
const newWidth = width * scaleX;
const newHeight = height * scaleY;
const newX = (width - newWidth) * originX + x;
const newY = (height - newHeight) * originY + y;
layer.frame.width = newWidth;
layer.frame.height = newHeight;
layer.frame.x = newX;
layer.frame.y = newY;
}
const originXDict = {
'left': 0,
'center': 0.5,
'right': 1,
};
const originYDict = {
'top': 0,
'center': 0.5,
'bottom': 1,
}
const originOptions = [];
const originXKeys = Object.keys(originXDict);
const originYKeys = Object.keys(originYDict);
originYKeys.forEach(keyY => {
originXKeys.forEach(keyX => {
originOptions.push(keyY + ` ` + keyX);
})
})
const stringScale = UI.getStringFromUser('Enter scale', '');
const [ _, selectedOrigin, ok ] = UI.getSelectionFromUser('Enter origin', originOptions);
if (!!stringScale && ok) {
const scale = Number.parseFloat(stringScale);
const [ keyY, keyX ] = originOptions[selectedOrigin].split(' ');
console.log(keyY, keyX);
selection
.forEach(l => resize(l, {
scaleX: scale,
scaleY: scale,
originX: originXDict[keyX],
originY: originYDict[keyY],
}))
UI.message(`Updated ${selection.length} layers.`);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment