forked from bomberstudios/sketch-framer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Export to Framer.sketchplugin
646 lines (567 loc) · 22.7 KB
/
Export to Framer.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
// Sketch Framer (ctrl alt command f)
/* Import the variables and complain if they are undefined */
#import 'sketch-framer-config.js'
/*
This is a port to JSTalk of <https://github.com/leighmcculloch/AppSandboxFileAccess>
Here's the original license for AppSandboxFileAccess:
## License
Copyright (c) 2013, Leigh McCulloch All rights reserved.
BSD-2-Clause License: http://opensource.org/licenses/BSD-2-Clause
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var AppSandboxFileAccessPersist = {
keyForBookmarkDataForURL: function(url) {
log("AppSandboxFileAccessPersist.keyForBookmarkDataForURL("+url+")")
var urlStr = [url absoluteString];
log("> " + [NSString stringWithFormat:@"bd_%1$@", urlStr])
return [NSString stringWithFormat:@"bd_%1$@", urlStr];
},
bookmarkDataForURL: function(url) {
log("AppSandboxFileAccessPersist.bookmarkDataForURL('"+ url +"')")
var defaults = [NSUserDefaults standardUserDefaults];
// loop through the bookmarks one path at a time down the URL
var subUrl = url;
while ([subUrl path].length() > 1) { // give up when only '/' is left in the path
var key = AppSandboxFileAccessPersist.keyForBookmarkDataForURL(subUrl);
var bookmark = [defaults dataForKey:key];
if (bookmark) { // if a bookmark is found, return it
return bookmark;
}
subUrl = [subUrl URLByDeletingLastPathComponent];
}
// no bookmarks for the URL, or parent to the URL were found
return nil;
},
setBookmarkData: function(data, url) {
log("AppSandboxFileAccessPersist.setBookmarkData")
log("data: " + data)
log("URL: " + url)
var defaults = [NSUserDefaults standardUserDefaults];
var key = AppSandboxFileAccessPersist.keyForBookmarkDataForURL(url);
[defaults setObject:data forKey:key];
}
}
var AppSandboxFileAccess = {
init: function(opts){
this.message = opts.message || "Please authorize Sketch to write to this folder. You will only need to do this once."
this.prompt = opts.prompt || "Authorize",
this.title = opts.title || "Sketch Authorization"
return this;
},
askPermissionForUrl: function(url) {
log("AppSandboxFileAccess.askPermissionForUrl("+url+")")
// this url will be the url allowed, it might be a parent url of the url passed in
var allowedUrl;
// create delegate that will limit which files in the open panel can be selected, to ensure only a folder
// or file giving permission to the file requested can be selected
// AppSandboxFileAccessOpenSavePanelDelegate *openPanelDelegate = [[AppSandboxFileAccessOpenSavePanelDelegate alloc] initWithFileURL:url];
// check that the url exists, if it doesn't, find the parent path of the url that does exist and ask permission for that
var fileManager = [NSFileManager defaultManager];
var path = [url path];
while (path.length() > 1) { // give up when only '/' is left in the path or if we get to a path that exists
if ([fileManager fileExistsAtPath:path]) {
break;
}
path = [path stringByDeletingLastPathComponent];
}
log("Looks like we have a winner: " + path)
url = [NSURL fileURLWithPath:path];
// display the open panel
var openPanel = [NSOpenPanel openPanel];
[openPanel setMessage:this.message];
[openPanel setPrompt:this.prompt];
[openPanel setTitle:this.title];
// [openPanel setDelegate:openPanelDelegate];
[openPanel setCanCreateDirectories:NO];
[openPanel setCanChooseFiles:YES];
[openPanel setCanChooseDirectories:YES];
[openPanel setAllowsMultipleSelection:NO];
[openPanel setShowsHiddenFiles:NO];
[openPanel setExtensionHidden:NO];
[openPanel setDirectoryURL:url];
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
var openPanelButtonPressed = [openPanel runModal];
if (openPanelButtonPressed == NSFileHandlingPanelOKButton) {
allowedUrl = [openPanel URL];
}
return allowedUrl;
},
persistPermissionPath: function(path) {
this.persistPermissionURL([NSURL fileURLWithPath:path]);
},
persistPermissionURL: function(url) {
log("AppSandboxFileAccess.persistPermissionURL("+url+")")
// store the sandbox permissions
url = [[url URLByStandardizingPath] URLByResolvingSymlinksInPath]
var bookmarkData = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
includingResourceValuesForKeys:nil
relativeToURL:nil
error:null];
if (bookmarkData) {
AppSandboxFileAccessPersist.setBookmarkData(bookmarkData, url);
}
},
accessFilePath_withBlock_persistPermission: function(path, block, persist) {
log("AppSandboxFileAccess.accessFilePath_withBlock_persistPermission")
log("path: " + path)
return AppSandboxFileAccess.accessFileURL_withBlock_persistPermission([NSURL fileURLWithPath:path], block, persist);
},
accessFileURL_withBlock_persistPermission: function(fileUrl, block, persist) {
log("AppSandboxFileAccess.accessFileURL_withBlock_persistPermission")
log("fileUrl: " + fileUrl)
log("block: " + block)
log("persist: " + persist)
var allowedUrl = false;
// standardize the file url and remove any symlinks so that the url we lookup in bookmark data would match a url given by the askPermissionForUrl method
var fileUrl = [[fileUrl URLByStandardizingPath] URLByResolvingSymlinksInPath];
// lookup bookmark data for this url, this will automatically load bookmark data for a parent path if we have it
var bookmarkData = AppSandboxFileAccessPersist.bookmarkDataForURL(fileUrl);
if (bookmarkData) {
log("Bookmark data found")
// resolve the bookmark data into an NSURL object that will allow us to use the file
var bookmarkDataIsStale;
allowedUrl = [NSURL URLByResolvingBookmarkData:bookmarkData options:NSURLBookmarkResolutionWithSecurityScope|NSURLBookmarkResolutionWithoutUI relativeToURL:nil bookmarkDataIsStale:bookmarkDataIsStale error:null];
// if the bookmark data is stale, we'll create new bookmark data further down
if (bookmarkDataIsStale) {
bookmarkData = nil;
}
} else {
log("No bookmark data found")
}
// if allowed url is nil, we need to ask the user for permission
if (!allowedUrl) {
allowedUrl = AppSandboxFileAccess.askPermissionForUrl(fileUrl);
if (!allowedUrl) {
// if the user did not give permission, exit out here
return false;
}
}
// if we have no bookmark data, we need to create it, this may be because our bookmark data was stale, or this is the first time being given permission
if (persist && !bookmarkData) {
AppSandboxFileAccess.persistPermissionURL(allowedUrl);
}
// execute the block with the file access permissions
try {
[allowedUrl startAccessingSecurityScopedResource];
block();
} finally {
[allowedUrl stopAccessingSecurityScopedResource];
}
return true;
}
}
function in_sandbox(){
var environ = [[NSProcessInfo processInfo] environment];
return (nil != [environ objectForKey:@"APP_SANDBOX_CONTAINER_ID"]);
}
var sandboxAccess = AppSandboxFileAccess.init({
message: "Please authorize Sketch to write to this folder. You will only need to do this once per folder.",
prompt: "Authorize",
title: "Sketch Authorization"
})
/* Configuration */
var framerjs_url = "https://raw.github.com/koenbok/Framer/master/build/framer.js";
function should_become_view(layer) {
return is_group(layer) || [layer name].slice(-1) == '+';
}
function is_group(layer) {
return [layer isMemberOfClass:[MSLayerGroup class]] || [layer isMemberOfClass:[MSArtboardGroup class]]
}
function should_ignore_layer(layer) {
return [layer name].slice(-1) == '-' || [layer className] == "MSPage";
}
function sanitize_filename(name){
return name.replace(/(\s|:|\/)/g ,"_").replace(/__/g,"_").replace("*","").replace("+","").replace("&","");
}
function has_art(layer) {
// return true;
if(is_group(layer) && !should_flatten_layer(layer)) {
var has_art = false;
var sublayers = [layer layers];
for (var sub=([sublayers length] - 1); sub >= 0; sub--) {
var sublayer = sublayers[sub];
if(!should_ignore_layer(sublayer) && !should_become_view(sublayer)) {
has_art = true;
}
}
return has_art;
} else {
return true;
}
}
function should_flatten_layer(layer) {
var name = [layer name];
if(name.slice(-1) == "*") {
return true;
} else {
return false;
}
}
function log_depth(message, depth) {
var padding = "";
for(var i=0; i<depth; i++) {
padding += ">"
}
log(padding + " " + message);
}
function save_file_from_string(filename,the_string) {
log("save_file_from_string("+filename+")")
var path = [@"" stringByAppendingString:filename],
str = [@"" stringByAppendingString:the_string];
if (in_sandbox()) {
sandboxAccess.accessFilePath_withBlock_persistPermission(filename, function(){
[str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:null];
}, true)
} else {
[str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:null];
}
}
function mask_bounds(layer) {
var sublayers = [layer layers];
for (var sub=([sublayers length] - 1); sub >= 0; sub--) {
var current = sublayers[sub];
if([current name].toLowerCase().indexOf('maskframe') != -1) {
return metadata_for(current);
}
}
return null;
}
function calculate_real_position_for(layer) {
var gkrect = [GKRect rectWithRect:[layer rectByAccountingForStyleSize:[[layer absoluteRect] rect]]],
absrect = [layer absoluteRect];
var rulerDeltaX = [absrect rulerX] - [absrect x],
rulerDeltaY = [absrect rulerY] - [absrect y],
GKRectRulerX = [gkrect x] + rulerDeltaX,
GKRectRulerY = [gkrect y] + rulerDeltaY;
return {
x: Math.round(GKRectRulerX),
y: Math.round(GKRectRulerY)
}
}
function metadata_for(layer) {
log("metadata_for("+layer+")")
var frame = [layer frame],
gkrect = [GKRect rectWithRect:[layer rectByAccountingForStyleSize:[[layer absoluteRect] rect]]],
absrect = [layer absoluteRect],
position = calculate_real_position_for(layer),
x,y,w,h,r
if ([layer className] == "MSArtboardGroup") {
// FIXME: This is probably still wrong, test with different artboard positions
x = [[layer absoluteRect] rulerX],
y = [[layer absoluteRect] rulerY],
w = [frame width],
h = [frame height],
r = {
x: x,
y: y,
width: w,
height: h
}
} else {
x = position.x,
y = position.y,
w = [gkrect width],
h = [gkrect height],
r = {
x: x,
y: y,
width: w,
height: h
}
}
log(JSON.stringify(r))
return r
}
function extract_metadata_from(layer) {
log("extract_metadata_from("+layer+")")
var layerFrame = metadata_for(layer)
log(layerFrame)
var maskFrame = mask_bounds(layer)
log("maskFrame: " + maskFrame)
if(maskFrame){
log(JSON.stringify(maskFrame))
}
var metadata = {
name: sanitize_filename([layer name]),
layerFrame: layerFrame
// id: [layer objectID]
}
if (maskFrame) {
metadata.maskFrame = maskFrame
}
if(has_art(layer)) {
metadata.image = {
path: "images/" + sanitize_filename([layer name]) + ".png",
frame: layerFrame
};
metadata.imageType = "png";
// TODO: Find out how the modification hash is calculated in Framer.app
// metadata.modification = new Date();
}
// if it was invisible, mark this in the metadata as well
if ([layer name].slice(-1) == '&') {
metadata.visible = false
}
return metadata;
}
function make_folder(path){
if (in_sandbox()) {
sandboxAccess.accessFilePath_withBlock_persistPermission(path, function(){
[file_manager createDirectoryAtPath:path withIntermediateDirectories:true attributes:nil error:nil];
},true)
} else {
[file_manager createDirectoryAtPath:path withIntermediateDirectories:true attributes:nil error:nil];
}
}
function make_folders(){
// Create folders
var folders = [target_folder,images_folder,framer_folder]
for (var i = 0; i < folders.length; i++) {
make_folder(folders[i])
}
}
function process_all_layers(){
var all_layers = [[doc currentPage] layers]
for (var i=0; i < [all_layers length]; i++) {
process_layer(all_layers[i],project_metadata, 0);
}
}
function process_layer(layer,metadata_container,depth) {
log("Processing layer: " + layer)
log("Metadata Container: " + metadata_container)
depth++
if(should_ignore_layer(layer)) {
log_depth("Ignoring <" + [layer name] + "> of type <" + [layer className] + ">", depth)
return;
}
if(should_become_view(layer)){
log_depth("Processing <" + [layer name] + "> of type <" + [layer className] + ">", depth)
if (![layer isVisible]) {
log_depth("Temporarily showing " + layer,depth)
[layer setName:[layer name] + "&"]
}
[layer setIsVisible:true]
// Get layer data
var layer_data = extract_metadata_from(layer)
layer_data.children = []
// Export image if layer has no subgroups
if (!should_flatten_layer(layer) && is_group(layer)) {
var sublayers = [layer layers];
// Apparently, Sketch returns child layers in reverse order (back to front),
// so we'll loop backwards through them:
for (var sub=([sublayers length] - 1); sub >= 0; sub--) {
var current = sublayers[sub];
process_layer(current,layer_data.children,depth+1);
}
}
if ([layer className] == "MSArtboardGroup") {
export_artboard(layer)
} else {
if ([NSApp applicationVersion] >= 3) {
export_isolated_layer_v3(layer,images_folder + "/" + sanitize_filename([layer name]) + ".png")
} else {
export_isolated_layer_v2(layer,images_folder + "/" + sanitize_filename([layer name]) + ".png")
}
}
// FIXME: why is this here? Still needed?
// if(should_flatten_layer(layer)){
// duplicate_and_export(layer)
// }
metadata_container.push(layer_data);
if ([layer name].slice(-1) == '&') {
log("Restore visibility for " + [layer name])
[layer setName:[layer name].slice(0, -1)];
[layer setIsVisible:false]
// Small hack: this forces a redraw
[layer resetPointsBasedOnUserInteraction]
}
log([layer isVisible])
}
}
function create_files(metadata){
log("create_files("+metadata+")")
var file_path = framer_folder + "/views." + document_name + ".js";
log(JSON.stringify(metadata))
var file_contents = "window.FramerPS = window.FramerPS || {};\nwindow.FramerPS['" + document_name +"'] = " + JSON.stringify(metadata,null,2)
save_file_from_string(file_path,file_contents);
log(framer_folder)
try {
// Save JS files from templates:
save_file_from_string(framer_folder + "/framerps.js", FramerPSJSContents);
if(![file_manager fileExistsAtPath:(target_folder + "/" + FramerScriptFileName)]) {
save_file_from_string(target_folder + "/" + FramerScriptFileName, FramerScriptFileContents);
}
} catch(e) {
log(e)
}
// Get JS files from Github
var task = [[NSTask alloc] init],
argsArray = [NSArray arrayWithObjects:"-O", framerjs_url, nil];
[task setCurrentDirectoryPath:framer_folder];
[task setLaunchPath:"/usr/bin/curl"];
[task setArguments:argsArray];
[task launch];
// Get library files if one if configured and isn't yet downloaded
if(FramerLibraryUrl) {
if(![file_manager fileExistsAtPath:(framer_folder + "/" + FramerLibraryFileName)]) {
var task2 = [[NSTask alloc] init],
argsArray = [NSArray arrayWithObjects:"-O", FramerLibraryUrl, nil];
[task2 setCurrentDirectoryPath:framer_folder];
[task2 setLaunchPath:"/usr/bin/curl"];
[task2 setArguments:argsArray];
[task2 launch];
}
}
// Create HTML and open in default browser if it's the first time we're exporting
if(![file_manager fileExistsAtPath:(target_folder + "/index.html")]) {
save_file_from_string(target_folder + "/index.html", FramerIndexFileContents.replace("{{ views }}",'<script src="framer/views.' + document_name + '.js"></script>'));
var open_task = [[NSTask alloc] init],
open_task_args = [NSArray arrayWithObjects:(target_folder + "/index.html"), nil];
[open_task setCurrentDirectoryPath:framer_folder];
[open_task setLaunchPath:"/usr/bin/open"];
[open_task setArguments:open_task_args];
[open_task launch];
}
}
function export_isolated_layer_v3(layer,filename){
// Select layer, so we can be sure there's a current artboard
[layer select:true byExpandingSelection:false]
// (if there is, indeed, an artboard, that is :)
if([[[doc currentPage] artboards] count] > 0){
log("Here be artboards")
var art = [[doc currentPage] currentArtboard],
did_disable_background = false
if([art includeBackgroundColorInExport]){
log("Artboard has a background color set to export, so we'll momentarily disable it")
[art setIncludeBackgroundColorInExport:false]
did_disable_background = true
}
}
var filename = [@"" stringByAppendingString:filename]
var slice = [[MSSliceMaker slicesFromExportableLayer:layer] firstObject]
slice.page = [doc currentPage]
var imageData = [MSSliceExporter dataForSlice:slice format:@"png"]
if (in_sandbox()) {
sandboxAccess.accessFilePath_withBlock_persistPermission([filename stringByDeletingLastPathComponent], function(){
[imageData writeToFile:filename atomically:YES]
}, true)
} else {
[imageData writeToFile:filename atomically:YES]
}
if (did_disable_background) {
[art setIncludeBackgroundColorInExport:true]
}
}
function export_isolated_layer_v2(layer,filename){
var filename = [@"" stringByAppendingString:filename]
var copy = [layer duplicate]
[[copy frame] setX:-100000]
var rect = [copy rectByAccountingForStyleSize:[[copy absoluteRect] rect]],
slice = [GKRect rectWithRect:rect]
if (in_sandbox()) {
sandboxAccess.accessFilePath_withBlock_persistPermission([filename stringByDeletingLastPathComponent], function(){
[doc saveArtboardOrSlice:slice toFile:filename];
}, true)
} else {
[doc saveArtboardOrSlice:slice toFile:filename];
}
[copy removeFromParent]
}
function export_artboard(layer){
log_depth("Exporting artboard: <" + [layer name] + ">");
var filename = images_folder + "/" + sanitize_filename([layer name]) + ".png",
hidden_sublayers = []
var artnames = [],
has_art = false;
// Remove subgroups and only leave shapes
if(is_group(layer) && !should_flatten_layer(layer)) {
var sublayers = [layer layers]
for (var sub=([sublayers length] - 1); sub >= 0; sub--) {
var sublayer = sublayers[sub];
if(should_become_view(sublayer) || should_ignore_layer(sublayer)) {
log(" REMOVING " + [sublayer name])
[sublayer setIsVisible:false]
hidden_sublayers.push(sublayer)
} else {
log(" KEEPING " + [sublayer name]);
artnames.push([sublayer name])
has_art = true
}
}
} else {
// not a group, single art
has_art = true;
}
// Export
if(!has_art) {
log("Exporting <" + [layer name] + ">, no image");
} else {
log("Exporting <" + [layer name] + "> including children art (" + artnames.join(", ") + ")");
var slice = [MSSlice sliceWithRect:[[layer absoluteRect] rect] scale:2]
if (in_sandbox()) {
sandboxAccess.accessFilePath_withBlock_persistPermission([filename stringByDeletingLastPathComponent], function(){
[doc saveArtboardOrSlice:slice toFile:filename];
}, true)
} else {
[doc saveArtboardOrSlice:slice toFile:filename];
}
}
// Restore
log("Restoring formerly visible elements: " + hidden_sublayers.length)
if(hidden_sublayers.length > 0){
for (var i = 0; i < hidden_sublayers.length; i++) {
var l = hidden_sublayers[i]
[l setIsVisible:true]
}
hidden_sublayers = []
}
}
var document_path = [[doc fileURL] path].split([doc displayName])[0],
document_name = [doc displayName].replace(".sketch",""),
target_folder = document_path + document_name,
images_folder = target_folder + "/images",
framer_folder = target_folder + "/framer",
file_manager = [NSFileManager defaultManager],
invisible_layers = [],
project_metadata = []
function main() {
log('########################################################################');
make_folders()
process_all_layers()
create_files(project_metadata)
[[doc currentPage] deselectAllLayers]
// Display message in toolbar
[doc showMessage: "Sketch Framer: Project exported to “" + target_folder + "”"];
}
/*
This plugin will export your Sketch document to a Framer project.
This command REQUIRES at least Sketch Version 2.3 (419),
as it uses an API not available in earlier versions
- Every layer group will become an ImageView, backed by the pixel and/or shape layers in that layer group.
- Views are accessible by name under the PSD object (yeah, I know...)
- Framer respects the layer hierarchy, so a sub layer group will become a subview. This way you can define your view structure in Photoshop.
- TODO: Layer groups with vector masks will become views with clipping enabled. You can optionally add "scroll" to the layer group name to make it a ScrollView.
- Layer group names should be unique, if they're not Framer will silently rename them on export.
// TODO: Check for duplicated names in exported files
// TODO: Calculate proper IDs (not sure what they're used for, but hey...)
// TODO: Calculate proper modification hash
// TODO: Embed framer.js on this file, instead of 'curl'ing it from Github
// TODO: Multiple Artboards support
*/
main();