Skip to content

Instantly share code, notes, and snippets.

@benwaldie
Created April 5, 2013 16:45
Show Gist options
  • Save benwaldie/5320770 to your computer and use it in GitHub Desktop.
Save benwaldie/5320770 to your computer and use it in GitHub Desktop.
AppleScripting Email > Developing a Quoted Reply Service
-- This handler is triggered by Automator. The input and parameters variables are populated by Automator. Input contains the selected text passed to the workflow for processing.
on run {input, parameters}
-- Make sure the input is a string
set theText to input as string
-- Determine the maximum length for each line. Note that some lines may be longer, if needed to prevent words from breaking
set theMaxLineLength to 42
-- Determine the prefix for each line
set theLinePrefix to "> "
set theQuotedText to theLinePrefix
-- Loop through the paragraphs of the text
repeat with a from 1 to (count paragraphs of theText)
set thePar to paragraph a of theText
-- If the current paragraph is longer than the maximum length, then split it into separate lines
if (length of thePar) is greater than theMaxLineLength then
set breakLine to false
repeat with b from 1 to (length of thePar)
set theChar to character b of thePar
set theQuotedText to theQuotedText & theChar
if b mod theMaxLineLength = 0 then set breakLine to true
if breakLine = true then
if {tab, space, ".", ",", "?", "!", "\"", "(", ")"} contains theChar then
set theQuotedText to theQuotedText & return & "> "
set breakLine to false
end if
end if
end repeat
-- If the current paragraph is not longer than the maximum length, then append the text
else
set theQuotedText to theQuotedText & thePar
end if
-- Add a return and prefix
if a is not equal to (count paragraphs of theText) then set theQuotedText to theQuotedText & return & "> "
end repeat
-- Return the processed text
return theQuotedText
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment