Skip to content

Instantly share code, notes, and snippets.

@dannysmith
Created June 28, 2013 21:00
Show Gist options
  • Select an option

  • Save dannysmith/5888072 to your computer and use it in GitHub Desktop.

Select an option

Save dannysmith/5888072 to your computer and use it in GitHub Desktop.
--Fucntions
on add_leading_zeros(this_number, max_leading_zeros)
set the threshold_number to (10 ^ max_leading_zeros) as integer
if this_number is less than the threshold_number then
set the leading_zeros to ""
set the digit_count to the length of ((this_number div 1) as string)
set the character_count to (max_leading_zeros + 1) - digit_count
repeat character_count times
set the leading_zeros to (the leading_zeros & "0") as string
end repeat
return (leading_zeros & (this_number as text)) as string
else
return this_number as text
end if
end add_leading_zeros
--Build Year String
set y to year of (current date)
set d to my add_leading_zeros(day of (current date), 1)
set m to my add_leading_zeros(month of (current date) as integer, 1)
set myDate to y & m & d as string
tell application "Mail"
--Get the selected message and activate mail.
set theSelection to selection
if theSelection is {} then return
activate
-- TODO: Stop looping, just get the first message in theSelection
repeat with thisMessage in theSelection
set mySubject to subject of thisMessage
set legalPrefixCharacters to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
set legalSuffixCharacters to {"-U", "-P", "-PS", "-PC", "-PD", "-R", "-S", "-TS", "9", "0"}
--Get the prefix eg "20130101" and the suffix eg "-U".
if length of mySubject > 8 then
set thePrefix to (characters 1 thru 8) of mySubject as rich text
set hasMODPrefix to true
--Check that the prefix is all numbers
repeat with thisChr from 1 to (get count of characters in thePrefix)
set theChr to character thisChr of thePrefix
if theChr is not in legalPrefixCharacters then
set hasMODPrefix to false
end if
end repeat
else
set hasMODPrefix to false
end if
--If the prefix is there, get the rest of the message and put the new prefix date in it.
--Otherwise use the whole message and add the new prefix date.
if hasMODPrefix is true then
set newSubject to (myDate & "-Re: " & (characters 10 thru -1) of mySubject as rich text)
else
set newSubject to myDate & "-Re: " & mySubject
end if
set theSuffix to (characters -1 thru -2) of newSubject as rich text
--If the suffix is not there, add it
if theSuffix is not in legalSuffixCharacters then
set newSubject to newSubject & "-U"
end if
--Open a reply and set the subject
set theOutgoingMessage to reply thisMessage with opening window
set subject of theOutgoingMessage to newSubject
end repeat
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment