Skip to content

Instantly share code, notes, and snippets.

@nkrisc
Created February 28, 2019 02:10
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nkrisc/afef754770c6c28f7ba7ee232b59716a to your computer and use it in GitHub Desktop.
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