Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dump the names of all symbols in a Sketch file to an alphabetically sorted text file.
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
You can’t perform that action at this time.