Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How can you programmatically import ics files?

My work used MS Exchange , and i like to export business meetings to ics files so that I can import them into Apple calendar to get reminder of them on my iPhone. Sometimes I get too busy and forget to check my personal mail in order to import the ics file. This has led to more than one missed meeting as I get pretty deep my work of writing code, architecting systems, etc. So much so, sometimes it takes coworker leaving for the day to remind me that I should go home for the day, too!


Since my Mac at home is always online, I've looked at using Dropbox/Hazel instead of email/applescript, but it looks like either way I hit the same stumbling block - programmatically ( i.e. non-interactively ) importing the ics file. I've found snippets of Applescript that may work with some tweaking. The below one runs until the Calendar app opens prompting for the name of the calendar to import the event into. I need to manually answer the prompt with cancel/ok at that point. I've changes the window title setting to "Add" and default calendar name setting to "Home", but it still gets stuck there.

set dupCalName to""


set thePath to (path to desktop as text) & "Personal.ics"


tell application id "com.apple.iCal"

activate

-- Get the application's name (presumably either "iCal" or "Calendar") for the GUI Scripting later. set iCalName toits name

-- Since it's possible for more than one calendar to have the same name, the repeat loop idea is better than simple name references here. But only one loop is necessary in this part of the script. repeatwith i from 1 to count of calendars

set thisCalendar to calendar i -- Gets an id reference to the calendar. if (name of thisCalendar is"Personal") then

set dupCalName to"Personal"

set replacementName to"Personal (old)"

set name of thisCalendar to replacementName

elseif (name of thisCalendar is"Personal 2") then

set dupCalName to"Personal 2"

set replacementName to"Personal 2 (old)"

set name of thisCalendar to replacementName

endif

endrepeat

endtell


delay 1


-- "Double-click" the file, since iCal/Calendar's 'open' doesn't work in later versions.tell application "Finder"to open file thePath


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 beginswith"Add event")

repeat until (it exists)

delay 0.2

endrepeat

click pop up button 1

repeat until (menu 1 of pop up button 1 exists)

delay 0.2

endrepeat

click menu item "New Calendar"of menu 1 of pop up button 1

repeatwhile (menu 1 of pop up button 1 exists)

delay 0.2

endrepeat

click button "OK"

endtell

endtell

endtell


--check if a Personal 2 exists after the import of new iCal and change its nametell application id "com.apple.iCal"

activate

repeatwith i from 1 to count of calendars

set thisCalendar to calendar i

if (name of thisCalendar) is"Personal 2"then

set name of thisCalendar to"Personal"

exitrepeat

endif

endrepeat

endtell


tell application "SystemUIServer"

activate

if dupCalName isnot""then display dialog "Found an existing calendar named " & dupCalName & ". It has been renamed "& replacementName & "." buttons {"OK"} default button "OK"with icon 2

endtell

OS X El Capitan (10.11.2), 512GB SSD 16GB RAM

Posted on Feb 14, 2016 3:06 PM

Reply
Question marked as Best reply

Posted on Feb 15, 2016 2:06 PM

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]

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 "com.apple.iCal"

activate

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

[/code]


This works great with Hazel and El Capitan.

1 reply
Question marked as Best reply

Feb 15, 2016 2:06 PM in response to AxeBox360

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]

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 "com.apple.iCal"

activate

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

[/code]


This works great with Hazel and El Capitan.

How can you programmatically import ics files?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.