|
| 1 | +var onRun = function(context) { |
| 2 | + var output = 'Style, Typeface, Color, Opacity, Size, Alignment, Line, Character' |
| 3 | + |
| 4 | + var alignmentEnum = Object.freeze([ |
| 5 | + 'left','right','center','justified' |
| 6 | + ]); |
| 7 | + |
| 8 | + var doc = context.document; |
| 9 | + var sharedStyles = doc.documentData().layerTextStyles(); |
| 10 | + var styleObjects = sharedStyles.objects(); |
| 11 | + var textLayer = [[MSTextLayer alloc] initWithFrame:nil]; |
| 12 | + |
| 13 | + |
| 14 | + for (var i=0; i<styleObjects.length; i++) { |
| 15 | + var color, opacity, alignment = ""; |
| 16 | + |
| 17 | + textLayer.setStyle(styleObjects[i].style()); |
| 18 | + |
| 19 | + var name = styleObjects[i].name(); |
| 20 | + |
| 21 | + var attributes = styleObjects[i].style().textStyle().attributes() |
| 22 | + |
| 23 | + var paragraphStyle = attributes.NSParagraphStyle; |
| 24 | + |
| 25 | + if(paragraphStyle) { |
| 26 | + alignment = alignmentEnum[paragraphStyle.alignment()] || ""; |
| 27 | + } |
| 28 | + |
| 29 | + var line = textLayer.lineHeight() || ''; |
| 30 | + var character = textLayer.characterSpacing() || ''; |
| 31 | + |
| 32 | + var font = attributes.NSFont.fontName(); |
| 33 | + |
| 34 | + if(attributes.NSColor) { |
| 35 | + color = attributes.NSColor.hexValue(); |
| 36 | + opacity = attributes.NSColor.alphaComponent(); |
| 37 | + } |
| 38 | + var size = attributes.NSFont.pointSize(); |
| 39 | + output += '\n' + name +', '+ font +', '+ color +', '+ opacity +', '+ size +', '+ alignment +', '+ line +', '+ character; |
| 40 | + } |
| 41 | + |
| 42 | + var writeTextToFile = function(text, filePath) { |
| 43 | + var t = [NSString stringWithFormat:@"%@", text], |
| 44 | + f = [NSString stringWithFormat:@"%@", filePath]; |
| 45 | + return [t writeToFile:f atomically:true encoding:NSUTF8StringEncoding error:nil]; |
| 46 | + } |
| 47 | + |
| 48 | + var openInFinder = function(path) { |
| 49 | + var finderTask = [[NSTask alloc] init], |
| 50 | + openFinderArgs = [NSArray arrayWithObjects:"-R", path, nil]; |
| 51 | + |
| 52 | + [finderTask setLaunchPath:"/usr/bin/open"]; |
| 53 | + [finderTask setArguments:openFinderArgs]; |
| 54 | + [finderTask launch]; |
| 55 | + } |
| 56 | + |
| 57 | + var createTempFolderNamed = function(name) { |
| 58 | + var tempPath = getTempFolderPath(name); |
| 59 | + createFolderAtPath(tempPath); |
| 60 | + return tempPath; |
| 61 | + } |
| 62 | + var getTempFolderPath = function(withName) { |
| 63 | + var fileManager = [NSFileManager defaultManager], |
| 64 | + cachesURL = [[fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject], |
| 65 | + withName = (typeof withName !== 'undefined') ? withName : (Date.now() / 1000), |
| 66 | + folderName = [NSString stringWithFormat:"%@", withName]; |
| 67 | + return [[cachesURL URLByAppendingPathComponent:folderName] path]; |
| 68 | + } |
| 69 | + |
| 70 | + var createFolderAtPath = function(pathString) { |
| 71 | + var fileManager = [NSFileManager defaultManager]; |
| 72 | + if([fileManager fileExistsAtPath:pathString]) return true; |
| 73 | + return [fileManager createDirectoryAtPath:pathString withIntermediateDirectories:true attributes:nil error:nil]; |
| 74 | + } |
| 75 | + |
| 76 | + var folder = createTempFolderNamed('sync'); |
| 77 | + var file = folder + '/typography.csv'; |
| 78 | + |
| 79 | + var shortcutPath = folder + '/README.url'; |
| 80 | + var shortcutFile = '[InternetShortcut]\nURL=https://github.com/nolastan/sync.sketchplugin#exporting-styles' |
| 81 | + writeTextToFile(output, file); |
| 82 | + writeTextToFile(shortcutFile, shortcutPath); |
| 83 | + openInFinder(file); |
| 84 | +} |
0 commit comments