Class: SharedTools::Tools::Computer::MacDriver

Inherits:
BaseDriver
  • Object
show all
Defined in:
lib/shared_tools/tools/computer/mac_driver.rb

Overview

A driver for interacting with a Mac. Be careful with using as it can perform actions on your computer!

Constant Summary

Constants inherited from BaseDriver

BaseDriver::DEFAULT_DISPLAY_SCALE, BaseDriver::DEFAULT_MOUSE_BUTTON

Instance Attribute Summary

Attributes inherited from BaseDriver

#display_height, #display_number, #display_width

Instance Method Summary collapse

Methods inherited from BaseDriver

#mouse_double_click, #mouse_drag, #mouse_triple_click, #wait

Constructor Details

#initialize(keyboard: MacOS.keyboard, mouse: MacOS.mouse, display: MacOS.display) ⇒ MacDriver



8
9
10
11
12
13
14
# File 'lib/shared_tools/tools/computer/mac_driver.rb', line 8

def initialize(keyboard: MacOS.keyboard, mouse: MacOS.mouse, display: MacOS.display)
  @keyboard = keyboard
  @mouse = mouse
  @display = display

  super(display_width: display.wide, display_height: display.high, display_number: display.id)
end

Instance Method Details

#hold_key(text:, duration:) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/shared_tools/tools/computer/mac_driver.rb', line 23

def hold_key(text:, duration:)
  options = text.to_s.split('+')
  key     = options.pop
  mask    = options.reduce(0) { |m, opt| m | Library::CoreGraphics::EventFlags.find(opt) }

  @keyboard.key_down(key, mask: mask)
  Kernel.sleep(duration.to_f)
  @keyboard.key_up(key, mask: mask)

  { success: true, key: text, duration: duration }
end

#key(text:) ⇒ Object



17
18
19
# File 'lib/shared_tools/tools/computer/mac_driver.rb', line 17

def key(text:)
  @keyboard.keys(text)
end

#mouse_click(coordinate:, button:) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/shared_tools/tools/computer/mac_driver.rb', line 56

def mouse_click(coordinate:, button:)
  x = coordinate[:x]
  y = coordinate[:y]

  case button
  when "left" then @mouse.left_click(x:, y:)
  when "middle" then @mouse.middle_click(x:, y:)
  when "right" then @mouse.right_click(x:, y:)
  end
end

#mouse_down(coordinate:, button: DEFAULT_MOUSE_BUTTON) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/shared_tools/tools/computer/mac_driver.rb', line 68

def mouse_down(coordinate:, button: DEFAULT_MOUSE_BUTTON)
  x = coordinate[:x]
  y = coordinate[:y]

  case button
  when "left" then @mouse.left_down(x:, y:)
  when "middle" then @mouse.middle_down(x:, y:)
  when "right" then @mouse.right_down(x:, y:)
  end
end

#mouse_move(coordinate:) ⇒ Object



47
48
49
50
51
52
# File 'lib/shared_tools/tools/computer/mac_driver.rb', line 47

def mouse_move(coordinate:)
  x = coordinate[:x]
  y = coordinate[:y]

  @mouse.move(x:, y:)
end

#mouse_positionHash<{ x: Integer, y: Integer }>



36
37
38
39
40
41
42
43
44
45
# File 'lib/shared_tools/tools/computer/mac_driver.rb', line 36

def mouse_position
  position = @mouse.position
  x = position.x
  y = position.y

  {
    x:,
    y:,
  }
end

#mouse_up(coordinate:, button: DEFAULT_MOUSE_BUTTON) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/shared_tools/tools/computer/mac_driver.rb', line 81

def mouse_up(coordinate:, button: DEFAULT_MOUSE_BUTTON)
  x = coordinate[:x]
  y = coordinate[:y]

  case button
  when "left" then @mouse.left_up(x:, y:)
  when "middle" then @mouse.middle_up(x:, y:)
  when "right" then @mouse.right_up(x:, y:)
  end
end

#screenshot {|file| ... } ⇒ Object

Yields:

  • (file)

Yield Parameters:

  • file (File)


130
131
132
# File 'lib/shared_tools/tools/computer/mac_driver.rb', line 130

def screenshot(&)
  @display.screenshot(&)
end

#scroll(amount:, direction:) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/shared_tools/tools/computer/mac_driver.rb', line 99

def scroll(amount:, direction:)
  # Attach CGEventCreateScrollWheelEvent2 if not already done
  unless Library::CoreGraphics.respond_to?(:CGEventCreateScrollWheelEvent2)
    Library::CoreGraphics.module_eval do
      attach_function :CGEventCreateScrollWheelEvent2,
                      [:pointer, :uint32, :uint32, :int32, :int32, :int32],
                      :pointer
    end
  end

  amt = amount.to_i
  # kCGScrollEventUnitLine = 1; wheel_count = 2 (vertical + horizontal)
  delta_y, delta_x = case direction.to_s.downcase
                     when 'up'    then [ amt,  0]
                     when 'down'  then [-amt,  0]
                     when 'left'  then [0, -amt]
                     when 'right' then [0,  amt]
                     else              [0,    0]
                     end

  event = Library::CoreGraphics.CGEventCreateScrollWheelEvent2(nil, 1, 2, delta_y, delta_x, 0)
  Library::CoreGraphics.CGEventPost(
    Library::CoreGraphics::EventTapLocation::HID_EVENT_TAP, event
  )
  Library::CoreGraphics.CFRelease(event)

  { success: true, direction: direction, amount: amt }
end

#type(text:) ⇒ Object



93
94
95
# File 'lib/shared_tools/tools/computer/mac_driver.rb', line 93

def type(text:)
  @keyboard.type(text)
end