Getting Calendar events using AppleScript is delayed in retrieval

I’m a newbie. I use this script to update my word document by finding the next created appointment in exchange calendar. It kinda works - but there seems to be a long delay between updating an event on exchange calendar and this script being able to detect it. So when I create a new event in busycal - this event appears just fine in iCal or Outlook - to me, meaning that the event is created in the exchange server, and seen by other programs synced to exchange server. But when I run the script, it cannot find it - until like sometime 15 min later or more. I’m unclear why. Is there an explicit command to update / refresh “store”?

Thanks!
Kenneth

use AppleScript version "2.4"
use script "CalendarLib EC" version "1.1.3" -- put this at the top of your scripts
use scripting additions
use framework "Foundation"
use framework "AppKit"

set NextAppt to ""
set PtName to ""

tell application "Finder"
	set PtName to name of front window
end tell

set Pos1 to offset of "(PtNum" in PtName
set Pos2 to offset of ")" in PtName
set PtNumSearch to characters Pos1 thru Pos2 of PtName as string
set PtPtNumSearch to "*" & PtNumSearch & "*" as string

set theStartDate to (current date)
set theStartDate to theStartDate + (1 * days)
set hours of theStartDate to 0
set minutes of theStartDate to 0
set seconds of theStartDate to 0
set theEndDate to theStartDate + (540 * days)

set theStore to fetch store -- get the event store, which you need below
set theCalendars to fetch calendars "Calendar" event store theStore -- get the chosen calendars
set theEvents to fetch events starting date theStartDate ending date theEndDate searching cals theCalendars event store theStore -- get the events
set theEvents to filter events by pattern event list theEvents event summary PtPtNumSearch without using regex

if (count theEvents) is not 0 then
	
	display dialog "Found event."
	
	
	repeat with nextEvent in theEvents
		
		set theInfo to event info for event nextEvent
		
		if (event_summary of theInfo does not contain "(can)") and (event_summary of theInfo does not contain "(information only)") then
			set NextAppt to (event_start_date of theInfo) as string
			set len to length of NextAppt
			set NextApttEnd to characters (len - 2) thru len of NextAppt as string
			set NextAppt to (characters 1 thru (len - 6) of NextAppt) & NextApttEnd as string
			exit repeat
		end if
		
	end repeat
	
	tell application "Microsoft Word"
		activate
		
		if name of active document contains PtName then
			
			set ContentToFind to "ddddd" as string
			
			set findRange to find object of selection
			tell findRange
				set forward to false
				clear formatting
				clear formatting its replacement
				set content to ContentToFind
				set content of its replacement to NextAppt
				execute find wrap find find continue ¬
					replace replace one with match forward
			end tell
			
		end if
	end tell
	
	
	
else
	display dialog "Next appointment not found."
end if

I’m a bit surprised at that behavior, but there is a method you could try. After you call fetch store you could insert:

theStore's refreshSourcesIfNecessary()

Let us know if that makes a difference.

Thanks! I think it made a difference. There’s still kinda of a slight lag - but much better it seems so far.