def print_all_window_details(): for window in Window.get_all_windows(): if window.is_visible: window = Window.get_foreground() logger.info('Window details') logger.info('title=' + window.title) logger.info('executable=' + window.executable)
def test_set_handle(self): """ Test access to Window.handle property. """ # Verify that only integers and longs are accepted. Window(0) Window(int(1)) Window(long(2)) self.assertRaises(TypeError, Window, [None]) self.assertRaises(TypeError, Window, ["string"]) self.assertRaises(TypeError, Window, [3.4])
def process_grammars_context(self, window=None): """ Enable/disable grammars & rules based on their current contexts. This must be done preemptively for some SR engine back-ends, such as WSR, that don't apply context changes upon/after the utterance start has been detected. The WSR engine should call this automatically whenever the foreground application (or its title) changes. The user may want to call this manually to update when custom contexts. The *window* parameter is optional window information, which can be passed in as an optimization if it has already been gathered. """ if window is None: from dragonfly.windows.window import Window window = Window.get_foreground() for grammar in self.grammars: # Prevent 'notify_begin()' from being called. if grammar.name == "_recobs_grammar": continue grammar.process_begin(window.executable, window.title, window.handle)
def _execute(self, data=None): executable = self.executable title = self.title index = self.index if data and isinstance(data, dict): if executable: executable = (executable % data).lower() if title: title = (title % data).lower() if index: index = (index % data).lower() index = int(index) if index else 0 # Get the first matching window and bring it to the foreground. windows = Window.get_matching_windows(executable, title) if self.filter_func: windows = [ window for window in windows if self.filter_func(window) ] if windows and (index < len(windows)): window = windows[index] if self.focus_only: window.set_focus() else: window.set_foreground() else: raise ActionError("Failed to find window (%s)." % self._str)
def _ensure_execution_context(data): '''Populates the data field of execute with context information if not present.''' if data is None: data = {} if '_context' not in data: data['_context'] = Window.get_foreground() return data
def emit(self, record): # Brings status window to the forefront upon error if (settings.SETTINGS["miscellaneous"]["status_window_foreground_on_error"]): # The window title is unique to Natlink if (get_engine()._name == 'natlink'): windows = Window.get_matching_windows(None, "Messages from Python Macros V") if windows: windows[0].set_foreground()
def focus_was_success(title, executable): w=Window.get_foreground() success=True if title!=None: success=title in w.title if success and executable!=None: success=executable in w.executable return success
def callback(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime): window = Window.get_foreground() # Note: hwnd doesn't always match window.handle, even when # foreground window changed (and sometimes it didn't change) window_changed = ( window != self._last_foreground_window or window == self._last_foreground_window and window.title != self._last_foreground_window_title) if window_changed: self.process_grammars_context(window) self._last_foreground_window = window self._last_foreground_window_title = window.title
def _focus_window_after_starting(self, pid): timeout = 1.0 exe = self._args[0] # Wait for the window to appear for N seconds. action = WaitWindow(executable=exe, timeout=timeout) if action.execute(): # Bring the window to the foreground. Window.get_foreground().set_foreground() # The application window wasn't focused, so try to focus it. else: target = pid start = time.time() while time.time() - start < timeout: found = False for window in Window.get_matching_windows(exe): if target is None or window.pid == target: window.set_foreground() found = True break if found: break
def _process_recognition(self,node,extras): name=extras['name'].format().lower() windows=[w for w in Window.get_all_windows() if w.is_visible ] names=[w.executable.lower() for w in windows] names+=[w.title.lower() for w in windows] matches=get_close_matches(name,names,10,0.1) matches=self.clean_unwanted_matches(matches,name) if not matches: return for window in windows: if matches[0] in [window.executable.lower(),window.title.lower()]: # In Windows, this is not guaranteed to work. See: # http://stackoverflow.com/questions/688337/how-do-i-force-my-app-to-come-to-the-front-and-take-focus # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633539(v=vs.85).asp window.set_foreground() return
def _process_recognition(self, node, extras): name = extras['name'].format().lower() windows = [w for w in Window.get_all_windows() if w.is_visible] names = [w.executable.lower() for w in windows] names += [w.title.lower() for w in windows] matches = get_close_matches(name, names, 10, 0.1) matches = self.clean_unwanted_matches(matches, name) if not matches: return for window in windows: if matches[0] in [window.executable.lower(), window.title.lower()]: # In Windows, this is not guaranteed to work. See: # http://stackoverflow.com/questions/688337/how-do-i-force-my-app-to-come-to-the-front-and-take-focus # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633539(v=vs.85).asp window.set_foreground() return
def clear_log(): # Function to clear natlink status window try: # pylint: disable=import-error import natlink windows = Window.get_all_windows() matching = [w for w in windows if b"Messages from Python Macros" in w.title] if matching: handle = (matching[0].handle) rt_handle = win32gui.FindWindowEx(handle, None, "RICHEDIT", None) win32gui.SetWindowText(rt_handle, "") return except Exception as e: printer.out(e)
def _execute(self, data=None): self._log.debug("Waiting for window context: %s" % self) start_time = time.time() while 1: foreground = Window.get_foreground() mismatch = False for match_name in self._match_functions: match_func = getattr(self, match_name) if not match_func(foreground): mismatch = True break if not mismatch: return if time.time() - start_time > self._timeout: raise ActionError("Timeout while waiting for window context: %s" % self)
def show_window(): title = None if get_engine()._name == 'natlink': import natlinkstatus # pylint: disable=import-error status = natlinkstatus.NatlinkStatus() if status.NatlinkIsEnabled() == 1: if six.PY2: title = "Messages from Python Macros" else: title = "Messages from Natlink" else: title = "Caster: Status Window" if get_engine()._name != 'natlink': title = "Caster: Status Window" windows = Window.get_matching_windows(title=title) if windows: windows[0].set_foreground()
def _compute_kaldi_rules_activity(self, phrase_start=True): window_info = {} if phrase_start: fg_window = Window.get_foreground() window_info = { "executable": fg_window.executable, "title": fg_window.title, "handle": fg_window.handle, } for grammar_wrapper in self._iter_all_grammar_wrappers_dynamically(): grammar_wrapper.phrase_start_callback(**window_info) self.prepare_for_recognition() self._active_kaldi_rules = set() self._kaldi_rules_activity = [False] * self._compiler.num_kaldi_rules for grammar_wrapper in self._iter_all_grammar_wrappers_dynamically(): if grammar_wrapper.active and (not self._any_exclusive_grammars or grammar_wrapper.exclusive): for kaldi_rule in grammar_wrapper.kaldi_rule_by_rule_dict.values(): if kaldi_rule.active: self._active_kaldi_rules.add(kaldi_rule) self._kaldi_rules_activity[kaldi_rule.id] = True self._log.debug("active kaldi_rules (from window %s): %s", window_info, [kr.name for kr in self._active_kaldi_rules]) return self._kaldi_rules_activity
def attempt_focus(): for win in Window.get_all_windows(): w=win found_match=True if title!=None: found_match=title in w.title if found_match and executable!=None: found_match=executable in w.executable if found_match: try: BringApp(w.executable).execute() except Exception: print("Unable to set focus:\ntitle: "+w.title+"\nexe: "+w.executable) break # do not assume that it worked success = SuperFocusWindow.focus_was_success(title, executable) if not success: if title!=None: print("title failure: ", title, w.title) if executable!=None: print("executable failure: ", executable, w.executable, executable in w.executable) return success
def focus_mousegrid(gridtitle): ''' Loops over active windows for MouseGrid window titles. Issue #171 When MouseGrid window titles found focuses MouseGrid overly. ''' if sys.platform.startswith('win'): # May not be needed for Linux/Mac OS - testing required try: for i in range(9): matches = Window.get_matching_windows(title=gridtitle, executable="python") if not matches: Pause("50").execute() else: break if matches: for handle in matches: handle.set_foreground() break else: printer.out("`Title: `{}` no matching windows found".format(gridtitle)) except Exception as e: printer.out("Error focusing MouseGrid: {}".format(e)) else: pass
def get_active_window_path(): return Window.get_foreground().executable
def get_active_window_path(): return Window.get_foreground().executable
def _process_recognition(self, node, extras): window=Window.get_foreground() extras['operation'](window)
def mimic(self, words, **kwargs): """ Mimic a recognition of the given *words*. :param words: words to mimic :type words: str|iter :Keyword Arguments: optional *executable*, *title* and/or *handle* keyword arguments may be used to simulate a specific foreground window context. The current foreground window attributes will be used instead for any keyword arguments not present. .. note:: Any dictation words should be all uppercase, e.g. "HELLO WORLD". Dictation words not in uppercase will result in the engine **not** decoding and recognizing the command! """ # Handle string input. if isinstance(words, string_types): words = words.split() # Don't allow non-iterable objects. if not iter(words): raise TypeError("%r is not a string or other iterable object" % words) # Fail on empty input. if not words: raise MimicFailure("Invalid mimic input %r" % words) # Notify observers that a recognition has begun. self._recognition_observer_manager.notify_begin() # Generate the input for process_words. words_rules = self.generate_words_rules(words) w = Window.get_foreground() process_args = { "executable": w.executable, "title": w.title, "handle": w.handle, } # Allows optional passing of window attributes to mimic process_args.update(kwargs) # Call process_begin() for each grammar wrapper. Use a copy of # _grammar_wrappers in case it changes. for wrapper in self._grammar_wrappers.copy().values(): wrapper.process_begin(**process_args) # Take another copy of _grammar_wrappers to use for processing. grammar_wrappers = self._grammar_wrappers.copy().values() # Count exclusive grammars. exclusive_count = 0 for wrapper in grammar_wrappers: if wrapper.exclusive: exclusive_count += 1 # Call process_words() for each grammar wrapper, stopping early if # processing occurred. processing_occurred = False for wrapper in grammar_wrappers: # Skip non-exclusive grammars if there are one or more exclusive # grammars. if exclusive_count > 0 and not wrapper.exclusive: continue # Process the grammar. processing_occurred = wrapper.process_words(words_rules) if processing_occurred: break # If no processing occurred, then the mimic failed. if not processing_occurred: self._recognition_observer_manager.notify_failure(None) raise MimicFailure("No matching rule found for words %r." % (words, ))
def _process_recognition(self, node, extras): monitor_number = int(extras['monitor']) - 1 self.move_to_monitor(Window.get_foreground(), monitor_number)
def _process_recognition(self, node, extras): window = Window.get_foreground() extras['operation'](window)
def _process_recognition(self, node, extras): monitor_number=int(extras['monitor'])-1 self.move_to_monitor(Window.get_foreground(),monitor_number)
def _process_recognition(self, node, extras): self.snap_window(Window.get_foreground(),extras['region'])
def _process_recognition(self, node, extras): window = Window.get_foreground() logger.info('Window details') logger.info('title=' + window.title) logger.info('executable=' + window.executable)
def _process_recognition(self, node, extras): self.snap_window(Window.get_foreground(), extras['region'])
def phrase_start_callback(self, stream_number, stream_position): window = Window.get_foreground() self.grammar.process_begin(window.executable, window.title, window.handle)
def _execute(self, data=None): foreground = Window.get_foreground() if foreground.title.find("Emacs editor") != -1: Key("c-y").execute() else: Key("c-v").execute()
def _execute(self, data=None): foreground = Window.get_foreground() if foreground.title.find("Emacs editor") != -1: Key("c-y").execute() else: Key("c-v").execute()
def _execute_events(self, events): """ Send events. """ window = Window.get_foreground() for event in events: event.execute(window)
def path_from_executable(executable): for win in Window.get_all_windows(): if executable in win.executable: return win.executable
def _process_recognition(self,node,extras): window=Window.get_foreground() print 'Window details' print 'title=',window.title print 'executable=',window.executable print 'window=',window