Monday, April 22, 2019

#1 2015-12-14 04:39:07 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5626

[ANN] CalendarLib, improved Calendar scripting

CalendarLib, a scripting library for calendar event scripting without Calendar.app, is now available here: www.macosxautomation.com/applescript/ap … arLib.html

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:

Applescript:

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

See the Web page and docs for details.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#2 2015-12-21 07:40:18 am

davidhmorgan
Member
From:: Sydney
Registered: 2014-08-20
Posts: 65

Re: [ANN] CalendarLib, improved Calendar scripting

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

Applescript:


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


Filed under: mail, calendar, Mail Rule, calLib

Offline

 

#3 2015-12-21 04:28:46 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5626

Re: [ANN] CalendarLib, improved Calendar scripting

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:

Applescript:

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#4 2015-12-27 06:25:35 am

davidhmorgan
Member
From:: Sydney
Registered: 2014-08-20
Posts: 65

Re: [ANN] CalendarLib, improved Calendar scripting

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.

Offline

 

#5 2015-12-27 06:30:18 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5626

Re: [ANN] CalendarLib, improved Calendar scripting

davidhmorgan wrote:

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#6 2015-12-27 07:22:59 am

davidhmorgan
Member
From:: Sydney
Registered: 2014-08-20
Posts: 65

Re: [ANN] CalendarLib, improved Calendar scripting

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

Applescript:


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:

Applescript:


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.

Offline

 

#7 2015-12-27 04:13:03 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5626

Re: [ANN] CalendarLib, improved Calendar scripting

davidhmorgan wrote:

So, where do I find this other terminology?


The easiest way: put it in ~/Library/Script Libraries/, then in Script Editor choose File -> Open Dictionary... You'll see it in the list.

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

Applescript:

set theCal to its fetchCalendarNamed:"Conference Calls" ofType:1 fromStore:theStore

is equivalent to this:

Applescript:

set theCal to its fetchCalendarNamed_ofType_fromStore_("Conference Calls", 1, theStore)

As you will see when you compile it.

Edited to fix path.

Last edited by Shane Stanley (2015-12-27 05:43:55 pm)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#8 2015-12-27 04:29:23 pm

davidhmorgan
Member
From:: Sydney
Registered: 2014-08-20
Posts: 65

Re: [ANN] CalendarLib, improved Calendar scripting

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?

Offline

 

#9 2015-12-27 04:35:53 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5626

Re: [ANN] CalendarLib, improved Calendar scripting

davidhmorgan wrote:

I tried copying the CalendarLib.scptd to the ~/Library/Scripting Additions/ folder and I also put it in the users/Library/Scripting Libraries/ folder.


Sorry,my bad -- I meant Script Libraries (not Scripting).

But when I choose file/open/library in Script Editor and find it in the list, it doesn't open anything.


It should show a normal dictionary, like a scriptable app. Try quitting Script Editor and trying again.

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.


It's a bit tricky in SD. You need to drag the file over it's icon, then press the option key, and let go.

The last-resort is to control-click on it in the Finder, and drag the .sdef file over SE or SD.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#10 2016-01-03 12:12:56 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5626

Re: [ANN] CalendarLib, improved Calendar scripting

FYI, CalendarLib has had a minor upgrade to version 1.1.0, adding a new command for retrieving multiple calendars.

Of perhaps more interest, it has a new sibling, CalendarLib EC. The differences are:

* It requires El Capitan or later

* It removes the requirement for BridgePlus

* Several parameters are now optional

Both are available from:

www.macosxautomation.com/applescript/ap … _Libs.html


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#11 2016-02-06 05:12:09 pm

wdockery
Member
Registered: 2011-09-06
Posts: 3

Re: [ANN] CalendarLib, improved Calendar scripting

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.

Applescript:

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)


Filed under: "Calendar Lib EC"

Offline

 

#12 2016-02-06 06:36:42 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5626

Re: [ANN] CalendarLib, improved Calendar scripting

First, can you confirm you are running 10.11? That's what the lib requires, but your post says you're running 10.10.

Second, are these test events repeating events?


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#13 2019-02-21 11:10:00 am

theboyler
Member
Registered: 2014-03-18
Posts: 2

Re: [ANN] CalendarLib, improved Calendar scripting

I've got eaxctly the same query - some guidance on how to invoke the remove command would be helpful!

Applescript:

   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

Offline

 

#14 2019-02-21 11:27:09 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3397

Re: [ANN] CalendarLib, improved Calendar scripting

What if you add some required instructions at the beginning of your script ?

Applescript:

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

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 21 février 2019 18:26:04

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)