Well that worked just fine, even with the time string from the OP.
It errors on the mm/dd, which is better than giving an incorrect result.
All the other date strings worked.
"--Sunny September 4 --> Friday, September 4, 2015 at 12:00:00 pm
--Fri., September 1 --> Tuesday, September 1, 2015 at 12:00:00 pm
--Fri. Sept. 2 --> Wednesday, September 2, 2015 at 12:00:00 pm
--Fri Sep 3 --> Thursday, September 3, 2015 at 12:00:00 pm
--Sep. 4 --> Friday, September 4, 2015 at 12:00:00 pm
--Sept. 9 --> Wednesday, September 9, 2015 at 12:00:00 pm
--9/8 --> Error --> missing value doesn’t understand the “date” message.
--9/7/15 --> Monday, September 7, 2015 at 12:00:00 pm
--09/06/2015 --> Sunday, September 6, 2015 at 12:00:00 pm
--9/5/2015 --> Saturday, September 5, 2015 at 12:00:00 pm
--10:11 --> Saturday, August 22, 2015 at 10:11:00 am
--13:11 --> Saturday, August 22, 2015 at 1:11:00 pm
--10:11 am --> Saturday, August 22, 2015 at 10:11:00 am
--13:11 am --> Saturday, August 22, 2015 at 1:11:00 pm"
Tinkering with some various date and time and location preferences seems to work for all but the mm/yy versions.
I wonder how well it works for our European friends.
Also, combining the two handlers into one seems to work.
Thanks, it looks like this solves a years-long issue.
------------
use
framework "Foundation"
use scripting additions
-- required for 'current date'
local thisDate
on getDatesIn:aString
-- convert string to Cocoa string
set
anNSString to
current application's
NSString's stringWithString:aString
-- create data detector
set
theDetector to
current application's
NSDataDetector's dataDetectorWithTypes:(current application's
NSTextCheckingTypeDate) |error|:(missing value)
-- find first match in string; returns an NSTextCheckingResult object
set
theMatch to theDetector's
firstMatchInString:anNSString
options:0 range:{0,
anNSString's |length|()}
-- get the date property of the NSTextCheckingResult
set
theDate to theMatch's
|date|()
return
theDate
end getDatesIn:
on makeASDateFrom:theNSDate
set
theCalendar to
current application's
NSCalendar's currentCalendar()
set
comps to theCalendar's
componentsInTimeZone:(missing value)
fromDate:theNSDate
-- 'missing value' means current time zone
tell (current date)
to set {theASDate,
year,
day, its month,
day,
time} to ¬
{it,
comps's |year|(), 1,
comps's |month|(),
comps's |day|(), (comps's
hour()) * hours + (comps's
minute()) * minutes + (comps's
|second|())}
return
theASDate
end makeASDateFrom:
set
dateStrings to "Sunny September 4
Fri., September 1
Fri. Sept. 2
Fri Sep 3
Sep. 4
Sept. 9
9/8
9/7/15
09/06/2015
9/5/2015
10:11
13:11
10:11 am
13:11 am"
set stringsAndDates
to {}
repeat
with thisDate in
paragraphs
of dateStrings
try
set
thisDate to thisDate
as text
set
theDate to (my
getDatesIn:thisDate)
set
theASDate to (my
makeASDateFrom:theDate)
set
the end of
stringsAndDates to "--" &
thisDate & " --> " & theASDate
as text
on
error errMsg
number errNum
set
the end of
stringsAndDates to "--" &
thisDate & " --> " & "Error --> " &
errMsg
end
try
end
repeat
set AppleScript's
text item delimiters to {return}
return stringsAndDates
as text