Import ics files into default Calendar(embedded applescript)

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

I found the importing of ics files was something I was forgetting to do manually, so I came with an Hazel rule that uses Apple script to automate it.

My Hazel rule applies to my Dropbox "watched" folder and to my downloads folder. ics files from work/other will arrive via Dropbox, and those from my macbook, where the rule runs, are saved in the downloads folder.

Hazel detects the ics file, runs the applescript, and finally moves the file into the trash folder. The script doesn't have a reliable way know when the Calendar app has finished importing the ics file, so I hard-coded a delay at the end of the script for now. Without the delay, Hazel will try to move the file before it has been imported. You'll get an error message from the calendar app that it cannot process the file when this happens. You may need to play with the delay if your system takes a long time to import the ics file.

Use cases:
Several of my social networking sites lets you export events as ics file to import into your calendar. Also, my employer uses MS Exchange to schedule meetings, and if you forward a meeting to your personal email address, everyone in meeting sees it. If you instead export the meeting to ics and import it, your information is safe :)

Code: Select all
property theFile : alias "Users:jgooch:Downloads:e1204988566180171.ics"

-- "Double-click" the file, since iCal/Calendar's 'open' doesn't work in later versions.
tell application id ""
   -- Get the application's name (presumably either "iCal" or "Calendar") for the GUI Scripting later.
   set iCalName to its name
end tell

tell application "Finder" to open file theFile

tell application "System Events"
   tell process iCalName
      -- Hedge bets over the name of the "Add Event" or "Add Events" window.
      tell (first window whose title begins with "Add event")
         repeat until (it exists)
            delay 0.2
         end repeat
         click button "OK"
      end tell
   end tell
end tell
delay 2
return true

I'd like to extend this to invitations by email, and also add some type of logging /notification for each import . Maybe later !
Posts: 10
Joined: Sat Nov 07, 2015 11:42 pm
Location: Denver,co

Return to Tips & Tricks - DO NOT POST QUESTIONS