Skip to content

Instantly share code, notes, and snippets.

@eban
Created June 24, 2014 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eban/c0372936da329ad4992f to your computer and use it in GitHub Desktop.
Save eban/c0372936da329ad4992f to your computer and use it in GitHub Desktop.
#! /usr/bin/ruby
# -*- coding: utf-8 -*-
require 'win32ole'
olFolderCalendar = 9
WIN32OLE.codepage = WIN32OLE::CP_UTF8
ol =
begin
WIN32OLE.connect("Outlook.Application")
rescue
WIN32OLE.new("Outlook.Application")
end
items = ol.Session.GetDefaultFolder(olFolderCalendar).Items
items.Sort "[Start]"
items.IncludeRecurrences = true
now = Time.now
today = Time.new(now.year, now.month, now.day)
seven = Time.at(today + 8 * 24 * 60 * 60)
body = <<'EOT'
BEGIN:VCALENDAR
BEGIN:VTIMEZONE
TZID:Asia/Tokyo
BEGIN:STANDARD
TZOFFSETFROM:+0900
TZOFFSETTO:+0900
TZNAME:JST
DTSTART:19700101T000000
END:STANDARD
END:VTIMEZONE
EOT
count = 0
STATUS_VALUES = ["CANCELLED", "TENTATIVE", "CONFIRMED"]
filter =
"[Start] >= '#{today.strftime("%Y/%m/%d %H:%M")}'" +
" AND [End] <= '#{seven.strftime("%Y/%m/%d")}'" +
" AND [BusyStatus] > 0"
e = items.Find(filter)
while e
valarm = 1 < e.BusyStatus && today < e.Start ? <<'EOT' : ""
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER;VALUE=DURATION:-PT5M
END:VALARM
EOT
body << <<"EOT"
BEGIN:VEVENT
UID:#{count+=1}
SUMMARY:#{e.Subject} [#{STATUS_VALUES[e.BusyStatus][0,2]}]
DTSTART;TZID=Asia/Tokyo:#{e.Start.strftime("%Y%m%dT%H%M%S")}
DTEND;TZID=Asia/Tokyo:#{e.End.strftime("%Y%m%dT%H%M%S")}
LOCATION:#{e.Location}
STATUS:#{STATUS_VALUES[e.BusyStatus]}
ATTENDEE;ROLE=REQ-PARTICIPANT:#{e.RequiredAttendees}
DESCRIPTION:#{e.Body.gsub(/\r?\n/, '\n')}
#{valarm}END:VEVENT
EOT
e = items.FindNext
end
body << "END:VCALENDAR\n"
ARGV[0] ? IO.write(ARGV[0], body) : puts(body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment