Skip to content

Instantly share code, notes, and snippets.

@dannysmith
Created June 28, 2013 20:43
Show Gist options
  • Select an option

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

Select an option

Save dannysmith/5887954 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
activate
tell application "System Events" to tell process "Mail"
tell text field "Subject:" of window 1
set focused to true
set mySubject to value
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 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 & "-" & (characters 10 thru -1) of mySubject as text)
else
set newSubject to myDate & "-" & mySubject
end if
set theSuffix to (characters -1 thru -2) of newSubject as text
--If the suffix is not there, add it
if theSuffix is not in legalSuffixCharacters then
set newSubject to newSubject & "-U"
end if
set value to newSubject
end tell
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment