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'm a developer, and I'd like to be able to distinguish blocks of code from regular text, when I write emails.

Basically I wanna be able to do this. In the Mail app, (or Sparrow!).

Is there a way to do this with a shortcut, or macro, etc so that I don't have to reach for the mouse every time and change like 20 font attributes?

share|improve this question

3 Answers 3

up vote 13 down vote accepted

The magic behind the formatting on StackExchange is a great perl script called Markdown. Download it, and double-click the resulting Markdown_1.0.1.zip file.

Then, in terminal, type

chmod a+x ~/Downloads/Markdown_1.0.1/Markdown.pl
sudo cp ~/Downloads/Markdown_1.0.1/Markdown.pl /usr/local/bin/

Once you have that done, you can create a Service in Automator.

The service you create receives rich text in any application. Uncheck the box that says Output replaces selected text.

Your service will contain two actions:

The first action is Run Shell Script. Here is the shell script:

/usr/local/bin/Markdown.pl | /usr/bin/textutil -stdin -stdout -format html -convert rtf | /usr/bin/pbcopy

Your section action is Run AppleScript. Here is the AppleScript:

tell application "System Events"
    keystroke "v" using {command down}
end tell

Save the service, and, if you wish, assign it a keyboard shortcut using System Preferences » Keyboard » Keyboard Shortcuts » Services

Then, you can type text using the formatting codes you use on this site, and run this service to generate formatted text. This will only work if your mail messages are set up as Rich Text -- if you have plain text mail messages, running this service will simply erase the contents of your message, as your rich text formatted by the service will not be able to be pasted.

share|improve this answer
1  
Cleaner than "rendering" the HTML in Safari. I like it. –  jaberg Mar 19 '12 at 0:25
    
Sadly, this still doesn't work for me. I now get "Failed to write output" and again it looks like it's caused by the second part (pasting). I'm running 10.6.8 with Automator 2.1.1. I'm guessing it's a versioning issue. –  whitman Mar 19 '12 at 13:34
1  
If anyone has debugging issues, we have a room: chat.stackexchange.com/rooms/info/2830/… –  jaberg Mar 19 '12 at 14:35
    
This is brilliant. For those of you who prefer reStructuredText: /path/to/rst2html.py -q --stylesheet=/create/a/style.css --embed-stylesheet | /path/to/textutil -stdin -stdout -format html -convert rtf | pbcopy –  Shabbyrobe Mar 30 '12 at 1:21

Assuming you're comfortable sending HTML mail you could use Markdown to format your email message and create an automator service to convert, render and replace your markdown with formatted text.

ᔥ Ryan Gray: Markdown Compose for Mac OS X Mail

[W]hat I had to do was to save the HTML generated from the Markdown conversion to a temporary file, then open that in Safari, have Safari select all and copy to the clipboard (copying an HTML object), then having Mail paste it – replacing the selected Markdown text. So, the service workflow takes the selected text, but does not itself replace the selection directly.

Instructions for creating this service are provided in the article. In addition to providing an explicit path to the markdown.pl file in your Automator script, be certain that you make markdown.pl an executable file.

chmod a+x your/path/to/markdown.pl

Note: With the demise of Posterous, the original link to Ryan Gray's article broke. I've edited the answer to point to a version archived at The Internet Archive Wayback Machine. ↬Dominique for bringing the broken link to my attention.

share|improve this answer
    
Ryan Gray's article is no longer at the address given now that Posterous is gone. However, Brett Terpstra has an awesome set of OSX services for Markdown! –  Dominique Jun 18 '13 at 13:16
    
Yes, I use Brett's Markdown Services constantly. –  jaberg Jun 27 '13 at 14:58

I use a shell script like this:

echo "<style>
$(cat /Applications/Marked.app/Contents/Resources/swiss.css |
sed 's/&/&amp;/g;s/</\&lt;/g;s/>/\&gt;/g')
</style>
$(cat | pandoc --strict -p)" |
textutil -stdin -stdout -inputencoding UTF-8 -format html -convert rtf |
LC_CTYPE=UTF-8 pbcopy

pandoc --strict -p acts like Markdown.pl and preserves tabs.

-inputencoding UTF-8 is required to preserve non-ASCII characters. LC_CTYPE=UTF-8 is not needed if LC_CTYPE is already set to a UTF-8 locale.

share|improve this answer

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.