Monday, February 22, 2010

Quote html for MarsEdit

I am using MarsEdit to post my blog entries, which is a nice editor (ok, it could sometimes have a more wysiwig mode, but in total I am happy with it).

One thing that I find often annoying or missing when writing posts is that there is no build in quotation mechanism for '<' in a <pre> block (many of my posts are rather technical and contain XML snippets).

Luckily MarsEdit has AppleScript support, so I wrote this little AppleScript script (actually I copied a good part from the supplied Add Paragraph Tags one):

tell application "MarsEdit"
set currentWindow to document 1
on error errorMessage
displayErrorMessage("Can’t add paragraph tags because no post windows are open. This script works on text in the frontmost post window.") of me
end try

set currentTextView to selected text in currentWindow
set numChars to number of (characters in currentTextView)
set newBodyText to ""

repeat with i from 1 to numChars

set currentChar to character i in currentTextView

if (currentChar is "<") then
set newBodyText to newBodyText & "&lt;"
set newBodyText to newBodyText & currentChar
end if
end repeat

set (selected text in document 1) to newBodyText
end tell

This allows to quote the current selection. Save the script to
~/Library/Application Support/MarsEdit/Scripts/Quote Html.scpt.
It will then be availably in the ApppleScript Menu:

Bildschirmfoto 2010-02-22 um 09.35.54.png

To use it, just select the text and then invoke this Quote Html menu item

No comments: