Changing Text to Title Case

Am a complete newbie to this whole AppleScript thing (though used to program Basic when I was a kid and have dabbled with RealBasic in recent years) but it’s fun and rewarding - strangely satisfying when you compile a lot of lines in one go and it does it with no errors.

Anyway enough of my newbie ramblings.

I have been looking for a way to change a string (in this case a person’s name) if entered in lowercase into title case (ie. tom cruise into Tom Cruise).

The following works (and am quite pleased with myself for getting it right) but thought I’d post it here incase anyone has any ideas for streamlining it or if infact, as I suspect, there is a single command or simple series of commands that can do the same job.



set entered_text to text returned of (display dialog "enter name" default answer "")
set count_words to count words in entered_text
set word_list to words 1 through count_words of entered_text as list
set revised_text to ""

repeat with i from 1 to count_words
	
	set this_item to item i of word_list
	set ascii_number to ASCII number this_item
	if ascii_number > 96 and ascii_number < 123 then -- only affect lowercase letters
		set ascii_number to ascii_number - 32
		set capital_letter to ASCII character ascii_number
		set count_letters to count characters in this_item
		if count_letters is 1 then
			set revised_word to capital_letter
		else
			set revised_word to characters 2 through count_letters of this_item
			set revised_word to revised_word as string
			set revised_word to capital_letter & revised_word
		end if
		if i < count_words then
			set revised_text to revised_text & revised_word & " "
		else
			set revised_text to revised_text & revised_word
		end if
	else
		if i < count_words then
			set revised_text to revised_text & this_item & " "
		else
			set revised_text to revised_text & this_item
		end if
	end if

end repeat



I remember from my old Basic days that there were commands left$, mid$ and right$ which would be a far quicker way of getting all the letters in a word bar the first one (ie a$ = right$ (this_item, len (this_item -1)) or something like that - i forget the exact syntax, it’s been over twenty years but Applescript only seems to have ‘middle’ not left or right.

I’m happy with how it works, though it only changes the first letter at the moment thus

tom cruise becomes Tom Cruise
TOM CRUISE stays as TOM CRUISE
tOM cRUISE becomes TOM CRUISE etc

though I guess I could use the same principles to change every other letter.

Hi Hawkeye.

In Code Exchange forum there are a few suggestions:

http://bbs.applescript.net/viewtopic.php?id=12758

Great first effort Hawkeye - welcome to the board.

Note in the examples that care must be taken to guard against characters that are not letters, like periods after initials, for example.

Thank you, very useful.

Though I think shell scripts looks like something it might be a long, long time before I look into.

Have no idea how or why this works but it’ll be perfect for my needs:

on titlecase(txt)
   return do shell script "python -c \"import sys; print unicode(sys.argv[1], 'utf8').title().encode('utf8')\" " & quoted form of txt
end titlecase

Here’s another like it in Perl:

set titleCaseString to (do shell script "/bin/echo" & space & quoted form of theString & space & "| /usr/bin/perl -p -e 's/(\\w+)/\\u\\L$1/g;'") as Unicode text

where ‘theString’ must be defined first.

Thanks Adam, nice to be made to feel welcome.

Ironically, and probably inadvertently, my attempt got round the problem of punctuation or periods by only concerning itself with ASCII characters 97 to 122.

– Actually, no I’m wrong. My version discards any punctuation. Ah. Perl and shell scripts, the way to go. Yay!

Also, I must admit to being a little surprised, considering how user friendly Applescript is in most areas, that there is not a single command to do this.

ie: set my_text to uppercase entered_text as string
or: set my_text to titlecase entered_text as string

Would have been nice :slight_smile:

There is a scripting addition by Satimage (Satimage osax 3.1.8) which provides (almost) this function,
but it changes every character of the string

http://www.satimage.fr/software/en/downloads_osaxen_pop.html

Hi Hawkeye,

You can make your own subroutines and if you want create a library. For instance your basic stuff could be something like this:

set t to "abCd"
set r to rght(t, len(t) - 1)

on len(t)
   return length of t
end len

on rght(t, l)
   set r to ""
   try
      set r to text -l thru -1 of t
   end try
   return r
end rght

gl,