Capitalize only the 1st word in a sentence

I’m wanting, if possible to capitalize only the first word in a sentence of provided text. I’ve seen scripts that capitalise all words in a sentence, but not just the first word.

For example:
“this text contains a Name in the PDF”

I’d only want to capitalise “this” in the sentence, and leave any other words that had capitals or were in uppercase.
Any help would be greatly appreciated.

Maybe you will find what you need in :

(*
Pages 5.0 casses
Yvan KOENIG (VALLAURIS, France)
*)
# Initialisation de quelques variables
if my parle_anglais() then
	set lesChoix_theChoices to {"lowercase", "UPPERCASE", "Sentences"}
	set leTitre_theTitle to "Select a case"
	set errMessage to "Nothing is selected in Pages !"
else
	set lesChoix_theChoices to {"minuscules", "MAJUSCULES", "Phrase"}
	set leTitre_theTitle to "Casse à appliquer"
	set errMessage to "Rien n'est sélectionné dans Pages !"
end if

# Use the Finder's localized strings so that default shortcut escape apply
tell application "Finder"
	set OKbtn to localized string "AL4"
	set annuler_cancel to localized string "AL1"
end tell

tell application "/Applications/Pages.app" to activate
tell application "System Events" to tell (first process whose frontmost is true)
	set appID to its id
end tell
(*
Fill the clipboard with a fake string *)
set tt to "All The Things You Could Be By Now If Sigmund Freud's Wife Was Your Mother, © Charles Mingus"
set the clipboard to tt
tell application "System Events" to tell (first process whose id is appID)
	delay 0.2
	keystroke "c" using {command down}
	tell (first application whose frontmost is true)
		(*
Loop waiting the achievement of the Copy task. *)
		repeat 30 times
			try
				if (the clipboard as text) is not tt then exit repeat
			on error
				delay 0.1
			end try
		end repeat
		
		set texte_text to the clipboard as text
		
		if texte_text is tt then error errMessage
		
		tell application "/Applications/Pages.app" # (path to frontmost application as string) to put the dialog at front
			set quelleCasse_whichCase to choose from list lesChoix_theChoices with title leTitre_theTitle OK button name OKbtn cancel button name annuler_cancel
			if quelleCasse_whichCase is false then error number -128
			set quelleCasse_whichCase to item 1 of quelleCasse_whichCase
		end tell
		
		if quelleCasse_whichCase = item 3 of lesChoix_theChoices then
			repeat with eol in {".", "!", "?", "."}
				set liste_list to my decoupe(texte_text, eol as text)
				repeat with i from 1 to count liste_list
					set item_i to item i of liste_list
					set |trouvéDébutDePhrase_foundBegOfSentence| to false
					repeat with j from 1 to count item_i
						if (character j of item_i) is not in {space, tab, linefeed, return, character id 8232, character id 8233} then
							set |trouvéDébutDePhrase_foundBegOfSentence| to true
							exit repeat
						end if
					end repeat # j
					if |trouvéDébutDePhrase_foundBegOfSentence| then
						character j of item_i
						set converti_converted to my changecase(result, "upper")
						set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ""}
						set l to text items of item_i
						set item j of l to converti_converted
						set item_i to l as text
						set AppleScript's text item delimiters to oTIDs
					end if
					set item i of liste_list to item_i
				end repeat # i
				set texte_text to my recolle(liste_list, eol as text)
			end repeat # eol
		else if quelleCasse_whichCase is item 1 of lesChoix_theChoices then
			set texte_text to my changecase(texte_text, "lower")
		else if quelleCasse_whichCase is item 2 of lesChoix_theChoices then
			set texte_text to my changecase(texte_text, "upper")
		end if
		set the clipboard to texte_text
	end tell
	keystroke "v" using {command down}
end tell

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to "" & l
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====
(*
Le second argument peut être: "upper", "lower", "title", "capitalize"
ATTENTION, capitalize change uniquement le premier caractère de la sélection
*)

on changecase(txt, mode)
	set python_script to "import sys; print sys.argv[1].decode('utf8')." & mode & "().encode('utf8')"
	return do shell script "/usr/bin/python -c " & quoted form of python_script & " " & quoted form of txt
end changecase

#=====
(*
Le second argument peut être: "upper", "lower", "capitalize"
ATTENTION, capitalize change uniquement le premier caractère de la sélection
*)
(*
on change_case(txt, mode)
	tell application "ASObjC Runner"
		if mode is "upper" then
			return modify string txt so it is caps
		else if mode is "lower" then
			return modify string txt so it is lower
		else if mode is "capitalize" then
			return modify string txt so it is cap words
		end if
	end tell
end change_case
*)
#=====

on parle_anglais()
	return (do shell script "defaults read 'Apple Global Domain' AppleLocale") does not start with "fr_"
end parle_anglais

#=====

The script was written for a French user so several varNames and handlerNames are ” more or less ” French Words.

Yvan KOENIG (VALLAURIS, France) vendredi 17 janvier 2014 17:28:41

Here’s another way. :slight_smile:

set theSentence to "This text contains a Name in the PDF" as text
set theSentence to capFirstLetter(theSentence)

on capFirstLetter(theString)
	set theLength to length of theString
	set theResult to "" as string
	try
		if theLength > 1 then
			set endString to characters 2 thru theLength of theString as text
			
			set theResult to upperCaseChar(character 1 of theString) & endString as text
		else
			set theResult to upperCaseChar(character 1 of theString) as text
		end if
	end try
	return theResult
end capFirstLetter

on upperCaseChar(theChar)
	set asciiChar to ASCII number of theChar
	if asciiChar ≥ 97 and asciiChar ≤ 122 then set theChar to ASCII character of (asciiChar - 32)
	return theChar
end upperCaseChar

Or Google MacScripter for “sentence case”: Change Text Case

Thanks a lot guys.

I’ll use haolesurferdude’s as it’s simpler/easier for me to understand :slight_smile:

haolesurferdude’s code treats a single sentence.
Triggering such a script is not faster than changing the first character by hand.
The code which I sent is designed to capitalize the first character of every sentences embedded in a block of text containing several sentences.

My memory fooled me, most of the varNames are made of French names concatenated to English names.
The code is really clever. It doesn’t use complex instructions.

The other proposed answers treat only ASCII characters, mine treats Unicode ones.

Yvan KOENIG (VALLAURIS, France) vendredi 17 janvier 2014 21:49:11

Thanks, I’ll look at it in more depth :slight_smile: