How to create Automator Service to change font of selected text?

Solution 1:

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

Solution 2:

After much research, the workflow below works for me in TextEdit, Mail, Notes, Stickies. Where it doesn't work is when the app uses Styles (Microsoft Word, Pages, Keynote), because the style overrides the font you input. The only way I can think to override the style is to directly script each app to make a custom style.

Force Selected Font Workflow

ApplesScript Code:

on run {input, parameters}

    do shell script "pbpaste | textutil -stdin -stdout -convert rtf -font Arial -fontsize 12 | pbcopy"
    tell application "System Events" to keystroke "v" using command down

    return input
end run