Adding a Numerical Amount to each Character of a Variable

I’m trying to write a script to convert names in Excel to their proper punctuation. So if a cell shows “george washington”, my script will change it to “George Washington”. To do this, I’m reading the text of the names in Excel by their ASCII characters to determine if the letters are capitalized or not.

I ran into a problem with names like “GEORGE WASHINGTON”, where every letter is capitalized.

I know I can break down that name with AppleScript into ASCII characters like this:


set theName_ascii to {}
	
	repeat with a from 1 to the count of characters in theName
		set the end of theName_ascii to (ASCII number of (character a of theName))
	end repeat

And I know that the “EORGE” and “ASHINGTON” parts of the names are what need to be in lower case letters. Looking at the ASCII charts, I know that each one of those letters “E”, “O”, “R”, ect… represented by their respective ASCII characters need to be added by 32 each to get to their lower case letters.

Is there a way in AppleScript to add 32 to each “item” of the text in the Excel cell after breaking it down into ASCII characters?

In English, I’d say “For each character in the variable theName_ascii, add 32”. And the results would be such that each letter would now be represented by their lowercase ASCII character.

Hi George,

what’s about this simple solution


set a to "george washington"
do shell script "echo " & quoted form of a & " | awk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1'"
--> "George Washington"

Hey Stefan, thanks for the reply. That shell script does work, but how would I modify it to work on cases where I need to not de-capitalize letters
, like in the case where the name is GEORGE WASHINGTON, that shell script will leave it all capitalized.

I am amazed though by that simple line. You did in one line of code what took me 50 +, so for cases where I just need to capitalize the first or last name, I’m definitely going to use your code over mine

(sorry if that’s a simple question … I really don’t know anything about shell scripting)


set a to "GEORGE WASHINGTON thomas jefferson"
do shell script "echo " & quoted form of a & " | tr A-Z a-z | awk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1'"
--> "George Washington Thomas Jefferson"

Thanks StefanK, you’re a genius!! works perfectly

See also: Change Text Case