Text Item Delimiter

Hi all,
I’m trying to turn this text:

Sample Text (More Sample Text)

into this:

Sample text (More sample text)

Basically, I need to have the beginning of the string and the beginning INSIDE the parentheses to be sentence cased.

I’m tryin using a existing TID script I have, but I’m not getting very far.


property theLowerAlphabet : "abcdefghijklmnopqrstuvwxyzà à èìòùáéíóúâêîôûãñõäëïöüÿæåçøœ1234567890"
property theUpperAlphabet : "ABCDEFGHIJKLMNOPQRSTUVWXYZÀÀÈÌÒÙÁÉÍÓÚÂÊÎÔÛÃÑÕÄËÏÖÜŸÆÅÇØŒ1234567890"
property whiteSpace : {space, tab, return, ASCII character 10, ASCII character 13, ASCII character 40}

set theOriginalText to the clipboard

setTID("(")
set textInside to (text item 2 of theOriginalText) as string
setTID(")")
set textInside to (text item 1 of textInside) as string
setTID("")

setTID("(")
set textOutside to (text item 1 of theOriginalText) as string


to setTID(newTID)
	set AppleScript's text item delimiters to {newTID}
end setTID

set textInside to the clipboard

get change_case(textOutside, textInside, "Sentence")


on change_case(this_text, this_case)
	set new_text to ""
	if this_case is not in {"UPPER", "lower", "Title", "Sentence"} then
		return "Error: Case must be UPPER, lower, Title or Sentence"
	end if
	if this_case is "lower" then
		set use_capital to false
	else
		set use_capital to true
	end if
	repeat with this_char in this_text
		set x to offset of this_char in theLowerAlphabet
		if x is not 0 then
			if use_capital then
				set new_text to new_text & character x of theUpperAlphabet as string
				if this_case is not "UPPER" then
					set use_capital to false
				end if
			else
				set new_text to new_text & character x of theLowerAlphabet as string
			end if
		else
			if this_case is "Title" and this_char is in whiteSpace then
				set use_capital to true
			end if
			set new_text to new_text & this_char as string
		end if
	end repeat
	--	set the clipboard to new_text
	--	return new_text
end change_case

--set theInput to the clipboard
return textOutside & "(" & textInside & ")"

Any help would be greatly appreciated!
Thanks in advance and happy holidays!

Browser: Safari 525.26.12
Operating System: Mac OS X (10.5)

There’s got to be a better way, but you could set this up as a function to fit your needs:

set x to quoted form of "Sample Text (More Sample Text)"

set x to do shell script "echo " & x & " | tr 'A-Z' 'a-z'"
set y to quoted form of text 1 of x
set y to do shell script "echo " & y & " | tr 'a-z' 'A-Z'"
set x to y & text 2 thru -1 of x
set n to (offset of "(" in x) + 1
set z to quoted form of text n of x
set z to do shell script "echo " & z & " | tr 'a-z' 'A-Z'"
set x to text 1 thru (n - 1) of x & z & text (n + 1) thru -1 of x

If you can get the separate sentences separated out with text item delimiters, you can just pass them to this routine. This is a modified version of something someone else posted here in the past. Sorry, I don’t remember who.

on changeCase(theConvertString)
	return (do shell script "/usr/bin/python -c \"import sys; " & "print unicode(sys.argv[1], 'utf8').capitalize().encode('utf8')\" " & quoted form of theConvertString) --capitalize, title, lower, upper
end changeCase

well thats actually part of the problem im having. I can get one part, but not the second.
ideally, id like to get each one and set it to a variable, then run the event and piece them back together…

any thoughts?
thanks already!
cwtnospam, yours works, i just want to see if theres a way without shell.
matt, havent tried yet. will give it a shot soon tho! thanks

Are there any assumptions that one can make? Such as, will the string always have one sentence enclosed in parentheses? Could there be no parentheses or more than one set of parentheses?

An idea to experiment with:

set theString to "Sample Text (More Sample Text)"

if theString contains "(" then
	set subStringText to text (offset of "(" in theString) thru (offset of ")" in theString) of theString
end if

yea, the idea is that it will always have one set of parentheses.