How to create Automator Service to change font of selected text

applescriptautomatorservices

I'm trying to create a "Service" in Automator that changes the font of the selected text in the frontmost app.

Best Answer

You could assign a shortcut to a script like this:

tell application "System Events" to tell (process 1 where frontmost is true)
    set {c1, c2} to value of attribute "AXSelectedTextRange" of text area 1 of scroll area 1 of window 1
end tell
tell application (path to frontmost application as text) to tell document 1
    tell characters c1 thru c2
        set font to "Menlo"
        set size to 18
    end tell
end tell

It works with TextEdit and WriteRoom, but it probably needs to be modified for other applications.

Another really awful option:

try
    set old to the clipboard as record
end try
do shell script "textutil -stdin -stdout -format html -convert rtf <<< '<span style=\"font: 18px Menlo\">a</span>' | pbcopy"
tell application "System Events"
    keystroke "v" using command down
    keystroke "c" using {option down, command down}
    keystroke "z" using command down
    keystroke "v" using {option down, command down}
end tell
delay 0.05
try
    set the clipboard to old
end try