Change Text Case

Thanks Nigel,

Your example 2.0 is much better for today’s Applescript. Maybe I should have commented that my example was only for Tiger and earlier.

Well my example is almost directly translated from C. That’s why I pointed out that it is useless because Applescript is high level. Well as you can see when running a script like this


--in Tiger and older
set characterArray to {}
repeat with x from 1 to 127
set end of characterArray to ascii character x
end
return characterArray as string

--in Leopard and newer
set characterArray to {}
repeat with x from 1 to 127
	set end of characterArray to string id x
end repeat
return characterArray as string

that the sequence of the lower case characters and upper case characters are the same. So I think foreknowledge is not the case here. I agree with you when saying that ascii number “a” - ascii number “A” is tons of overhead I’ve created in my script. But like I said it is a translation from C and we don’t have that problem there.

Hi. You all have really complex methods of changing the cases, but I used a much simpler way of doing things.

--Capitalize 1.0.1
--Copyright © Panah Neshati, 2011.

on replaceText(find, replace, subject)
	set prevTIDs to text item delimiters of AppleScript
	set text item delimiters of AppleScript to find
	set subject to text items of subject
	
	set text item delimiters of AppleScript to replace
	set subject to "" & subject
	set text item delimiters of AppleScript to prevTIDs
	
	return subject
end replaceText

on capitalize(a)
	set a to (get replaceText("a", "A", a))
	set a to (get replaceText("b", "B", a))
	set a to (get replaceText("c", "C", a))
	set a to (get replaceText("d", "D", a))
	set a to (get replaceText("e", "E", a))
	set a to (get replaceText("f", "F", a))
	set a to (get replaceText("g", "G", a))
	set a to (get replaceText("h", "H", a))
	set a to (get replaceText("i", "I", a))
	set a to (get replaceText("j", "J", a))
	set a to (get replaceText("k", "K", a))
	set a to (get replaceText("l", "L", a))
	set a to (get replaceText("m", "M", a))
	set a to (get replaceText("n", "N", a))
	set a to (get replaceText("o", "O", a))
	set a to (get replaceText("p", "P", a))
	set a to (get replaceText("q", "Q", a))
	set a to (get replaceText("r", "R", a))
	set a to (get replaceText("s", "S", a))
	set a to (get replaceText("t", "T", a))
	set a to (get replaceText("u", "U", a))
	set a to (get replaceText("v", "V", a))
	set a to (get replaceText("w", "W", a))
	set a to (get replaceText("x", "X", a))
	set a to (get replaceText("y", "Y", a))
	set a to (get replaceText("z", "Z", a))
	return a
end capitalize

And, of course, the opposite,

--Lowercase 1.0.1
--Copyright © Panah Neshati, 2011.

on replaceText(find, replace, subject)
	set prevTIDs to text item delimiters of AppleScript
	set text item delimiters of AppleScript to find
	set subject to text items of subject
	
	set text item delimiters of AppleScript to replace
	set subject to "" & subject
	set text item delimiters of AppleScript to prevTIDs
	
	return subject
end replaceText

on lowercase(a)
	set a to (get replaceText("A", "a", a))
	set a to (get replaceText("B", "b", a))
	set a to (get replaceText("C", "c", a))
	set a to (get replaceText("D", "d", a))
	set a to (get replaceText("E", "e", a))
	set a to (get replaceText("F", "f", a))
	set a to (get replaceText("G", "g", a))
	set a to (get replaceText("H", "h", a))
	set a to (get replaceText("I", "i", a))
	set a to (get replaceText("J", "j", a))
	set a to (get replaceText("K", "k", a))
	set a to (get replaceText("L", "l", a))
	set a to (get replaceText("M", "m", a))
	set a to (get replaceText("N", "n", a))
	set a to (get replaceText("O", "o", a))
	set a to (get replaceText("P", "p", a))
	set a to (get replaceText("Q", "q", a))
	set a to (get replaceText("R", "r", a))
	set a to (get replaceText("S", "s", a))
	set a to (get replaceText("T", "t", a))
	set a to (get replaceText("U", "u", a))
	set a to (get replaceText("V", "v", a))
	set a to (get replaceText("W", "w", a))
	set a to (get replaceText("X", "x", a))
	set a to (get replaceText("Y", "y", a))
	set a to (get replaceText("Z", "z", a))
	return a
end lowercase

While it’s not exactly efficient coding, it certainly gets the job done. I’m working on making an improved version that gets the ASCII number of the input, adds 32, and gets the new ASCII character.

I’ll reply when it’s finished.

Hi, pneshati.

That’s exactly what my script in post #40 above does, except that mine uses the character ‘id’ functions introduced with AppleScript 2.0 (in Mac OS 10.5) rather than the now deprecated ‘ASCII character’ and ‘ASCII number’.

You’ll probably find that your “improved version” will be much slower than what you have already, since it will have to call both the ‘ASCII number’ and ‘ASCII character’ functions for every individual character in the text and will have to test each character individually to see if it’s a letter, whether it’s already in the required case, etc.

Thanks for the advice, Nigel! I didn’t know about the character id functions, I’ll certainly check them out.

:slight_smile: