Class: Autogui::Application
- Inherits:
-
Object
- Object
- Autogui::Application
- Includes:
- Logging, Windows::Handle, Windows::Process, Windows::Synchronize
- Defined in:
- lib/win32/autogui/application.rb
Overview
The Application class wraps a binary application so that it can be started and controlled via Ruby. This class is meant to be subclassed.
Constant Summary
Constants included from Logging
Logging::DEBUG, Logging::ERROR, Logging::FATAL, Logging::INFO, Logging::STANDARD_LOGGER, Logging::WARN
Instance Attribute Summary collapse
-
#create_process_timeout ⇒ Number
The wait timeout in seconds used by Process.create.
-
#main_window_timeout ⇒ Number
The main_window wait timeout in seconds.
-
#name ⇒ String
The executable name of the application.
-
#parameters ⇒ String
The executable application parameters.
-
#pid ⇒ Number
readonly
The process identifier (PID) returned by Process.create.
-
#thread_id ⇒ Number
readonly
The process thread id returned by Process.create.
-
#title ⇒ String
Window title of the application.
Instance Method Summary collapse
- #clipboard ⇒ Clipboard
-
#close(options = {}) ⇒ Object
Call the main_window’s close method.
-
#combined_text ⇒ String
The main_window text including all child windows joined together with newlines.
-
#initialize(options = {}) ⇒ Application
constructor
A new instance of Application.
-
#kill ⇒ Object
Send SIGKILL to force the application to die.
-
#main_window ⇒ Autogui::Window
The application main window found by enumerating windows by title and application pid.
-
#running? ⇒ Boolean
If the application is currently running.
-
#set_focus ⇒ Number
Set the application input focus to the main_window.
-
#start ⇒ Number
Start up the binary application via Process.create and set the window focus to the main_window.
Methods included from Logging
Constructor Details
#initialize(options = {}) ⇒ Application
Returns a new instance of Application.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/win32/autogui/application.rb', line 130 def initialize( = {}) unless .kind_of?(Hash) raise_error ArgumentError, 'Initialize expecting options to be a Hash' end @name = [:name] || name @title = [:title] || name @main_window_timeout = [:main_window_timeout] || 10 @create_process_timeout = [:create_process_timeout] || 10 @parameters = [:parameters] # logger setup if .include?(:logger_logfile) logger.trunc = .include?(:logger_trunc) ? [:logger_trunc] : true end logger.logfile = [:logger_logfile] if [:logger_logfile] logger.level = [:logger_level] if [:logger_level] # sanity checks raise_error 'application name not set' unless name start end |
Instance Attribute Details
#create_process_timeout ⇒ Number
Returns the wait timeout in seconds used by Process.create.
91 92 93 |
# File 'lib/win32/autogui/application.rb', line 91 def create_process_timeout @create_process_timeout end |
#main_window_timeout ⇒ Number
Returns the main_window wait timeout in seconds.
88 89 90 |
# File 'lib/win32/autogui/application.rb', line 88 def main_window_timeout @main_window_timeout end |
#name ⇒ String
Returns the executable name of the application.
73 74 75 |
# File 'lib/win32/autogui/application.rb', line 73 def name @name end |
#parameters ⇒ String
Returns the executable application parameters.
76 77 78 |
# File 'lib/win32/autogui/application.rb', line 76 def parameters @parameters end |
#pid ⇒ Number (readonly)
Returns the process identifier (PID) returned by Process.create.
82 83 84 |
# File 'lib/win32/autogui/application.rb', line 82 def pid @pid end |
#thread_id ⇒ Number (readonly)
Returns the process thread id returned by Process.create.
85 86 87 |
# File 'lib/win32/autogui/application.rb', line 85 def thread_id @thread_id end |
#title ⇒ String
Returns window title of the application.
79 80 81 |
# File 'lib/win32/autogui/application.rb', line 79 def title @title end |
Instance Method Details
#clipboard ⇒ Clipboard
278 279 280 |
# File 'lib/win32/autogui/application.rb', line 278 def clipboard @clipboard ||= Autogui::Clipboard.new end |
#close(options = {}) ⇒ Object
Call the main_window’s close method
PostMessage SC_CLOSE and optionally wait for the window to close
231 232 233 |
# File 'lib/win32/autogui/application.rb', line 231 def close(={}) main_window.close() end |
#combined_text ⇒ String
The main_window text including all child windows joined together with newlines. Faciliates matching text.
264 265 266 |
# File 'lib/win32/autogui/application.rb', line 264 def combined_text main_window.combined_text if running? end |
#kill ⇒ Object
Send SIGKILL to force the application to die
236 237 238 |
# File 'lib/win32/autogui/application.rb', line 236 def kill Process::kill(9, pid) end |
#main_window ⇒ Autogui::Window
The application main window found by enumerating windows by title and application pid. This method will keep looking unit main_window_timeout (default: 10s) is exceeded.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/win32/autogui/application.rb', line 200 def main_window return @main_window if @main_window # pre sanity checks raise_error "calling main_window without a pid, application not initialized properly" unless @pid raise_error "calling main_window without a window title, application not initialized properly" unless @title timeout(main_window_timeout) do begin # There may be multiple instances, use title and pid to id our main window @main_window = Autogui::EnumerateDesktopWindows.new.find do |w| w.title.match(title) && w.pid == pid end sleep 0.1 end until @main_window end # post sanity checks raise_error "cannot find main_window, check application title" unless @main_window @main_window end |
#running? ⇒ Boolean
Returns if the application is currently running.
241 242 243 |
# File 'lib/win32/autogui/application.rb', line 241 def running? main_window && (main_window.is_window?) end |
#set_focus ⇒ Number
Set the application input focus to the main_window
249 250 251 |
# File 'lib/win32/autogui/application.rb', line 249 def set_focus main_window.set_focus if running? end |
#start ⇒ Number
Start up the binary application via Process.create and set the window focus to the main_window
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/win32/autogui/application.rb', line 163 def start command_line = name command_line = name + ' ' + parameters if parameters # returns a struct, raises an error if fails process_info = Process.create( :command_line => command_line, :close_handles => false, :creation_flags => Process::DETACHED_PROCESS ) @pid = process_info.process_id @thread_id = process_info.thread_id process_handle = process_info.process_handle thread_handle = process_info.thread_handle # wait for process ret = WaitForInputIdle(process_handle, (create_process_timeout * 1000)) # done with the handles CloseHandle(process_handle) CloseHandle(thread_handle) raise_error "start command failed on create_process_timeout" if ret == WAIT_TIMEOUT raise_error "start command failed while waiting for idle input, reason unknown" unless (ret == 0) @pid end |