Use "sample strings" as object

Maybe this is not a useful thing but I was wondering if it is possible to manipulate the sample string in order to add, subtract, export or import stuff in it via script.

Can it be used like this?

Thanks.

I guess this snippet will do:

def addToSampleStrings( kernStrings ):
		sampleTexts = Glyphs.defaults["SampleTexts"].mutableCopy()
		sampleTexts.extend(kernStrings)
		Glyphs.defaults["SampleTexts"] = sampleTexts

myNewKernStrings = ["abcdefg","hijklmnop","qrstuvwxyz"]
addToSampleStrings( myNewKernStrings )
2 Likes

It worked perfectly, @mekkablue! Thanks a lot.

However, could you explain what mutableCopy() means?

Thanks again.

It is a PyObjC thing. I need a (editable) list in the sampleTexts variable because I want to add something to them. The way to do that in Cocoa is to request a ‘mutable copy’ with the function of the same name.

1 Like

@mekkablue this solution works for Glyphs 2 but not Glyphs 3. How can we access the user’s sample strings in v3?

In Glyphs 3, you need to add a dict to the preferences with the appropriate Glyphs.defaults. There’s a mekkablue script Kerning > Sample String Maker. It imports the sampleText.py next to it. In that file, look for the addToSampleText() method.

1 Like

Thanks Rainer! So the sample text is now in Glyphs.defaults["SampleTextsList"] and is structured as a list of dictionaries. :+1:

Hey @mekkablue,
I was looking to write a script to add string in the Sample Strings Menu, but I feel something is not working correctly.

It’s seem that your script no longer add string is the Sample Strings.

It’s seems that content display in the Sample Strings Menu come from Glyphs.defaults["SampleTextsList"]

Wherease your script write in Glyphs.defaults["SampleTexts"]

Adding or Removing item with the UI in the Sample Strings Menu change Glyphs.defaults["SampleTextsList"] but not Glyphs.defaults["SampleTexts"]

No, it works, it is just a UI update problem, unfortunately. Restart the app and see again.

My apologies, I don’t know why but I was looking to an old sampleText.py (probaby from Glyph2) That is why my script didn’t work correcly.

1 Like