Sha256: a19890f6fec4fee867051ddda951cd8b5f35be3131bbe86e77b1787e01202f72

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

module Waddup

  class Source::AppleCalendar < Waddup::Source
    include Waddup::Extension::AppleScript
    extend Waddup::Extension::AppleScript

    ALIAS = 'ical'
    ICON  = "\xF0\x9F\x93\x85 "

    EVENT_SCRIPT = %Q{
      on run argv
        set window_from to date (item 1 of argv)
        set window_to   to date (item 2 of argv)

        tell application "Calendar"
          set results to {}

          set cdars to (events whose start date <= window_to and end date > window_from) in every calendar
          repeat with cdar in cdars
            repeat with evt in cdar
              set end of results to {summary:summary of evt, start_date:start date of evt as string, end_date:end date of evt as string}
            end repeat
          end repeat

          results
        end tell
      end run
    }

    # Aggregates calendar events
    #
    # Arguments:
    #
    #   :from (datetime)
    #   :to   (datetime)
    #
    def events(from, to)
      results = applescript EVENT_SCRIPT,
        :as_ruby => true,
        :args    => [from.strftime('%d/%m/%Y %H:%M'), to.strftime('%d/%m/%Y %H:%M')]

      results.map do |result|
        Waddup::Event.new do |e|
          e.label   = result[:summary]
          e.at      = DateTime.parse(result[:start_date])
          e.until   = DateTime.parse(result[:end_date])
          e.source  = self
        end
      end
    end

    # Requires AppleScript to be available
    def self.usable?
      applescript?
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
waddup-0.0.2 lib/waddup/sources/apple_calendar.rb