My “simple” objective is to use a keyboard shortcut to change the color of a block of highlighted text in a TextEdit document to (for example) “blue”.
I have tried to create a simple Applescript to set the font of highlighted text (“the selection”) to “blue” but I kept getting type reference errors until I opened a new document, pasted the highlighted text into it, changed the color, then copied the text back into the original document. And now, if I try to automate this script with a keyboard shortcut it fails,
though when the Script Editor is open and I click on “Run” it works. How come? Any way [1] make it work with a keyboard shortcut and [2] avoid creating a new/temp document?
{see code snippet A}
I have also tried automating the color picker: can use System Events to control the keystrokes to bring up the color picker but then I am lost: I don’y know how to get System Events or keystrokes or even standard commands to set a specified color.
{see code snippet B}
Any help or hints would be vastly appreciated.
– code snippet A –
-- Code: Change Highlighted Text to Blue
-- Andrew White 3 Jan 2007
-- uses a 2nd document to make the change & copy back
--
set theText to "" as string
property paraColor : {0, 65000, 0}
-- copy the selected text to clipboard
tell application "TextEdit"
	activate
	tell application "System Events"
		tell process "TextEdit"
			keystroke "c" using {command down}
		end tell
	end tell
	
	--set theText to the clipboard as text
	set theText to (the clipboard)
	
	-- paste to new document
	set newDoc to (make new document at beginning of documents)
	set the text of document 1 to (the clipboard)
	
	-- color the text of the entire new document
	tell the text of document 1
		-- set the color of every word to {0, 65535, 0} -- green
		--set the color of every word to {65535, 0, 0} -- red
		set the color of every word to {0, 0, 65535} -- blue
	end tell
	
	-- copy the colored text to clipboard	
	--set the clipboard to the text of document 1
	
	--set text of document 1 to (the clipboard)
	tell application "System Events"
		tell process "TextEdit"
			keystroke "a" using {command down}
			--delay 1
			keystroke "c" using {command down}
		end tell
	end tell
	
	-- close the temp document
	close document 1 saving no
	
	-- temp: paste to new document
	-- set newDoc to (make new document at beginning of documents)
	
	
	-- paste to original document (hopefully still highlighted selection)
	tell application "System Events"
		tell process "TextEdit"
			keystroke "v" using {command down}
		end tell
	end tell
	
end tell
– code snippet B –
tell application "TextEdit" to activate
tell application "System Events"
	tell process "TextEdit"
              -- this just pops up the color picker
		keystroke "c" using {command down, shift down}
              -- but how to make a given color "selected" ?
              -- and if selected will it change the highlighted text color?
	end tell
end tell



 anwhite03:
 anwhite03:
 As with so many things, the trick is to break it down into more easily digestible chunks ” exactly as you’ve been doing.
 As with so many things, the trick is to break it down into more easily digestible chunks ” exactly as you’ve been doing. kai:
 kai: