Place SVG and assign name and Unicode when dragged?

Question:
I have a folder with SVGs,
for the filenames, they have a name, plus their unicode name, ie:

search-uniE000.svg
trash-uniE001.svg
home-uniE002.svg

Is there a way to assign both the Unicode AND the “nice name” when placing (drag and dropping) the SVGs in Glyphs?

That looks like a job for a script.

1 Like

thanks, I think editing the file in Sublime Text seems more practical for now.
Haven’t found a syntax highlighter for plist, but the AppleScript one seems to work fine so far.

Search for property list. There should be Sublime Text compatible syntax definitions.

1 Like

Here’s a stub you can use:

import os
from Foundation import NSURL
thisFont = Glyphs.font
directory = "~/Desktop/" # trailing slash

for fileName in os.listdir(directory):
	if fileName.endswith(".svg"):
		# make note in macro window:
		print("Importing %s" % fileName)
		
		# check if the name complies:
		if "-" in fileName:
			glyphName, unicodeValue = fileName.replace(".svg","").split(":")[0:2]
			unicodeValue = unicodeValue.replace("uni","")
			# you may want to do a sanity check here and find out if
			# unicodeValue really is 4 chars and only "0-F" etc.
		else:
			glyphName = fileName.replace(".svg","")
			unicodeValue = None
		
		# create the glyph and add it to the font:
		newGlyph = GSGlyph(glyphName, autoName=False)
		newGlyph.unicode = unicodeValue
		thisFont.glyphs.append(newGlyph)
		firstLayer = newGlyph.layers[0]
		
		# for importing the SVG into the first layer, we need to convert
		# the path into an NSURL, because the method requires it for input:
		svgPath = directory+fileName
		svgURL = NSURL.alloc().initFileURLWithPath_(svgPath)
		firstLayer.importOutlinesFromURL_scale_error_(svgURL, 1.0, None)
1 Like

Hi @mekkablue , thanks a lot!

Does the script work in Glyphs 2? or do I need Glyphs3?

In Glyphs 2 I’m getting this error in the Macro window:

Importing home-E002.svg
Traceback (most recent call last):
  File "<macro>", line 23, in <module>
ValueError: need more than 1 value to unpack

thanks


Edit:

found this script one from a different thread,
There seems to be a minor bug, in that it doesn’t create the SVG layer, but is solved by manually creating a “svg” one, that fixes it, showing the placed SVG correctly.
as usual, thanks!