Mac AppleScript: Capitalize text in your clipboard!
I was looking for a way to capitalize my keyword lists (each word, not every letter), and found this little gem off of MacOSXHints.com.
If allows you to input some text and get it capitalized all together, each word, or sentences. I think you can go the other way around too.
**I added a little update so you do not have to input the text manually, it will just take the text that is currently in your clipboard.
This is very useful for mass editing keywords in Yahoo Search Marketing. The reason I use it in YSM is because when you are using keyword insertion, there isn’t an option to automatically capitalize your keywords so I have to do it myself. I just copy my keyword list into the clipboard and press run! It will output the newly formatted text which you can pop into your YSM account.
-- syntax : changeCase of someText to caseType
-- someText (string) : plain or encoded text
-- caseType (string) : the type of case required ("upper", "lower", "sentence", "title" or "mixed")
-- "upper" : all uppercase text (no exceptions)
-- "lower" : all lowercase text (no exceptions)
-- "sentence" : uppercase character at start of each sentence, other characters lowercase (apart from words in sentenceModList)
-- "title" : uppercase character at start of each word, other characters lowercase (no exceptions)
-- "mixed" : similar to title, except for definite and indefinite articles, conjunctions and prepositions (see mixedModList) that don't start a sentence
property lowerStr : "abcdefghijklmnopqrstuvwxyzáàâäãåæçéèêëíìîïñóòôöõøœúùûüÿ"
property upperStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZÁÀÂÄÃÅÆÇÉÈÊËÍÌÎÏÑÓÒÔÖÕØŒÚÙÛÜŸ"
property alphaList : lowerStr's characters & reverse of upperStr's characters
property sentenceBreak : {".", "!", "?"}
property wordBreak : {space, ASCII character 202, tab}
property everyBreak : wordBreak & sentenceBreak
property whiteSpace : wordBreak & {return, ASCII character 10}
property currList : missing value
property sentenceModList : {"i", "i'm", "i’m", "i've", "i’ve", "I’ve", "I've", "I’m", "I'm", "I"} (* could be extended to include certain proper nouns, acronyms, etc. *)
property mixedModList : {"By Means Of", "In Front Of", "In Order That", "On Account Of", "Whether Or Not", "According To", "As To", "Aside From", "Because Of", "Even If", "Even Though", "In Case", "Inside Of", "Now That", "Only If", "Out Of", "Owing To", "Prior To", "Subsequent To", "A", "About", "Above", "Across", "After", "Against", "Along", "Although", "Among", "An", "And", "Around", "As", "At", "Because", "Before", "Behind", "Below", "Beneath", "Beside", "Between", "Beyond", "But", "By", "De", "Down", "During", "Except", "For", "From", "If", "In", "Inside", "Into", "Like", "Near", "Of", "Off", "On", "Onto", "Or", "Out", "Outside", "Over", "Past", "Since", "So", "The", "Though", "Through", "Throughout", "To", "Under", "Unless", "Until", "Up", "Upon", "When", "Whereas", "While", "With", "Within", "Without", "Ye", "ye", "without", "within", "with", "while", "whereas", "when", "upon", "up", "until", "unless", "under", "to", "throughout", "through", "though", "the", "so", "since", "past", "over", "outside", "out", "or", "onto", "on", "off", "of", "near", "like", "into", "inside", "in", "if", "from", "for", "except", "during", "down", "de", "by", "but", "beyond", "between", "beside", "beneath", "below", "behind", "before", "because", "at", "as", "around", "and", "an", "among", "although", "along", "against", "after", "across", "above", "about", "a", "subsequent to", "prior to", "owing to", "out of", "only if", "now that", "inside of", "in case", "even though", "even if", "because of", "aside from", "as to", "according to", "whether or not", "on account of", "in order that", "in front of", "by means of"}
on textItems from currTxt
tell (count currTxt's text items) to if it > 4000 then tell it div 2 to return my (textItems from (currTxt's text 1 thru text item it)) & my (textItems from (currTxt's text from text item (it + 1) to -1))
currTxt's text items
end textItems
on initialCap(currTxt)
tell currTxt to if (count words) > 0 then tell word 1's character 1 to if it is in lowerStr then
set AppleScript's text item delimiters to it
tell my (textItems from currTxt) to return beginning & upperStr's character ((count lowerStr's text item 1) + 1) & rest
end if
currTxt
end initialCap
to capItems from currTxt against breakList
repeat with currBreak in breakList
set text item delimiters to currBreak
if (count currTxt's text items) > 1 then
set currList to my (textItems from currTxt)
repeat with n from 2 to count currList
set my currList's item n to initialCap(my currList's item n)
end repeat
set text item delimiters to currBreak's contents
tell my currList to set currTxt to beginning & ({""} & rest)
end if
end repeat
currTxt
end capItems
on modItems from currTxt against modList
set currList to modList
set currCount to (count modList) div 2
repeat with currBreak in everyBreak
set text item delimiters to currBreak
if (count currTxt's text items) > 1 then repeat with n from 1 to currCount
set text item delimiters to my currList's item n & currBreak
if (count currTxt's text items) > 1 then
set currTxt to textItems from currTxt
set text item delimiters to my currList's item -n & currBreak
tell currTxt to set currTxt to beginning & ({""} & rest)
end if
end repeat
end repeat
currTxt
end modItems
to changeCase of currTxt to caseType
if (count currTxt's words) is 0 then return currTxt
ignoring case
tell caseType to set {upper_Case, lower_Case, sentence_Case, title_Case, mixed_Case} to {it is "upper", it is "lower", it is "sentence", it is "title", it is "mixed"}
end ignoring
if not (upper_Case or lower_Case or title_Case or sentence_Case or mixed_Case) then
error "The term \"" & caseType & "\" is not a valid case type option. Please use \"upper\", \"lower\", \"sentence\", \"title\" or \"mixed\"."
else if upper_Case then
set n to 1
else
set n to -1
end if
considering case
set tid to text item delimiters
repeat with n from n to n * (count lowerStr) by n
set text item delimiters to my alphaList's item n
set currTxt to textItems from currTxt
set text item delimiters to my alphaList's item -n
tell currTxt to set currTxt to beginning & ({""} & rest)
end repeat
if sentence_Case then
set currTxt to initialCap(modItems from (capItems from currTxt against sentenceBreak) against sentenceModList)
else if title_Case or mixed_Case then
set currTxt to initialCap(capItems from currTxt against whiteSpace)
if mixed_Case then set currTxt to initialCap(capItems from (modItems from currTxt against mixedModList) against sentenceBreak)
end if
set text item delimiters to tid
end considering
set the clipboard to currTxt
end changeCase
(* set someText to " " *)
set someText to (the clipboard)
changeCase of someText to "title" (* "upper", "lower", "sentence", "title" or "mixed" *)
Hope this solves some other people’s problems!
-Nick
Grab a feed!
Buy me a coffee


February 29th, 2008 at 4:25 pm
Good evening, Nick!
And thank you very much for sharing this text-manipulating code with us. Nevertheless I think, that it might have been a lot easier to delegate some of the string manipulation to Python using the do shell script-command.
Just compare this whole AppleScript code for creating upper and lower case representations with the simple and elegant Python string functions:
name = u’Martin was here’
name.lower() # lower case > u’martin was here’
name.upper() # upper case > u’MARTIN WAS HERE’
name.title() # title > u’Martin Was Here’
Best regards from Germany,
Martin
February 29th, 2008 at 11:21 pm
Not a problem!
I wish I knew what all that meant… I’m a web programmer that wishes he knew a little bit more about computer programming. The way you showed definitely looks a lot more efficient and short! If only I knew how to implement it.
Thanks for the comment!
Nick
March 1st, 2008 at 4:50 am
I recently wrote a sample script, that shows how to combine the power of AppleScript and Python. You can get it right here:
http://joyofscripting.com/wp/?p=138
As a web programmer you should have no problems in understanding the code, it’s pretty straight forward. The whole script can be opened and inspected in the Script Editor.
Have a pretty good weekend!
Martin
March 3rd, 2008 at 8:38 am
Nick, this is a great script. I really like what you are doing here.
May 15th, 2008 at 9:16 am
What charset is this page? The second half of the script appears as gibberish on my browser.
May 15th, 2008 at 9:41 am
ah, it seems it was just a render glitch. UTF-8
May 15th, 2008 at 10:43 am
Actually I fixed it and didn’t have enough time to comment until now. Thanks for letting me know!
Sorry,
Nick