Help - Modify Text Format Inside Quotes

I need a script that will set text to Italic if inside quotes and set text to bold if inside parenthesis.

This is for use on Pages in the body and text boxes.

Example

John said “Hello” (Ref1)

After running script should be

John said “Hello” (Ref1)

SOLUTION:

 tell application "Pages"
	tell front document
		set allTextItems to every text item
		if allTextItems is not {} then
			repeat with thisTextItem in allTextItems
				my handleIt(a reference to object text of thisTextItem)
			end repeat
		end if
		
		set allShapes to every shape
		if allShapes is not {} then
			repeat with thisShape in allShapes
				my handleIt(a reference to object text of thisShape)
			end repeat
		end if
		
		if document body then
			my handleIt(a reference to body text)
		end if
	end tell # front document
end tell # Pages

#=====

on handleIt(target_object)
	tell application "Pages" to tell front document
		tell target_object
			set body_text to its contents
			
			# Set text enclosed between parentheses in "Helvetica-Bold" and color black
			set theList to my Divide(body_text, "(")
			if (count theList) > 1 then
				set off7 to (count item 1 of theList) + 2
				repeat with j from 2 to count theList
					tell me to set off7end to off7 + (offset of ")" in theList's item j) - 2
					
					set (font of characters off7 thru off7end) to "Helvetica-Bold"
					set (color of characters off7 thru off7end) to "black"
					
					set off7 to off7 + (count item j of theList) + 1
				end repeat
			end if
			
			# Use the same code to treat text enclosed into brackets
			set theList to my Divide(body_text, "[")
			if (count theList) > 1 then
				set off7 to (count item 1 of theList) + 2
				repeat with j from 2 to count theList
					tell me to set off7end to off7 + (offset of "]" in theList's item j) - 2
					
					set (font of characters off7 thru off7end) to "Helvetica-BoldOblique"
					set (color of characters off7 thru off7end) to {0, 10 * 256, 85 * 256}
					
					set off7 to off7 + (count item j of theList) + 1
				end repeat
			end if
			
			# Set text enclosed between straight double quotes ” named quote by AppleScript ”
			set theList to my Divide(body_text, quote)
			if (count theList) > 1 then
				set off7 to 0
				#set off7 to (count item 1 of theList) + 2
				repeat with j from 2 to count theList by 2
					set off7 to off7 + (count item (j - 1) of theList) + 2
					set off7end to off7 + (count item j of theList) - 1
					
					set (font of characters off7 thru off7end) to "Helvetica-Oblique"
					
					set off7 to off7end + 1
				end repeat
			end if
			
			# set specific words to some color - Check by paragraph to avoid locking application
			
			set wordlist to {"Missed", "missed"} # Still trying to figure out how to ignore case / use unicode in Object Text
			repeat with ct from 1 to count of wordlist
				set MyWord to item ct of wordlist
				set (color of every word of every paragraph where it is MyWord) to "red"
				set (font of every word of every paragraph where it is MyWord) to "Helvetica-BoldOblique"
			end repeat
			
			set wordlist to {"beat", "Beat"}
			repeat with ct from 1 to count of wordlist
				set MyWord to item ct of wordlist
				set (color of every word of every paragraph where it contains MyWord) to {0, 100 * 256, 0} #dark green
				set (font of every word of every paragraph where it contains MyWord) to "Helvetica-BoldOblique"
			end repeat
			
			
		end tell
	end tell
end handleIt

#===== 2

on Divide(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 Divide

#===== 3 

Yikes thats a lot of code to make some text italics.

lol - well it does a lot more. Here is a trimmed/shortened version, which, could be made even shorter if you are only dealing with body text and not text in other objects

tell application "Pages"
	tell front document
		set allTextItems to every text item
		if allTextItems is not {} then
			repeat with thisTextItem in allTextItems
				my handleIt(a reference to object text of thisTextItem)
			end repeat
		end if
		
		set allShapes to every shape
		if allShapes is not {} then
			repeat with thisShape in allShapes
				my handleIt(a reference to object text of thisShape)
			end repeat
		end if
		
		if document body then
			my handleIt(a reference to body text)
		end if
	end tell # front document
end tell # Pages

#=====

on handleIt(target_object)
	tell application "Pages" to tell front document
		tell target_object
			set body_text to its contents
			# Set text enclosed between straight double quotes ” named quote by AppleScript ”
			set theList to my Divide(body_text, quote)
			if (count theList) > 1 then
				set off7 to 0
				#set off7 to (count item 1 of theList) + 2
				repeat with j from 2 to count theList by 2
					set off7 to off7 + (count item (j - 1) of theList) + 2
					set off7end to off7 + (count item j of theList) - 1
					
					set (font of characters off7 thru off7end) to "Helvetica-Oblique"
					
					set off7 to off7end + 1
				end repeat
			end if
			
		end tell
	end tell
end handleIt

#===== 2

on Divide(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 Divide

#===== 3 

You may enhance the caller code. so that it use only one loop.

tell application "Pages"
	tell front document
		set allTargetItems to every text item & every shape
		
		if allTargetItems is not {} then
			repeat with aTargetItem in allTargetItems
				my handleIt(a reference to object text of aTargetItem)
			end repeat
		end if
		
		if document body then
			my handleIt(a reference to body text)
		end if
	end tell # front document
end tell # Pages

#=====

on handleIt(target_object)
	tell application "Pages" to tell front document
		tell target_object
			set body_text to its contents
			# Set text enclosed between straight double quotes ” named quote by AppleScript ”
			set theList to my Divide(body_text, quote)
			if (count theList) > 1 then
				set off7 to 0
				#set off7 to (count item 1 of theList) + 2
				repeat with j from 2 to count theList by 2
					set off7 to off7 + (count item (j - 1) of theList) + 2
					set off7end to off7 + (count item j of theList) - 1
					
					set (font of characters off7 thru off7end) to "Helvetica-Oblique"
					
					set off7 to off7end + 1
				end repeat
			end if
			
		end tell
	end tell
end handleIt

#===== 2

on Divide(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 Divide

#===== 3 

Yvan KOENIG (VALLAURIS, France) dimanche 3 août 2014 19:59:40