Thursday, December 13, 2018

#1 2018-12-03 02:06:28 am

tempel
Member
From:: Germany
Registered: 2010-11-19
Posts: 68
Website

Calculate business (work) days, exclude national / regional holidays

I need to determine the date from now, adding 10 working days. Working days are, in my case, Mon-Fri, plus any regional holidays (christmas etc.). My Calendar app has included a calendar that defines those holidays.

Now I wonder if I can script Calendar.app to calculate these date offsets. I'd first take the start date (e.g. today's) and add a day, checking whether it's Mon-Fri. If so, I count these days until my limit has been reached. I also have to check whether it's a holiday. So I'd get the holidays calender and see if that day contains an event on that day. But how do I do that?

I've looked at its dictionary, and browsed it with Script Debugger.

I found that when I run this code:

Applescript:

tell application id "com.apple.iCal" -- Calendar
   tell its calendar "National Holidays"
       tell its event 2
           start date
       end tell
   end tell
end tell

Then it takes SEVERAL SECONDS to get my the result. The particular calendar has 800 entries (holidays for several years in advance). This is on 10.12.6. Did not try later macOS versions, yet.

Can this be optimized? Or is this a known issue with the extreme slowness here?

Offline

 

#2 2018-12-03 02:48:10 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11526
Website

Re: Calculate business (work) days, exclude national / regional holidays

Scripting Calendar is indeed very slow.

Have a look at CalendarLib EC v1.1.3 on https://www.macosxautomation.com/apples … _Libs.html.

The library uses the EventKit framework which is much more efficient


regards

Stefan

Offline

 

#3 2018-12-03 07:00:45 am

tempel
Member
From:: Germany
Registered: 2010-11-19
Posts: 68
Website

Re: Calculate business (work) days, exclude national / regional holidays

Ah, thanks.

I have solved my task differently now, though: As the app that requires this is written in Xojo, I found that there's an MBS plugin for accessing the classic Calendar API, and that is fast, and even lets me query all Events within a date range, which is perfect for my needs.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)