Module: Rum::System

Extended by:
System
Included in:
App, System, Window, WindowMatcher
Defined in:
lib/rum/mac/system.rb,
lib/rum/windows/system.rb,
lib/rum/windows/system_foreign_functions.rb

Defined Under Namespace

Modules: Clipboard Classes: Window, WindowMatcher

Constant Summary collapse

ShellExecute =
Win32::API.new('ShellExecute', 'LPPPPI', 'L', 'shell32')
WM_COMMAND =
0x0111
WM_SYSCOMMAND =
0x0112
SC_CLOSE =
0xF060
SC_RESTORE =
0xF120
SC_MAXIMIZE =
0xF030
SC_MINIMIZE =
0xF020
WM_GETTEXT =
0x000D
EM_GETSEL =
0x00B0
EM_SETSEL =
0x00B1
SW_HIDE =
0
SW_SHOWNORMAL =
1
SW_NORMAL =
1
SW_SHOWMINIMIZED =
2
SW_SHOWMAXIMIZED =
3
SW_MAXIMIZE =
3
SW_SHOWNOACTIVATE =
4
SW_SHOW =
5
SW_MINIMIZE =
6
SW_SHOWMINNOACTIVE =
7
SW_SHOWNA =
8
SW_RESTORE =
9
SW_SHOWDEFAULT =
10
SW_FORCEMINIMIZE =
11
SW_MAX =
11
SWP_NOMOVE =
2
SWP_NOSIZE =
1
HWND_TOPMOST =
-1
HWND_NOTOPMOST =
-2
GWL_EXSTYLE =
-20
WS_EX_TOPMOST =
8

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.def_api(function_name, parameters, return_value, rename = nil) ⇒ Object



32
33
34
35
36
37
# File 'lib/rum/windows/system_foreign_functions.rb', line 32

def self.def_api(function_name, parameters, return_value, rename=nil)
  api = Win32::API.new function_name, parameters, return_value, 'user32'
  define_method(rename || snake_case(function_name)) do |*args|
    api.call *args
  end
end

.snake_case(str) ⇒ Object



28
29
30
# File 'lib/rum/windows/system_foreign_functions.rb', line 28

def self.snake_case str
  str.gsub(/([a-z])([A-Z0-9])/, '\1_\2').downcase
end

Instance Method Details

#active_windowObject



36
37
38
# File 'lib/rum/windows/system.rb', line 36

def active_window
  Window.new get_foreground_window
end

#active_window_handlesObject



26
27
28
# File 'lib/rum/windows/system.rb', line 26

def active_window_handles
  Enumerator.new(self, :enum_windows)
end

#active_windowsObject



30
31
32
33
34
# File 'lib/rum/windows/system.rb', line 30

def active_windows
  Enumerator.new do |yielder|
    enum_windows { |handle| yielder.yield Window.new(handle) }
  end
end

#applescript(src) ⇒ Object



5
6
7
8
9
# File 'lib/rum/mac/system.rb', line 5

def applescript src
  pointer = Pointer.new_with_type("@")
  as = NSAppleScript.alloc.initWithSource(src)
  as.executeAndReturnError(pointer)
end

#applescript_quote_string(str) ⇒ Object



31
32
33
34
35
# File 'lib/rum/mac/system.rb', line 31

def applescript_quote_string str
  str.gsub!('\\', '\\\\\\\\')
  str.gsub!('"', '\\"')
  '"' << str << '"'
end

#c_string(obj) ⇒ Object

returns a copy



60
61
62
63
# File 'lib/rum/windows/system.rb', line 60

def c_string obj
  str = obj.to_s + "\0"
  str.encode(Encoding::UTF_16LE)
end

#escape_shell_word(str) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rum/mac/system.rb', line 15

def escape_shell_word str
  if str.empty? or %r{\A[0-9A-Za-z+,./:=@_-]+\z} =~ str
    str
  else
    result = ''
    str.scan(/('+)|[^']+/) {
      if $1
        result << %q{\'} * $1.length
      else
        result << "'#{$&}'"
      end
    }
    result
  end
end

#get_selectionObject



88
89
90
# File 'lib/rum/windows/system.rb', line 88

def get_selection
  Clipboard.preserve { Clipboard.get_selection }
end

#input_box(text, title, default_text) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rum/windows/system.rb', line 69

def input_box text, title, default_text
  buffer_size = 2048
  buffer = default_text.to_s.encode Encoding::UTF_16LE
  buffer.force_encoding Encoding::BINARY
  if buffer.length >= buffer_size
    buffer << "\0\0"
  else
    buffer << "\0" * (buffer_size - buffer.length)
  end

  length = input_box_internal(c_string(text), c_string(title), buffer)
  if length == 0
    ''
  else
    result = buffer[0, length*2]
    result.force_encoding(Encoding::UTF_16LE).encode(Encoding::UTF_8)
  end
end

#kill_task(exe_name) ⇒ Object



92
93
94
# File 'lib/rum/windows/system.rb', line 92

def kill_task exe_name
  system("taskkill /F /IM #{exe_name}.exe")
end

#message_box(message, title = '') ⇒ Object



65
66
67
# File 'lib/rum/windows/system.rb', line 65

def message_box message, title=''
  message_box_internal(c_string(message), c_string(title))
end

#send_key_event(key, down) ⇒ Object



7
8
9
# File 'lib/rum/windows/system.rb', line 7

def send_key_event key, down
  send_key_event_for_id key.id, down
end

#send_key_event_for_id(id, down) ⇒ Object



11
12
13
14
15
# File 'lib/rum/windows/system.rb', line 11

def send_key_event_for_id id, down
  extended, id = id.divmod 2**9
  extended = (extended == 1)
  keybd_event id, down, 0, extended
end

#send_keypress(key) ⇒ Object



17
18
19
20
# File 'lib/rum/windows/system.rb', line 17

def send_keypress key
  send_key_event key, true
  send_key_event key, false
end

#send_unicode_char(char) ⇒ Object



22
23
24
# File 'lib/rum/windows/system.rb', line 22

def send_unicode_char char
  send_unicode_char_internal(char.encode(Encoding::UTF_16LE))
end

#spawn_in_terminal(*args) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/rum/mac/system.rb', line 37

def spawn_in_terminal(*args)
  close = args.delete :close_if_successful
  command = args.map { |arg| escape_shell_word(arg) }.join(' ')
  command << ';[ $? -eq 0 ] && exit' if close
  command = applescript_quote_string(command)
  applescript 'tell application "Terminal" to do script ' + command
end

#start(command, parameters = 0) ⇒ Object



11
12
13
# File 'lib/rum/mac/system.rb', line 11

def start *args
  system 'open', *args
end

#terminal_windowObject



40
41
42
# File 'lib/rum/windows/system.rb', line 40

def terminal_window
  Window.new get_console_window
end