Created
February 28, 2019 02:10
Star
You must be signed in to star a gist
Dump the names of all symbols in a Sketch file to an alphabetically sorted text 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
import os | |
import zipfile | |
import tempfile | |
import sys | |
import json | |
sketch_doc = sys.argv[1] | |
def get_symbol_names(zipname): | |
symbol_names = [] | |
# read the sketch file as a zip file | |
with zipfile.ZipFile(zipname, 'r') as sketch_file: | |
for item in sketch_file.infolist(): | |
data = sketch_file.read(item.filename) | |
# find pages and collect symbol names | |
if item.filename.startswith('pages') and item.filename.endswith('.json'): | |
parsed_json = json.loads(data) | |
for layer in parsed_json['layers']: | |
# only collect names of symbol masters | |
if layer['_class'] == 'symbolMaster': | |
symbol_names.append(layer['name']) | |
sketch_file.close() | |
sorted_symbol_names = sorted(symbol_names) | |
# make new file for symbol names | |
out_name = '%s-symbol-names.txt' % sketch_doc | |
out = open(out_name, 'w+') | |
for item in sorted_symbol_names: | |
line = '%s\r\n' % item | |
out.write(line) | |
out.close() | |
print 'Found %d symbols in the document.' % len(symbol_names) | |
get_symbol_names(sketch_doc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment