Class: Mac::Robot

Inherits:
Object
  • Object
show all
Defined in:
lib/mac-robot.rb

Defined Under Namespace

Classes: Error, OutOfResolution

Constant Summary collapse

BUTTONS =
{
  :left => 0,
  :right => 1,
  :center => 2
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRobot

Returns a new instance of Robot.



17
18
19
20
21
22
23
# File 'lib/mac-robot.rb', line 17

def initialize
  @x = 0
  @y = 0
          
  @state = :mouse_up
  @dispatcher = EventDispatcher.new
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



9
10
11
# File 'lib/mac-robot.rb', line 9

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



9
10
11
# File 'lib/mac-robot.rb', line 9

def y
  @y
end

Instance Method Details

#display_pixel_sizeObject



88
89
90
# File 'lib/mac-robot.rb', line 88

def display_pixel_size
  @screen ||= Struct.new(:width, :height).new(*Util.get_display_pixel_size)
end

#get_pixel_color(x, y) ⇒ Object

Raises:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mac-robot.rb', line 71

def get_pixel_color(x, y)
  raise OutOfResolution if x < 0 || y < 0
  raise OutOfResolution if display_pixel_size.width < x || display_pixel_size.height < y

  # workarownd for exeption occurs when x == display_pixel_size.width
  #
  # *** Assertion failure in -[NSBitmapImageRep initWithCGImage:], /SourceCache/AppKit/AppKit-1265/AppKit.subproj/NSBitmapImageRep.m:1286
  # An uncaught exception was raised
  # Invalid parameter not satisfying: cgImage != NULL
  if display_pixel_size.width == x
    x = x - 0.1
  end

  color = Util.get_pixel_color(x, y)
  Struct.new(:red, :green, :blue, :alpha).new(*color)
end

#key_press(keycode) ⇒ Object



54
55
56
# File 'lib/mac-robot.rb', line 54

def key_press(keycode)
  keyboard_event(keycode, 1)
end

#key_release(keycode) ⇒ Object



58
59
60
# File 'lib/mac-robot.rb', line 58

def key_release(keycode)
  keyboard_event(keycode, 0)
end

#mouse_current_locationObject



49
50
51
52
# File 'lib/mac-robot.rb', line 49

def mouse_current_location
  loc = Util.get_mouse_current_location
  Struct.new(:x, :y).new(*loc)
end

#mouse_drag(x, y) ⇒ Object



42
43
44
45
46
47
# File 'lib/mac-robot.rb', line 42

def mouse_drag(x, y)
  @x = x
  @y = y

  mouse_event(:left, :mouse_drag)
end

#mouse_move(x, y) ⇒ Object



35
36
37
38
39
40
# File 'lib/mac-robot.rb', line 35

def mouse_move(x, y)
  @x = x
  @y = y

  mouse_event(:left, :mouse_move)
end

#mouse_press(button = :left) ⇒ Object



25
26
27
28
# File 'lib/mac-robot.rb', line 25

def mouse_press(button = :left)
  mouse_event(button, :mouse_down)
  @state = :mouse_down
end

#mouse_release(button = :left) ⇒ Object



30
31
32
33
# File 'lib/mac-robot.rb', line 30

def mouse_release(button = :left)
  mouse_event(button, :mouse_up)
  @state = :mouse_up
end

#scroll_wheel_line(args = {}) ⇒ Object Also known as: scroll_wheel



62
63
64
# File 'lib/mac-robot.rb', line 62

def scroll_wheel_line(args = {})
  scroll_wheel_event(:line, *scroll_count_extract(args))
end

#scroll_wheel_pixel(args = {}) ⇒ Object



67
68
69
# File 'lib/mac-robot.rb', line 67

def scroll_wheel_pixel(args = {})
  scroll_wheel_event(:pixel, *scroll_count_extract(args))
end