Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I write a lot of technical emails, and like to use a monospaced font for the code bits. Highlight text, hit apple-T, choose my font from the list of favorites, then close the font window. Is there a way to use a keyboard shortcut to apply a given font to the currently selected text? Ideally this would be OS-wide, but if that's not possible I can use it for just a single app (Mail.app)

share|improve this question
    
What version? ⌘-T works in Mail 3.6. –  trashgod Jul 22 '11 at 2:22
    
@trashgod I'm looking for a single keyboard shortcut that I can use to set the font of the current selection to a specified font. Probably some sort of macro. –  Sam Barnum Jul 22 '11 at 12:13
    
This seems fairly trivial to do with AppleScript and then just assign a key to that script. If someone doesn't come by tonight I will whip something up tomorrow. –  lemonginger Sep 16 '11 at 22:46
add comment

migrated from stackoverflow.com Sep 16 '11 at 10:17

This question came from our site for professional and enthusiast programmers.

2 Answers

I don't know of a system-wide way to script the Fonts panel but here's a few ways to get quick access to monospace fonts in Mail.app

Way 1: Plan Text Message

Go to Mail.app's preferences > Fonts & Colors and check the checkbox for "Use fixed-width font for plain text messages"

enter image description here

Now when you're in a message where you want to use a monospace font you can simply hit: Command-Shift-T and you can toggle between the fixed-width font of your choice and your standard font (plain text message & rich text).

The downside of this method is if the receiver has not set their font to a fixed-width font they may not see the message as intended.

Way 2: Set the Font with Applescript

The second way to do this would be to create a new message with the correct font chosen using Applescript. You can do that like this:

tell application "Mail"
    set outgoingMessage to make new outgoing message
    set fontFace to "Times"
    tell outgoingMessage
        set the content to " "
        set font of content to fontFace
        set size of content to (14)
        set visible to true
    end tell
end tell

You do have to set the content to something with this script. It seems Mail doesn't let you set the font for an empty message.

Way 3: Use TextExpander

This way costs money but it might give you what you are after. You could setup a couple of TextExpander ($34.99 USD on the app store) shortcuts for fixed width and normal type so that you can switch back and forth by typing a few characters.

TextExpander

TextExpander will allow you to insert rich text (text with a font, color, style, etc set). Just create a snippet of type Formatted Text, Pictures and set a font for the text you'll be inserting. You will need to include at least a space character for this strategy to work.

You will need your snippits to include at least a space character to ensure that the font will take.

share|improve this answer
    
Samuel, I'm looking for a way to just make the highlighted text monospaced, not the entire message. I'm okay with using an Applescript that only affects the selection of the current app. Can you recommend a way to trigger the applescript with a custom keyboard shortcut? –  Sam Barnum Sep 19 '11 at 15:02
    
Sam, the trouble is Mail.app doesn't have an object for the selected text. I spent some time trying to figure out a way and I can say there is no clean way to accomplish this. –  Samuel Mikel Bowles Sep 19 '11 at 17:21
2  
With TextExpander, you can have it insert the contents of the clipboard into the monospace-formatted snippet. So when you highlight, you just type ⌘X and then your snippet abbreviation to paste it back in as monospace. Add a space with regular formatting to the end of the snippet and you're good to keep typing. TextExpander is otherwise crazily useful; everyone who uses a Mac to get things done should own it anyway. –  75th Trombone Sep 19 '11 at 21:32
add comment

Copy a character with a specific style:

echo "<span style=\"font:14px 'Monaco'\">&nbsp;</span>" | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf

pbcopy -Prefer rtf doesn't include plain text data, so paste will only work in rich text views.


Insert a character with a specific style:

try
    try
        set old to the clipboard
    end try
    set html to quoted form of "<span style=\"font:14px 'Monaco'\">&nbsp;</span>"
    do shell script "echo " & html & " | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
    tell application "System Events" to keystroke "v" using command down
    delay 0.03
    set the clipboard to old
end try

Paste style:

try
    try
        set old to the clipboard
    end try
    tell application "System Events" to keystroke "c" using command down
    delay 0.03
    set input to Unicode text of (the clipboard as record)
    set txt to quoted form of ("<span style=\"font:14px 'Monaco'\">" & input & "</span>")
    do shell script "echo " & txt & " | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
    tell application "System Events" to keystroke "v" using command down
    delay 0.03
    set the clipboard to old
end try
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.