• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Script for ical event via mail rules
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Script for ical event via mail rules


  • Subject: Re: Script for ical event via mail rules
  • From: Shane Stanley <email@hidden>
  • Date: Thu, 08 Oct 2015 11:41:06 +1100

On 8 Oct 2015, at 11:09 AM, Jean-Christophe Helary <email@hidden> wrote:

I'm guessing the main issue here will be to identify/parse/validate the date in the mail.

To avoid that part I'd simply use a dialog that asks me to input a date

A dialog to confirm it might be prudent, but why avoid trying to find it? That's what data detectors are for:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set theDate to my getDatesIn:"Some text containing Nov 5, for example"
considering numeric strings
if AppleScript's version < "2.5" then
set theDate to my makeASDateFrom:theDate
else
set theDate to theDate as date
end if
end considering

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|()}
if theMatch = missing value then error "No date found"
-- get the date property of the NSTextCheckingResult
set theDate to theMatch's |date|()
return theDate
end getDatesIn:

-- required before 10.11
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:


-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >Script for ical event via mail rules (From: Seamus Robertson <email@hidden>)
 >Re: Script for ical event via mail rules (From: Jean-Christophe Helary <email@hidden>)

  • Prev by Date: Re: Script for ical event via mail rules
  • Next by Date: can’t make current application into type string
  • Previous by thread: Re: Script for ical event via mail rules
  • Next by thread: can’t make current application into type string
  • Index(es):
    • Date
    • Thread