Script to make certain font changes

If I select some text in most any kind of document and want to change the font, I have to right-click on the text, select either Show Fonts or Show Colors and then make my choices. (Except, of course, for Bold or Italic, which have simple keystrokes - Cmd-B or Cmd-I).

How would I write an AppleScript to turn the selected text from whatever it is to Arial 12 point … and a script to turn the selected text, which is in color, to plain black?

Model: MBP 15"
AppleScript: 1.10.7
Browser: Firefox 2.0.0.3
Operating System: Mac OS X (10.4)

Well what app(s) specifically are you trying to do this in? There could be easier ways then GUI scripting (which is what your descirbing) depending on the application in question.

Pretty much everything, but right now the main ones are OmniOutliner, Microsoft Word, DevonThink Pro, Apple Mail and TextEdit.

Hi

This is a textedit script which does most of the things you want.

tell application "TextEdit"
	set D to document 1
	set C to (count of paragraphs of text of D)
	repeat with i from 1 to C
		set the color of word 1 of paragraph i of text of D to {0, 0, 0}--black
		set size of paragraph i of text of D to 12
		set font of paragraph i of text of D to "Arial"
	end repeat
end tell

you can alter it to suit your needs or see if any thing in there works in other apps

Hi Pidge, and thanks for the quick response.

One question: how would I rewrite this script to apply to selected text only? In other words, I’d highlight the text I wanted changed, and then run a script to change font or color for just that text, not the whole document.

Thanks very much.

/rb

Hi Rog

Not sure it can be done by selection (in textedit anyway)
will there be anything unique about the line of text you wish to change, you may have to reference it
by word, paragraph, character etc.

Unfortunately, there’s no universal method of achieving this kind of thing. The environment, scripting support and GUI structure of each application can vary significantly - so each approach (if a viable one exists) will probably be quite different.

On the specific question of performing such an operation in TextEdit, the solution isn’t that obvious. However, with a little juggling between the application itself and System Events, it is possible…

To change the font and size of the selection:

tell application "System Events" to set {b, e} to value of attribute "AXSelectedTextRange" of ¬
	text area 1 of scroll area 1 of front window of application process "TextEdit"
if e ≥ b then tell application "TextEdit" to tell front document's characters b thru e to set {font, size} to {"Arial", 12}

Similarly, to change the selection’s color:

tell application "System Events" to set {b, e} to value of attribute "AXSelectedTextRange" of ¬
	text area 1 of scroll area 1 of front window of application process "TextEdit"
if e ≥ b then tell application "TextEdit" to set color of front document's characters b thru e to {0, 0, 0}

Thank you very much. I’ll give that a try.