-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathplugin.js
More file actions
156 lines (141 loc) · 5.44 KB
/
Copy pathplugin.js
File metadata and controls
156 lines (141 loc) · 5.44 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
function showMessage(txt, timeout = 5){
NSApplication.sharedApplication().orderedDocuments().firstObject().displayMessage_timeout(txt, timeout)
}
function runImageOptim(context, files, hidden) {
if (!files.length) return;
const workspace = NSWorkspace.sharedWorkspace();
const bundleIdentifier = "net.pornel.ImageOptim";
const appURL = workspace.URLForApplicationWithBundleIdentifier(bundleIdentifier);
if (!appURL) {
showMessage("ImageOptim not installed", 10);
workspace.openURL(NSURL.URLWithString("https://imageoptim.com/mac?sketch"));
return;
}
let flags = NSWorkspaceLaunchWithoutAddingToRecents | NSWorkspaceLaunchAsync;
if (hidden) {
flags = flags | NSWorkspaceLaunchWithoutActivation | NSWorkspaceLaunchAndHide;
}
workspace.openURLs_withAppBundleIdentifier_options_additionalEventParamDescriptor_launchIdentifiers_(
files, bundleIdentifier, flags, null, null);
}
function getURLsToCompress(exportedAssets){
const urlsToCompress = []
for(let i=0; i < exportedAssets.count(); i++) {
const asset = exportedAssets.objectAtIndex(i)
if (NSFileManager.defaultManager().fileExistsAtPath(asset.path)) {
urlsToCompress.push(NSURL.fileURLWithPath(asset.path));
}
}
return urlsToCompress
}
function openFileDialog(path){
const openDlg = NSOpenPanel.openPanel()
openDlg.setTitle('Export & Optimize All Assets In…')
openDlg.setCanChooseFiles(false)
openDlg.setCanChooseDirectories(true)
openDlg.allowsMultipleSelection = false
openDlg.setCanCreateDirectories(true)
openDlg.setPrompt('Export')
if (path) {
openDlg.setDirectoryURL(path)
}
const buttonClicked = openDlg.runModal()
if (buttonClicked == NSOKButton) {
return openDlg.URLs().firstObject().path()
}
return null
}
function exportAndCompress(context){
const potentialExports = context.document.allExportableLayers()
if (potentialExports.count() > 0) {
showMessage('Exporting assets to ImageOptim')
const exportFolder = openFileDialog()
if (exportFolder) {
// TODO: If there's any exportable layer selected, only export those. Otherwise, export everything under the sun
const exports = NSMutableArray.alloc().init()
for(let exportCount=0; exportCount < potentialExports.count(); exportCount++) {
const exportableLayer = potentialExports.objectAtIndex(exportCount)
const requests = MSExportRequest.exportRequestsFromExportableLayer(exportableLayer)
if (requests.count() > 0) {
for (let j=0; j < requests.count(); j++) {
const request = requests.objectAtIndex(j)
const path = NSString.pathWithComponents([exportFolder, request.name() + '.' + request.format()])
exports.addObject({ request: request, path: path })
}
}
}
// First we'll need to actually export the assets
for (let k=0; k < exports.count(); k++) {
const currentExport = exports.objectAtIndex(k)
let render
if (currentExport.request.format() == "svg") {
render = MSExportRendererWithSVGSupport.exporterForRequest_colorSpace(currentExport.request, NSColorSpace.sRGBColorSpace())
} else {
render = MSExporter.exporterForRequest_colorSpace(currentExport.request, NSColorSpace.sRGBColorSpace())
}
render.data().writeToFile_atomically(currentExport.path, true)
}
// Filter out items that can't be optimized
const optimizableExports = filteredExports(exports);
// …and then we'll be able to compress them :)
const urlsToCompress = getURLsToCompress(optimizableExports);
if (urlsToCompress.length > 0) {
runImageOptim(context, urlsToCompress, false);
} else {
coscript.setShouldKeepAround(false)
}
}
} else {
showMessage('No exportable layers in the document')
}
}
function filteredExports(exports) {
var optimizableExports = NSMutableArray.new();
for (var i = 0; i < exports.count(); i++) {
if (!exports[i].request.format().isEqualToString("pdf")
&& !exports[i].request.format().isEqualToString("webp")
&& !exports[i].request.format().isEqualToString("eps")
&& !exports[i].request.format().isEqualToString("svg")) {
optimizableExports.addObject(exports[i]);
}
}
return optimizableExports;
}
function compressAutomatically(context){
const exports = context.actionContext.exports;
// Filter out items that can't be optimized
const optimizableExports = filteredExports(exports);
const urlsToCompress = getURLsToCompress(optimizableExports)
runImageOptim(context, urlsToCompress, true)
}
export const SketchPlugin = {
name: "ImageOptim",
description: "Optimize exported images with ImageOptim",
author: "Kornel Lesiński & Ale Muñoz",
authorEmail: "kornel@imageoptim.com",
version: "1.0.0",
identifier: "com.imageoptim.sketch",
homepage: "https://imageoptim.com",
compatibleVersion: 3.8,
commands: {
imageOptim: {
name: 'Export and Optimize All Assets…',
handlers: {
run : "___imageOptim_run_handler_",
actions: {
"ExportSlices": "___imageOptim_run_handler_",
"Export": "___imageOptim_run_handler_"
}
},
run(context) {
if (context.actionContext) {
// Plugin was triggered automatically
compressAutomatically(context)
} else {
// Plugin was triggered from the menu, so crush with all the power we've got : )
exportAndCompress(context)
}
}
}
}
}