Inserting Date/Time into Automated Email Message


I have found and tested a few clues, but am apparently not using the right coding to insert the date/time into the TEXT of automated emails when a Mac restarts. Yup, I know that an email inherently has this in its header, but sometimes the message is considerably delayed in actually being received and I need an easy way to extract the information into a spreadsheet.

My goal is to have the date/time format like this:

1/1/18, 08:00 AM

Here is what I have so far without any timestamp:

tell application “Mail”
set my_message to make new outgoing message
set subject of my_message to “MacMini Restart Alert”
set content of my_message to “Your MacMini has restarted.”
set sender of my_message to “

tell my_message
make new to recipient at end of to recipients with properties {name:“”}
end tell

send my_message
end tell

Very likely this is an easy answer for those more knowledgable with AppleScript than I. Any help would be appreciated.

Dr. Z.

Simplest is using the Standard Additions command current date.
This will return a full date in the current user’s format, as specified in System Prefs>Language & Region>Advanced:

current date -->date "maandag 8 oktober 2018 11:21:13"

I wouldn’t bother to convert this to something else, unless there’s some external requirement for another format.
Use it like so:

set textDate to (current date) as text-- must have text, not date object

tell application "Mail"
    set content of my_message to textDate & return & "Your MacMini has restarted."
end tell

Hi. Welcome to MacScripter.

If the date/time format you want is similar to the short date and time formats you have set up in the user preferences on your computer, you can get the required string very simply like this:

set now to (current date)
tell now to set textDate to its short date string & ", " & its time string

Otherwise, assuming you’re American and want month/day/year format, without leading zeros or century digits, and the hours and minutes in twelve-hour time with leading zeros, the AppleScript solution’s a bit more involved:

set now to (current date)
-- Set variables to the date's relevant properties.
set {month:m, day:d, year:y, hours:hr, minutes:min} to now
-- For the date, use the month number, the day, and the last two digits of the year.
set dateString to (m as integer as text) & "/" & d & "/" & (y mod 100)
-- Times before 12:00 are AM; those from 12:00 on are PM.
if (hr < 12) then
	set suffix to " AM"
	set suffix to " PM"
end if
-- Convert the hour to twelve-hour time.
set hr to (hr + 11) mod 12 + 1
-- Get a string containing the required digits for the time (including any leading zeros), extract the relevant sections, and insert a colon.
tell (10000 + hr * 100 + min) as text to set timeString to text 2 thru 3 & ":" & text 4 thru 5
-- Put together the final result.
set textDate to dateString & ", " & timeString & suffix

Or if you’re willing to venture into the world of ASObjC:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set now to current application's class "NSDate"'s |date|()
set dateFormatter to current application's class "NSDateFormatter"'s new()
set myLocale to current application's class "NSLocale"'s currentLocale()
tell dateFormatter to setLocale:(myLocale)
tell dateFormatter to setDateFormat:("M/d/yy, hh:mm a")
set textDate to (dateFormatter's stringFromDate:(now)) as text

As in alastor933’s reply, textDate should be worked out before the tell application “Mail” statement.

I was set to post my response and found Nigel’s excellent suggestion, which is the one to use. I decided to go ahead and post my suggestion, which admittedly is a bit of a klduge:

set {year:y, month:m, day:d, hours:h, minutes:min} to (current date)

set theDate to ((m as integer) as text) & "/" & d & "/" & text 3 thru 4 of (y as text)

if h < 1 then
	set theTime to "12:" & text -2 thru -1 of ("0" & min) & " AM"
else if h < 12 then
	set theTime to text -2 thru -1 of ("0" & h) & ":" & text -2 thru -1 of ("0" & min) & " AM"
else if h < 13 then
	set theTime to "12:" & text -2 thru -1 of ("0" & min) & " PM"
	set theTime to ((h - 12) as text) & ":" & text -2 thru -1 of ("0" & min) & " PM"
end if

set theDateTime to theDate & ", " & theTime

Just as an aside, the following thread contains a tremendous amount of information on using dates in Applescript and is absolutely worth a look.

Hi peavine.

I think the first line of your if statement should be:

if h is equal to 0 then -- Not 1

Thanks Nigel. I have corrected that.