• 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: Sorting list of calendar events
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sorting list of calendar events


  • Subject: Re: Sorting list of calendar events
  • From: Shane Stanley <email@hidden>
  • Date: Fri, 17 Apr 2015 12:46:25 +1000

On 16 Apr 2015, at 9:50 pm, Shane Stanley <email@hidden> wrote:

I posted a basic script for it here a while back

But there was an issue with it in terms of prompting the user to give the script access to Calendars. This version fixes that, so if Alex is still around, he might want to modify his code accordingly.

The only advantages of this method over something like Christian's approach are that it includes recurring events, it doesn't require Calendars to be running, and it's considerably faster. The disadvantages are that it's a bit more complex, and it requires Mavericks or Yosemite.

use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"
use framework "EventKit"

its titleOfTodaysEventsForCalendarsNamed:{"Work", "Home"}

on titleOfTodaysEventsForCalendarsNamed:listOfCalNames
-- create event store and get the OK to access Calendars
set theEKEventStore to current application's EKEventStore's alloc()'s init()
theEKEventStore's requestAccessToEntityType:0 completion:(missing value)


-- check if app has access; this will still occur the first time you OK authorization
set authorizationStatus to current application's EKEventStore's authorizationStatusForEntityType:0 -- work around enum bug
if authorizationStatus is not 3 then
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 if


-- get calendars that can store events
set theCalendars to theEKEventStore's calendarsForEntityType:0
-- get the calendars whose title is in the passed list of names
set theNSPredicate to current application's NSPredicate's predicateWithFormat_("title IN %@", listOfCalNames)
set calsToSearch to theCalendars's filteredArrayUsingPredicate:theNSPredicate


-- create start date and end date for occurences
set nowDate to current application's NSDate's |date|()
-- get start of day then end of day
set startNSDate to current application's NSCalendar's currentCalendar()'s startOfDayForDate:nowDate
set endNSDate to current application's NSDate's dateWithTimeInterval:(1 * days - 1) sinceDate:startNSDate


-- find matching events
set thePred to theEKEventStore's predicateForEventsWithStartDate:startNSDate endDate:endNSDate calendars:calsToSearch
set theEvents to (theEKEventStore's eventsMatchingPredicate:thePred)
-- sort by date
set theEvents to theEvents's sortedArrayUsingSelector:"compareStartDateWithEvent:"
-- return title property, called description in AS
return (theEvents's valueForKey:"title") as list
end titleOfTodaysEventsForCalendarsNamed:

-- 
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

  • Follow-Ups:
    • Re: Sorting list of calendar events
      • From: Jan Erik Moström <email@hidden>
References: 
 >Sorting list of calendar events (From: Jan Erik Moström <email@hidden>)
 >Re: Sorting list of calendar events (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: sqlite question
  • Next by Date: Re: Why does opening any script editor open many other apps?
  • Previous by thread: Re: Sorting list of calendar events
  • Next by thread: Re: Sorting list of calendar events
  • Index(es):
    • Date
    • Thread