我正在简单地浏览收件箱中的邮件。我想把日期写到文本文件中。该日期将于2017年4月2日星期日12:12:12发布。我想要做的就是将其转换为" 4/2/17"。我一直在" April"作为月份,不能强迫它到" 4" MM格式。
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
我尝试过使用shell echo,或者将sMonth强制转换为字符串或整数。无论我持续得到什么" April"而不是数字和#34;富文本"而不是字符串。
我不介意使用shell命令,但我不熟悉Linux,我不想转换当前日期(我需要使用过去电子邮件的日期)。我知道我必须遗漏一些简单的东西。
答案 0 :(得分:1)
首先要避免术语混淆(rich text
而不是text
)移动代码以将日期字符串创建为处理程序。
要从4
获取month
,只需将其强制转换为integer
,然后将所有组件强制转移到text
。要从年份中删除20
,只需使用最后两个字符。
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
如果要向日期和月份组件添加前导零,请使用另一个处理程序填充值
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
答案 1 :(得分:0)
这适用于我使用最新版本的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