Convert to Uppercase Script

(This is such a newbie question!)
I’m trying to make a very simple script that will convert the contents of a text tab into uppercase - either only the highlighted selection, or the entire tab if no text is selected.


But already I’m coming across this error:

upper() works on a string, and you are trying to apply it to a list. What about this:

upperCaseGlyphNames = ["/" + l.parent.name.upper() for l in selectedLayers]

Remember to catch exceptions like AE and OE and non-letters.

Thanks for the hint!

So far I’ve come up with this - I know it has many problems though.

  • It won’t catch glyphs if they are not in the dictionary: i.e. .001, .002, ss.01, ss.02 or glyphs with nonstandard unicode names
    A solution I’ve tried looking at is instead of using the glyph name, use the unicode, but trying unicode(eachGlyph) still returns the glyph name? Or, do some kind of grep (or filter?) to catch anything with a (glyph).(feature), i.e. /g.ss01, and strip and convert to /G
  • If no text is selected, it only converts the glyph after the cursor position.
  • It still only opens a new tab instead of converting the text in the current tab (maybe this is better anyway).
  • Ligatures…
  • Breaks at linebreaks (edit: works tentatively now at linebreaks)

Any hints :slight_smile: ?

The python function ‘unicode()’ converts a ‘str’ into and ‘Unicode’ object. That is a very anoying distinction in python and not what you need here.

Why is that the script works if I run it as code from the Macro panel, but not as a script…?

Here is a method that can convert lowercase glyph name to uppercase:

def UppercaseNameForName(name):
baseName = name
suffix = ““
periodPos = baseName.find(”.”)
if periodPos > 0:
baseName = name[:periodPos]
suffix = name[periodPos:]
Char = int(GSGlyphsInfo.glyphInfoForName_(baseName).unicode(), 16)
String = u"%c" % Char
UpperChar = ord(String.upper()[0])
UpperUnicode = “%0.4X” % UpperChar
UpperName = GSGlyphsInfo.glyphInfoForUnicode_(UpperUnicode).name()
return UpperName+suffix

lowercases = [“a”, “ae”, “a.ss01”]
for name in lowercases:
print UppercaseNameForName(name)

It might need some error checking for names that are not defined or have no uppercase…

Thanks for the help Georg,

Also, a.ss01 would probably only rarely correspond with A.ss01 ?

I incorporated your code into this gist but I’m curious why it still doesn’t work from the scripts folder - it hangs and crashes, but it works as a script inside the macro panel (Version 1.4.4 (602)), v2 works fine!

Furhtermore, how can I make the script:

  1. Replace highlighted text instead of creating a new tab
  2. Work on the whole text if no text is selected
Probably. But that wasn't the question ;)

What do you like to accomplish with this script? It seems like you just like to edit the uppercase counterparts of the selected glyphs?

I’ve found at times I want to easily make the text I have all uppercase i.e. with diacritic texts, or texts generated form stringmaker.

Of course, I also save texts I often use in lowercase and uppercase in Sample Strings.

This https://gist.github.com/schriftgestalt/2f5134d955229f63402a
should do what you need.

Thank you very much!