Class: Konjac::Office::Mac::Word

Inherits:
Shared show all
Defined in:
lib/konjac/office/mac/word.rb

Overview

Word for Mac

Defined Under Namespace

Classes: WordItem

Instance Attribute Summary

Attributes inherited from Base

#document

Instance Method Summary collapse

Methods inherited from Shared

#close, #open, #path

Methods inherited from Base

#[]=, #export, #import, #read, #shape_at, #tags, #write

Constructor Details

#initialize(path = nil) ⇒ Word

Creates a new Word item



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/konjac/office/mac/word.rb', line 26

def initialize(path = nil)
  super "Microsoft Word", path
  @item_opts.merge!({
    :ref_path     => [:paragraph],
    :content_path => [:text_object, :content],
    :delimiter    => "\v",
    :strippable   => /[\r\n\a]+$/
  })
  @shape_opts.merge!({
    :ref_path     => [:shape],
    :content_path => [:text_frame, :text_range, :content],
    :strippable   => /[\r\n\a]+$/
  })
end

Instance Method Details

#[](*args) ⇒ Object Also known as: item_at

This overrides the Base#[] method because we need to have a slightly modified version of the Item object to enable the selection of paragraph text before writing to it



44
45
46
47
48
49
50
51
# File 'lib/konjac/office/mac/word.rb', line 44

def [](*args)
  opts = parse_args(*args)
  if opts[:type].nil? || opts[:type].empty?
    WordItem.new @item_opts.merge(opts)
  else
    shape_at opts
  end
end

#active_documentObject

Retrieves the active document and caches it



55
56
57
# File 'lib/konjac/office/mac/word.rb', line 55

def active_document
  @active_document ||= @application.active_document
end

#dataObject

Creates a dump of the document’s data in Tag form



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/konjac/office/mac/word.rb', line 60

def data
  i = 1
  tags = []
  begin
    loop do
      temp = Tag.new
      temp.indices = [i]
      temp.removed = temp.added = item_at(i).read
      tags << temp unless temp.blank?
      i += 1
    end
  rescue Appscript::CommandError
  end

  # TODO: I should optimize this later like above, to prevent large
  # shapes from getting out of hand
  i = 1
  begin
    loop do
      temp = Tag.new
      temp.indices = [i]
      temp.removed = temp.added = shape_at(i).read
      temp.type = :shape
      tags << temp unless temp.blank?
      i += 1
    end
  rescue Appscript::CommandError, TypeError
  end

  tags
end

#sizeObject Also known as: length

Retrieves the number of paragraphs in the document. Note that this method fetches all paragraphs elements and can thus be very expensive for large documents.



95
96
97
# File 'lib/konjac/office/mac/word.rb', line 95

def size
  @document.paragraphs.get.size
end