The advantages of using CalendarLib rather than Calendar.app are:
You donât need to have Calendar.app running.
Itâs much faster. Hugely so, especially if you have a busy schedule.
When you ask for events that fall between two dates, unlike Calendar.app it includes recurring events.
If you want to change how it works, you can edit it to suit.
The disadvantages are:
You need to instal two libraries, either in a central Script Libraries folder or within the bundle of applets.
It doesnât support all the features of Calendar.app.
Hereâs a sample of how to use it:
use script "CalendarLib" -- put this at the top of your scripts
use scripting additions
-- fetch properties of events for next week
set d1 to current date
set d2 to d1 + 7 * days
set theStore to fetch store
set theCal to fetch calendar "Home" cal type cal cloud event store theStore -- change to suit
set theEvents to fetch events starting date d1 ending date d2 searching cals {theCal} event store theStore
repeat with anEvent in theEvents
log (event info for event anEvent)
log (event identifier for event anEvent)
log (event attendees for event anEvent)
log (event recurrence for event anEvent)
end repeat
Hi Shane,
thanks so much for this, Iâve been trying to create some Calendar operations activated by a mail rule and it was fine scripting the Calendar except I could not delete events. So this alternate way is holding some promise. Iâve tried the CalendarLib script and it works perfectly when used alone in a script and also in a bundle, but when I combine it with âusing terms fromâ and âon perform mail action with messagesâ⊠I run into problems.
For the following script I have saved as a script bundle and installed the CalendarLib and BridgePlus libraries inside the bundle, in the Script:Contents:Resources:Script Libraries folder.
This script is cut down from a larger script to try to nut out the problem. Iâve declared all the variables inside the script instead of extracting them from the incoming email. When this script is activated by the mail rule, nothing happens. I see the little gear symbol in the top right of the screen momentarily, and if I put in a âdisplay dialogâ before or after the calLib section of the script, it does appear but the CalLib section of the script seems to not get activated and the event does not get created.
Iâve tested the calLib section outside of the âusing terms fromâ and âon perform mail action with messagesâ section and it works perfectly, so it seems like the âtell calLibâ does not work within the âusing terms fromâ section.
Any help appreciated.
David
use calLib : script "CalendarLib"
use scripting additions
using terms from application "Mail"
on perform mail action with messages theSelectedMessages for rule theRule
set futureFlag to false
set allDay to false
set MyStartDateString to "September 24, 2015 at 12:46 pm"
set EventDTSTART to date MyStartDateString
set MyEndDateString to "September 24, 2015 at 1:00pm"
set EventDTEND to date MyEndDateString
set EventSummary to "DM Test"
set EventLocation to "Homebush"
set EventDescr to "Welcome to the test invitation" & return & "We really hope you can come."
tell calLib
set theStore to its fetchStore()
set theCal to its fetchCalendarNamed:"Conference Calls" ofType:1 fromStore:theStore
set EventDraft to its makeEventInStore:theStore forCalendar:theCal withSummary:EventSummary dateStart:EventDTSTART dateEnd:EventDTEND isAllDay:allDay location:EventLocation notes:EventDescr
set newEvent to its saveEvent:EventDraft inStore:theStore futureEvents:futureFlag
end tell
end perform mail action with messages
end using terms from
You can probably get away with replacing the âuse terms fromâŠâ statement with a âuse applicationâŠâ statement. You can also make the code a bit more readable by using CalendarLibâs terminology. So:
use calLib : script "CalendarLib"
use scripting additions
use application "Mail"
on perform mail action with messages theSelectedMessages for rule theRule
set futureFlag to false
set allDay to false
set MyStartDateString to "September 24, 2015 at 12:46 pm"
set EventDTSTART to date MyStartDateString
set MyEndDateString to "September 24, 2015 at 1:00pm"
set EventDTEND to date MyEndDateString
set EventSummary to "DM Test"
set EventLocation to "Homebush"
set EventDescr to "Welcome to the test invitation" & return & "We really hope you can come."
set theStore to fetch store
set theCal to fetch calendar "Conference Calls" cal type cal cloud event store theStore
set EventDraft to create event event store theStore destination calendar theCal event summary EventSummary starting date EventDTSTART ending date EventDTEND runs all day allDay event location EventLocation event description EventDescr
set newEvent to store event event EventDraft event store theStore future events futureFlag
end perform mail action with messages
If that still has problems, you will need to move the CalendarLib stuff into its own handler.
Thanks for the script Shane. For some reason when I first tried this, it didnât compile. Then I noticed the changes in syntax that you had put in your code and thought I needed to clean it up. Well I fell down a rabbit hole for a couple of days trying out different things until I eventually returned to your original script which works fine now.:rolleyes:
I first ran it as a script with the two libraries in the User/Library/Script Libraries/ folder, then I tried saving it as a bundle with the two libraries moved to the script bundleâs internal Script Libraries folder. I had to put the BridgePlus library inside the CalendarLibâs internal Script Libraries folder, and then put the CalendarLib inside my scriptâs internal Script Libraries folder for it to work as a script bundle.
FWIW, you could probably get away without the nesting by adding use script âBridgePlusâ to the main script, before the use script âCalendarLibâ statement.
Nice one. I have so many more questions relating to CalendarLib!
When you said I can make the code more readable by using CalendarLibâs terminology, that is useful but confused me because that is not in the ReadMe file supplied with CalendarLib nor could I find that terminology in the CalendarLib script itself. As an example, in the documentation to create an event it says to use the following:
set theEvent to its makeEventInStore:theStore forCalendar:theCal withSummary:"A test event" dateStart:d1 dateEnd:d2 isAllDay:false location:"Around here" notes:"some notes"
but using the CalendarLibâs terminology you have said to use:
set theEvent to create event event store theStore destination calendar theCal event summary "A test event" starting date d1 ending date d2 runs all day false event location "Around here" event description "some notes"
I think I have transcribed the variables correctly between these two versions, because the names of the parameters are quite different - ânotesâ and âevent descriptionâ for example. So, where do I find this other terminology?
Secondly, I think your latest script using CalendarLibâs terminology worked for me because it does not use the named parameters with colons in the handler calls. When I was trying to get it to work before, I had to remove the named parameters and just use the notation Iâm familiar with where I put the parameters unnamed inside brackets after the name of the handler. Do I just need to study up on named parameters?
As you can see, I may be opening a Pandoraâs box of questions and some of them may be of a more general nature rather than being specifically about the CalendarLib library. Let me know if you would rather I PM you.
The easiest way: put it in ~/Library/Script Libraries/, then in Script Editor choose File â Open Dictionary⊠Youâll see it in the list.
As far as I can see, there was nothing wrong with your original code â the defined terminology is more a convenience. But you canât just remove the colons. Something like this:
set theCal to its fetchCalendarNamed:"Conference Calls" ofType:1 fromStore:theStore
is equivalent to this:
set theCal to its fetchCalendarNamed_ofType_fromStore_("Conference Calls", 1, theStore)
I tried copying the CalendarLib.scptd to the ~/Library/Scripting Additions/ folder and I also put it in the users/Library/Scripting Libraries/ folder. But when I choose file/open/library in Script Editor and find it in the list, it doesnât open anything. It will open it as a file though, but that looks like the working mechanism of the script, not a library. I tried it in Script Debugger but that only offers me to choose an application to open a library from or a preset list of libraries. What am I doing wrong?
I am a beginner/intermediate AppleScripter and am enjoying using Calendar Lib EC. But now Iâve realized that I canât remove events.
I have fetched events and used info from those events to create new events, using a test calendar âZIBTESTâ which is a cloud calendar.
I have not successfully removed events. When I try to fetch all of the test events I have created and then remove them, I get an error.
use script "CalendarLib EC" -- put this at the top of your scripts
use scripting additions
set d1 to (current date) - 7 * days
set d2 to d1 + 102 * days
set theStore to fetch store
set theCal2 to fetch calendar "ZIBTEST" cal type cal cloud event store theStore
set theEvents to fetch events starting date d1 ending date d2 searching cals {theCal2} event store theStore
repeat with anEvent in theEvents
try
remove event anEvent event store theStore
end try
end repeat
replies to this script:
tell current application
current date
â date âSaturday, February 6, 2016 at 4:56:36 PMâ
end tell
tell application âScript Editorâ
«event !CLs!reE» given «class !Cst»:«class ocid» id «data optr0000000080913936B87F0000»
â error number -1708
«event !CLs!reE» given «class !Cst»:«class ocid» id «data optr0000000080913936B87F0000»
â error number -1708
. . .
«event !CLs!reE» given «class !Cst»:«class ocid» id «data optr0000000080913936B87F0000»
â error number -1708
«event !CLs!reE» given «class !Cst»:«class ocid» id «data optr0000000080913936B87F0000»
â error number -1708
«event !CLs!reE» given «class !Cst»:«class ocid» id «data optr0000000080913936B87F0000»
â error number -1708
«event !CLs!reE» given «class !Cst»:«class ocid» id «data optr0000000080913936B87F0000»
â error number -1708
«event !CLs!reE» given «class !Cst»:«class ocid» id «data optr0000000080913936B87F0000»
â error number -1708
end tell
There are a total of approximately 100 events and correspondingly 100 errors.
Any advice appreciatedâ
William
Model: MacBook Pro (Retina, 15-inch, Mid 2015)
AppleScript: AppleScript 2.5
Browser: Version 48.0.2564.103 (64-bit)
Operating System: Mac OS X (10.10)
Iâve got eaxctly the same query - some guidance on how to invoke the remove command would be helpful!
set theStore to fetch store
set theCal to fetch calendar destination cal type cal cloud event store theStore
set theEvents to fetch events starting date thestart ending date thestart + 365 * days searching cals theCal event store theStore
repeat with anEvent in theEvents
--set theStore to fetch store
---set theCal to fetch calendar destination cal type cal cloud event store theStore
--set theInfo to (event info for event anEvent)
remove event anEvent event store theStore
end repeat
What if you add some required instructions at the beginning of your script ?
use AppleScript version "2.5" -- requires OS X 10.11 or higher
use scripting additions
use framework "Foundation"
use framework "EventKit"
use script "CalendarLib EC" -- put this at the top of your scripts
set theStore to fetch store
set theCal to fetch calendar destination cal type cal cloud event store theStore
set theEvents to fetch events starting date thestart ending date thestart + 365 * days searching cals theCal event store theStore
repeat with anEvent in theEvents
--set theStore to fetch store
---set theCal to fetch calendar destination cal type cal cloud event store theStore
--set theInfo to (event info for event anEvent)
remove event anEvent event store theStore
end repeat
Hi folks- Iâve a series of scripts that use CalendarLib and since upgrading to Ventura, theyâre throwing errors whenever called upon to do anything other than delete events.
On further digging, it works fine for fetching events and deleting them but fails when events are attempted to be added; it seems like itâs failing on these lines:
set theResult to (theEvent's status())
if theResult is missing value then
theDict's setObject:"none" forKey:"event_status"
else
set theResult to theResult as integer
theDict's setObject:(item (theResult + 1) of {"none", "confirmed", "tentative", "canceled"}) forKey:"event_status"
end if
Iâm not sure whatâs happening there. Youâre not the first person to report problems, but I canât reproduce them, and they seem to be a bit erratic. Sorry I caât offer better news.