forked from tadija/AEIconizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAEIconizer.sketchplugin
More file actions
119 lines (99 loc) · 3.84 KB
/
AEIconizer.sketchplugin
File metadata and controls
119 lines (99 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Sketch Plugin: AEIconizer (ctrl shift i)
// Source: github.com/tadija/AEIconizer
// Version: 1.0
// iOS icon sizes
var iconSizes = [NSArray arrayWithObjects: 512, 76, 60, 40, 29, nil];
// do stuff
if (originalIcon = handleSelection()) {
if ([[originalIcon frame] width] == [[originalIcon frame] height]) {
[originalIcon setName: "Icon-Original"];
removeExistingIcons();
iconize(originalIcon);
[[doc currentView] centerLayersInCanvas];
} else {
[doc showMessage:"Oops, icon artboard must have same width and height"];
}
}
function handleSelection() {
var iconArtboard;
if([selection count] == 0) {
if ([[[doc currentPage] artboards] count] == 1) {
iconArtboard = [[[doc currentPage] artboards] firstObject];
} else {
[doc showMessage:"Oops, you have to select something"];
}
} else {
var currentSelection = selection[0];
if (currentSelection.className() == "MSArtboardGroup") {
iconArtboard = currentSelection;
} else {
iconArtboard = [currentSelection parentArtboard];
if (!iconArtboard) {
[doc showMessage:"Oops, selection has to be inside artboard"];
}
}
[currentSelection setIsSelected:false];
}
return iconArtboard
}
function removeExistingIcons() {
var artboards = [[doc currentPage] artboards];
var loop = [artboards objectEnumerator];
while (artboard = [loop nextObject]) {
var name = [artboard name];
var artboardNames = [NSArray arrayWithObjects:"Icon-29", "Icon-29@2x", "Icon-29@3x", "Icon-40", "Icon-40@2x", "Icon-40@3x", "Icon-60", "Icon-60@2x", "Icon-60@3x", "Icon-76", "Icon-76@2x", "Icon-76@3x", "iTunesArtwork-512", "iTunesArtwork-512@2x" , nil];
if ([artboardNames containsObject: name]) {
[[doc currentPage] removeLayer:artboard];
}
}
}
function iconize(originalIcon) {
var originalSize = [[originalIcon frame] width];
var originalX = [[originalIcon absoluteRect] rulerX];
var originalY = [[originalIcon absoluteRect] rulerY];
var spacing = 40;
var iconsRowHeight = 0;
var loop = [iconSizes objectEnumerator];
while (newSize = [loop nextObject]) {
if (newSize >= 512) {
var iconName = "iTunesArtwork-" + newSize;
var newX = originalX;
var newY = originalY + originalSize + spacing;
// iTunesArtwork-512@2x
var artwork2x = scaleIcon(originalIcon, (newSize * 2), iconName + "@2x");
[[artwork2x absoluteRect] setRulerX:newX];
[[artwork2x absoluteRect] setRulerY:newY];
// iTunesArtwork-512
var artwork1x = scaleIcon(originalIcon, newSize, iconName);
[[artwork1x absoluteRect] setRulerX:newX + (newSize * 2) + spacing];
[[artwork1x absoluteRect] setRulerY:newY];
} else {
var iconName = "Icon-" + newSize;
var newX = originalX + (1024 + 512 + (2 * spacing));
var newY = originalY + originalSize + iconsRowHeight + spacing;
// Icon-Size@3x
var icon3x = scaleIcon(originalIcon, newSize * 3, iconName + "@3x");
[[icon3x absoluteRect] setRulerX:newX];
[[icon3x absoluteRect] setRulerY:newY];
// Icon-Size@2x
var icon2x = scaleIcon(originalIcon, newSize * 2, iconName + "@2x");
[[icon2x absoluteRect] setRulerX:newX + (newSize * 3) + spacing];
[[icon2x absoluteRect] setRulerY:newY];
// Icon-Size
var icon1x = scaleIcon(originalIcon, newSize, iconName);
[[icon1x absoluteRect] setRulerX:newX + (newSize * 3) + (newSize * 2) + (2 * spacing)];
[[icon1x absoluteRect] setRulerY:newY];
// create new row
iconsRowHeight += (newSize * 3) + spacing;
}
}
}
function scaleIcon(originalIcon, newSize, newName) {
var newIcon = [originalIcon duplicate];
var originalSize = [[originalIcon frame] width];
var ratio = newSize / originalSize;
[newIcon multiplyBy:ratio];
[newIcon setName: newName];
[newIcon select:true byExpandingSelection:true];
return newIcon;
}