-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFrom Slice.sketchplugin
73 lines (58 loc) · 2.13 KB
/
From Slice.sketchplugin
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
// Sketch Plugin: Export to ICNS From Slice (shift command a)
// Source: https://github.com/solicomo/export-to-icns
// Version: 0.1.1
// Author: Soli
@import 'common.jst'
(function() {
var iconPath = save_path()
var tmpPath = NSTemporaryDirectory()
var guid = [[NSProcessInfo processInfo] globallyUniqueString]
var iconsetPath = [tmpPath stringByAppendingPathComponent: guid + @".iconset"]
var pngPath = [tmpPath stringByAppendingPathComponent: guid + @".png"]
var fileManager = [NSFileManager defaultManager]
[fileManager createDirectoryAtPath:iconsetPath withIntermediateDirectories:true attributes:nil error:nil]
var slices = [[doc currentPage] exportableLayers]
if (!slices || [slices count] < 1) {
[doc showMessage:"Failed: No exportable layer found."]
return false
}
var slice = [slices firstObject]
var sliceCount = [slices count]
for (var i=0; i < sliceCount; i++) {
var s = [slices objectAtIndex:i]
if ([s isSelected]) {
slice = s
break
}
}
if (!slice) {
[doc showMessage:"Failed: No slice found."]
return false
}
[doc saveArtboardOrSlice:slice toFile:pngPath]
var pngSizes = [NSDictionary dictionaryWithObjectsAndKeys:
@" 16 16 ", @"icon_16x16.png",
@" 32 32 ", @"[email protected]",
@" 32 32 ", @"icon_32x32.png",
@" 64 64 ", @"[email protected]",
@" 128 128 ", @"icon_128x128.png",
@" 256 256 ", @"[email protected]",
@" 256 256 ", @"icon_256x256.png",
@" 512 512 ", @"[email protected]",
@" 512 512 ", @"icon_512x512.png",
@" 1024 1024 ", @"[email protected]",
nil]
var enumerator = [pngSizes keyEnumerator]
var png = nil
while (png = [enumerator nextObject]) {
var convertTask = [[NSTask alloc] init]
var convertCmd = "sips -z" + [pngSizes valueForKey:png] + pngPath + " -o " + [iconsetPath stringByAppendingPathComponent:png]
[convertTask setLaunchPath:"/bin/bash"]
[convertTask setArguments:["-c", convertCmd]]
[convertTask launch]
[convertTask waitUntilExit]
}
convert_iconset_to_icns(iconsetPath, iconPath)
[fileManager removeItemAtPath:iconsetPath error:nil]
[fileManager removeItemAtPath:pngPath error:nil]
})();