Saturday, January 19, 2019

#1 2019-01-06 07:00:32 pm

akim
Member
Registered: 2010-04-04
Posts: 89

Script EventKit to register a new Calendar event

With the help of Shane and others, I have assembled the following script that creates a new calendar event,  and extracts a  unique event identifier, referencing that event to insert into a  Filemaker database.

I activate this applescript, after I have manually created a new event in the Calendar application, via that application's user interface.

When activated, the Apple_ICal_Selected_Event objectForKey function returns an identifier.

However, unless I click on Application Calendar's window outside of that new event, prior to activating this script, theEKEventStore's eventWithIdentifier:Apple_ICal_Selected_Event will return no value. As such the applescript fails to return a unique  EventKit event identifier of that Calendar EKEvent.

Applescript:

use scripting additions
use framework "Foundation"
use framework "EventKit"

set theEKEventStore to my initializeEventStore()
set NSEKEvent to missing value

set Apple_ICal_Selected_Event to (((current application's NSUserDefaults's alloc()'s ¬
   initWithSuiteName:"com.apple.iCal")'s ¬
   objectForKey:"SelectedEvents")'s ¬
   objectForKey:"iCal")'s ¬
   firstObject()
# get theEKEventStore's database reference to a specific event by means of a localUID
set NSEKEvent to theEKEventStore's eventWithIdentifier:Apple_ICal_Selected_Event

set {theResult, theError} to theEKEventStore's saveEvent:NSEKEvent span:0 commit:true |error|:(reference)


on initializeEventStore()
   # Instatiate an EventKit Event Store to store a database of events
   set theEKEventStore to current application's EKEventStore's alloc()'s init()
   
   # request access for theEKEventStore to access Calendars
   theEKEventStore's requestAccessToEntityType:0 completion:(missing value)
   
   # obtain event store authorization status in regard to security permissions
   my authorizationStatus() --abort if no clearance; continue if authorizationStatusForEntityType is 3
   theEKEventStore
end initializeEventStore

on authorizationStatus()
   # check if app has access and 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
end authorizationStatus

How can I script EventKit or Application "Calendar" to  cause EventKit to register that new event, without having to manually click on Calendar's window outside of that event field?

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)