-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from nolastan/export
Add export functionality + Update for Sketch 41
- Loading branch information
Showing
7 changed files
with
132 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
var onRun = function(context) { | ||
var output = 'Style, Typeface, Color, Opacity, Size, Alignment, Line, Character' | ||
|
||
var alignmentEnum = Object.freeze([ | ||
'left','right','center','justified' | ||
]); | ||
|
||
var doc = context.document; | ||
var sharedStyles = doc.documentData().layerTextStyles(); | ||
var styleObjects = sharedStyles.objects(); | ||
var textLayer = [[MSTextLayer alloc] initWithFrame:nil]; | ||
|
||
|
||
for (var i=0; i<styleObjects.length; i++) { | ||
var color, opacity, alignment = ""; | ||
|
||
textLayer.setStyle(styleObjects[i].style()); | ||
|
||
var name = styleObjects[i].name(); | ||
|
||
var attributes = styleObjects[i].style().textStyle().attributes() | ||
|
||
var paragraphStyle = attributes.NSParagraphStyle; | ||
|
||
if(paragraphStyle) { | ||
alignment = alignmentEnum[paragraphStyle.alignment()] || ""; | ||
} | ||
|
||
var line = textLayer.lineHeight() || ''; | ||
var character = textLayer.characterSpacing() || ''; | ||
|
||
var font = attributes.NSFont.fontName(); | ||
|
||
if(attributes.NSColor) { | ||
color = attributes.NSColor.hexValue(); | ||
opacity = attributes.NSColor.alphaComponent(); | ||
} | ||
var size = attributes.NSFont.pointSize(); | ||
output += '\n' + name +', '+ font +', '+ color +', '+ opacity +', '+ size +', '+ alignment +', '+ line +', '+ character; | ||
} | ||
|
||
var writeTextToFile = function(text, filePath) { | ||
var t = [NSString stringWithFormat:@"%@", text], | ||
f = [NSString stringWithFormat:@"%@", filePath]; | ||
return [t writeToFile:f atomically:true encoding:NSUTF8StringEncoding error:nil]; | ||
} | ||
|
||
var openInFinder = function(path) { | ||
var finderTask = [[NSTask alloc] init], | ||
openFinderArgs = [NSArray arrayWithObjects:"-R", path, nil]; | ||
|
||
[finderTask setLaunchPath:"/usr/bin/open"]; | ||
[finderTask setArguments:openFinderArgs]; | ||
[finderTask launch]; | ||
} | ||
|
||
var createTempFolderNamed = function(name) { | ||
var tempPath = getTempFolderPath(name); | ||
createFolderAtPath(tempPath); | ||
return tempPath; | ||
} | ||
var getTempFolderPath = function(withName) { | ||
var fileManager = [NSFileManager defaultManager], | ||
cachesURL = [[fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject], | ||
withName = (typeof withName !== 'undefined') ? withName : (Date.now() / 1000), | ||
folderName = [NSString stringWithFormat:"%@", withName]; | ||
return [[cachesURL URLByAppendingPathComponent:folderName] path]; | ||
} | ||
|
||
var createFolderAtPath = function(pathString) { | ||
var fileManager = [NSFileManager defaultManager]; | ||
if([fileManager fileExistsAtPath:pathString]) return true; | ||
return [fileManager createDirectoryAtPath:pathString withIntermediateDirectories:true attributes:nil error:nil]; | ||
} | ||
|
||
var folder = createTempFolderNamed('sync'); | ||
var file = folder + '/typography.csv'; | ||
|
||
var shortcutPath = folder + '/README.url'; | ||
var shortcutFile = '[InternetShortcut]\nURL=https://github.com/nolastan/sync.sketchplugin#exporting-styles' | ||
writeTextToFile(output, file); | ||
writeTextToFile(shortcutFile, shortcutPath); | ||
openInFinder(file); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,20 +4,27 @@ | |
{ | ||
"script" : "script.cocoascript", | ||
"handler" : "onRun", | ||
"shortcut" : "option s", | ||
"name" : "Sync", | ||
"identifier" : "sync" | ||
"shortcut" : "command option s", | ||
"name" : "⬇️ Import from URL", | ||
"identifier" : "import" | ||
}, | ||
{ | ||
"script" : "export.cocoascript", | ||
"handler" : "onRun", | ||
"name" : "⬆️ Export typography as CSV", | ||
"identifier" : "export" | ||
}, | ||
], | ||
"menu": { | ||
"isRoot": true, | ||
"isRoot": false, | ||
"items": [ | ||
"sync", | ||
"export", | ||
"import" | ||
], | ||
}, | ||
"identifier" : "com.github.nolastan.sync", | ||
"version" : "1.1", | ||
"description" : "Sync styles and symbols", | ||
"authorEmail" : "[email protected]", | ||
"name" : "Sync" | ||
"name" : "🎨 Sync" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters