Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 3bd2a78

Browse files
committed
Port Align commands to JS API. Closes #81
1 parent 44f3e49 commit 3bd2a78

File tree

5 files changed

+64
-61
lines changed

5 files changed

+64
-61
lines changed

Sketch Commands.sketchplugin/Contents/Sketch/Align/Space Horizontal.cocoascript

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Distributes the selected elements horizontally, with the same distance beetween them.
2+
var onRun = function(context) {
3+
var sketch = require('sketch')
4+
var Document = sketch.Document
5+
var UI = sketch.UI
6+
var document = Document.getSelectedDocument()
7+
var selection = document.selectedLayers
8+
9+
function sort_by_position(a,b){
10+
return a.frame.x - b.frame.x
11+
}
12+
13+
UI.getInputFromUser(
14+
"Spacing",
15+
{
16+
initialValue: 10,
17+
},
18+
(err, value) => {
19+
if (err) {
20+
return
21+
}
22+
var sorted_selection = selection.layers.sort(sort_by_position)
23+
var first_element = sorted_selection[0]
24+
var left_position = first_element.frame.x
25+
sorted_selection.forEach(layer => {
26+
layer.frame.x = left_position
27+
left_position = layer.frame.x + layer.frame.width + parseInt(value)
28+
})
29+
}
30+
)
31+
}

Sketch Commands.sketchplugin/Contents/Sketch/Align/Space Vertical.cocoascript

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Distributes the selected elements vertically, with the same distance beetween them.
2+
var onRun = function(context) {
3+
var sketch = require('sketch')
4+
var Document = sketch.Document
5+
var UI = sketch.UI
6+
var document = Document.getSelectedDocument()
7+
var selection = document.selectedLayers
8+
9+
function sort_by_position(a,b){
10+
return a.frame.x - b.frame.x
11+
}
12+
13+
UI.getInputFromUser(
14+
"Spacing",
15+
{
16+
initialValue: 10,
17+
},
18+
(err, value) => {
19+
if (err) {
20+
return
21+
}
22+
var sorted_selection = selection.layers.sort(sort_by_position)
23+
var first_element = sorted_selection[0]
24+
var top_position = first_element.frame.y
25+
sorted_selection.forEach(layer => {
26+
layer.frame.y = top_position
27+
top_position = layer.frame.y + layer.frame.height + parseInt(value)
28+
})
29+
}
30+
)
31+
}

Sketch Commands.sketchplugin/Contents/Sketch/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
"name": "Space Horizontal",
1616
"identifier": "spacehorizontal",
1717
"shortcut": "",
18-
"script": "Align/Space Horizontal.cocoascript"
18+
"script": "Align/Space Horizontal.js"
1919
},
2020
{
2121
"name": "Space Vertical",
2222
"identifier": "spacevertical",
2323
"shortcut": "",
24-
"script": "Align/Space Vertical.cocoascript"
24+
"script": "Align/Space Vertical.js"
2525
},
2626
{
2727
"name": "Alpha…",

0 commit comments

Comments
 (0)