Re: Getting the date of the next or current recurrence of an iCal/Calendar event
Re: Getting the date of the next or current recurrence of an iCal/Calendar event
- Subject: Re: Getting the date of the next or current recurrence of an iCal/Calendar event
- From: Nigel Garvey <email@hidden>
- Date: Tue, 29 Mar 2016 20:32:21 +0100
Anthony Fedd wrote on Tue, 29 Mar 2016 14:46:47 +0000 (GMT):
>I am successfully using a Nigel Garvey script to find and announce the
>birthday or anniversary of death of my calendar's recurring events every
>morning! It takes about 4-5 minutes to actually announce the day's
events,
>if any, following my preceding "Good morning" message.
[snip]
> I saw that in
>an exchange with member StefanK, an Objective-C framework referenced.
Not
>that I'm familiar with it, but is that something that can be applied to
>speed up the script?
The script below's actually by Shane (except for the 'say' stuff), but
I've been fooling around with it to try to arrive at a style which suits
me. It returns the summaries of any events occurring on the day its run
in the calendars whose names are passed to it. It includes recurring
events and events in progress over the period.
use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"
use framework "EventKit"
set theSummaries to todaysEventSummaries({"Birthdays etc.", "Home"}) -- Your calendar name(s) here
if (theSummaries is {}) then
say "Go back to sleep."
else
repeat with thisSummary in theSummaries
say thisSummary
delay 0.5
end repeat
end if
on todaysEventSummaries(listOfCalNames)
set listOfCalNames to listOfCalNames as list
-- create event store and get the OK to access calendars
tell current application's EKEventStore
set theEKEventStore to its alloc()'s init()
tell theEKEventStore to requestAccessToEntityType:0 completion:(missing value)
-- check if app has access; this will still occur the first time you OK authorization
set authorizationStatus to (its authorizationStatusForEntityType:0) -- work around enum bug
end tell
if (authorizationStatus is not 3) then getAuthorisation()
-- create start date and end date (start of day and end of day) for today's occurrences.
-- The original used NSDate's |date|() and NSCalendar()'s startOfDayForDate:, but this is faster and more legible.
tell (current date) to set startASDate to it - (its time)
tell current application's NSDate
set startNSDate to (its dateWithTimeInterval:0 sinceDate:startASDate)
set endNSDate to (its dateWithTimeInterval:0 sinceDate:(startASDate + (days - 1)))
end tell
-- Get a predicate meaning calendars whose "title" (name) is in the passed list of names
tell current application's NSPredicate to ¬
set theNSPredicate to its predicateWithFormat_("title IN %@", listOfCalNames)
tell theEKEventStore
-- get calendars that can store events and which conform to the predicate.
set calsToSearch to (its calendarsForEntityType:0)'s filteredArrayUsingPredicate:theNSPredicate
-- Get a predicate meaning events in them which occur or are in progress between the start and end dates.
set thePred to (its predicateForEventsWithStartDate:startNSDate endDate:endNSDate calendars:calsToSearch)
-- Get the events, sort them by start date, and return their "titles" (summaries).
tell (its eventsMatchingPredicate:thePred) to ¬
tell (its sortedArrayUsingSelector:"compareStartDateWithEvent:") to ¬
set theSummaries to (its valueForKey:"title") as list
end tell
return theSummaries
end todaysEventSummaries
on getAuthorisation()
display dialog "Access must be given in System Preferences" & linefeed & "-> Security & Privacy first." buttons {"OK"} default button 1
tell application "System Preferences"
activate
tell pane id "com.apple.preference.security" to reveal anchor "Privacy"
end tell
error number -128
end getAuthorisation
NG
_______________________________________________
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