Reputation: 462
I am doing a simple step through messages in my inbox. I want to write out the date to a text file. The date is coming through as "Sunday April 2, 2017 at 12:12:12:. All I want to do is convert this to "4/2/17". I keep getting "April" as the month and cannot coerce it to "4" MM format.
tell application "Mail"
repeat with aMessage in messages of inbox
set sSender to (get aMessage's sender)
set recDate to (date received of aMessage)
set sMonth to month of recDate
set sDate to day of recDate & "/" & month of recDate & "/" & year of recDate
end tell
I have tried using a shell echo, or coercing sMonth to a string or integer. No matter what I keep getting "April" instead of a number and "rich text" instead of string.
I don't mind using a shell command, but I am not good with Linux and I do not want to convert the current date (I need to use the date of a past email). I know I must be missing something simple.
Upvotes: 1
Views: 841
Reputation: 285180
First of all to avoid the terminology confusion (rich text
instead of text
) move the code to create the date string into a handler.
To get 4
from the month
just coerce it to integer
, then coerce all components to text
. To strip the 20
from the year just use only the last two characters.
tell application "Mail"
repeat with aMessage in messages of inbox
set sSender to (get aMessage's sender)
set sDate to my dateString(date received of aMessage)
end repeat
end tell
on dateString(theDate)
tell theDate to set {yr, mn, dy} to {year as text, its month as integer as text, day as text}
return mn & "/" & dy & "/" & text -2 thru -1 of yr -- 4/27/18
end dateString
If you want to add leading zeros to the day and month components use another handler to pad the values
on dateString(theDate)
tell theDate to set {yr, mn, dy} to {year as text, its month as integer as text, day as text}
return pad(mn) & "/" & pad(dy) & "/" & text -2 thru -1 of yr -- 04/27/18
end dateString
on pad(v)
return text -2 thru -1 of ("0" & v)
end pad
Upvotes: 1
Reputation: 3142
This works for me using the latest version of macOS High Sierra
property theShortDate : missing value
tell application "Mail"
repeat with aMessage in messages of inbox
set sSender to (get aMessage's sender)
set recDate to (date received of aMessage) as string
my shortDate(recDate)
log "This E-Mail Was Sent From: " & sSender & " " & "on" & " " & theShortDate
end repeat
end tell
on shortDate(recDate)
set AppleScript's text item delimiters to ","
set theLongDate to recDate
set currentMonth to (word 1 of text item 2 of theLongDate)
set currentDay to (word 2 of text item 2 of theLongDate)
set currentYear to (word 1 of text item 3 of theLongDate)
set monthList to {January, February, March, April, May, June, July, August, September, October, November, December}
repeat with x from 1 to 12
if currentMonth = ((item x of monthList) as string) then
set theRequestNumber to (text -2 thru -1 of ("0" & x))
exit repeat
end if
end repeat
set currentMonth to theRequestNumber
set currentDay to (text -2 thru -1 of ("0" & currentDay))
set theShortDate to (currentMonth & "/" & currentDay & "/" & currentYear) as string
end shortDate
Upvotes: 0