AutoMonkey 0.4.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,201 @@
1
+ from time import sleep
2
+
3
+ from sys import platform
4
+
5
+ if platform == "win32":
6
+ from os import startfile # It is used
7
+ elif platform == "linux":
8
+ from subprocess import call
9
+ def startfile(file):
10
+ call(["xdg-open", file])
11
+ from os import system
12
+ from os.path import isfile
13
+
14
+ from re import search
15
+
16
+ from subprocess import Popen
17
+
18
+ if platform == "win32":
19
+ from win32con import WM_CLOSE
20
+ from win32con import SW_RESTORE
21
+ from win32con import SW_MINIMIZE
22
+ from win32con import SW_MAXIMIZE
23
+
24
+ from win32gui import IsIconic
25
+ from win32gui import ShowWindow
26
+ from win32gui import FindWindow
27
+ from win32gui import PostMessage
28
+ from win32gui import EnumWindows
29
+ from win32gui import GetWindowText
30
+ from win32gui import SetForegroundWindow
31
+
32
+ from pyautogui import keyUp
33
+ from pyautogui import keyDown
34
+ from .key_funcs import hotkey as keys2 # keyboard-layout aware
35
+
36
+ from .utils import copy
37
+
38
+ def msoffice_replace(replace_this: str, with_this: str, match_case: bool = True, whole_words: bool = True, delay_factor: float = 1):
39
+ """Search and replace in all MS Office Software. No Guarantees.
40
+
41
+ Args:
42
+ replace_this (str): string to be replaced
43
+ with_this (str): new string
44
+ delay_factor (float, optional): Delay factor in case
45
+ the default sleep times for waiting that the replacement
46
+ is finished are too fast. Defaults to 1.
47
+ """
48
+ copy(replace_this)
49
+ sleep(0.2 * delay_factor)
50
+ keys2('ctrl', 'h')
51
+ sleep(0.2 * delay_factor)
52
+ if match_case or whole_words:
53
+ keys2('alt', 'm')
54
+ sleep(0.2 * delay_factor)
55
+ if match_case:
56
+ keys2('alt', 'h')
57
+ sleep(0.2 * delay_factor)
58
+ if whole_words:
59
+ keys2('alt', 'y')
60
+ sleep(0.2 * delay_factor)
61
+ keys2('alt', 'n')
62
+ sleep(0.2 * delay_factor)
63
+ keys2('ctrl', 'v')
64
+ sleep(0.2 * delay_factor)
65
+ copy(with_this)
66
+ sleep(0.2 * delay_factor)
67
+ keys2('alt', 'i')
68
+ sleep(0.2 * delay_factor)
69
+ keys2('ctrl', 'v')
70
+ sleep(0.2 * delay_factor)
71
+ keys2('alt', 'a')
72
+ sleep(0.2 * delay_factor)
73
+ keys2('enter')
74
+ sleep(0.2 * delay_factor)
75
+ keys2('enter')
76
+ sleep(0.2 * delay_factor)
77
+ keys2('esc')
78
+ sleep(0.2 * delay_factor)
79
+ keys2('esc')
80
+ sleep(0.2 * delay_factor)
81
+
82
+
83
+ if platform == "win32":
84
+ class WindowManager:
85
+ """Window Manager
86
+ """
87
+ def __init__(self):
88
+ self._handle = None
89
+
90
+ def get_window_by_class(self, class_name, window_name=None):
91
+ """Find a window by the class name
92
+ """
93
+ self._handle = FindWindow(class_name, window_name)
94
+
95
+ def _parse_windows(self, hwnd, pattern):
96
+ """Pass to EnumWindows() to check all opened windows
97
+ """
98
+ if search(pattern, str(GetWindowText(hwnd))) is not None:
99
+ self._handle = hwnd
100
+
101
+ def get_window_by_title(self, pattern):
102
+ """Find a window whose title matches the given regex pattern
103
+ """
104
+ self._handle = None
105
+ EnumWindows(self._parse_windows, pattern)
106
+
107
+ def focus(self):
108
+ """Bring focus to the selected window
109
+ """
110
+ # SetForegroundWindow works well only after pressing alt
111
+ keyDown('alt')
112
+ SetForegroundWindow(self._handle)
113
+ self.restore()
114
+ keyUp('alt')
115
+
116
+ def minimize(self):
117
+ if not IsIconic(self._handle):
118
+ ShowWindow(self._handle, SW_MINIMIZE)
119
+
120
+ def restore(self):
121
+ if IsIconic(self._handle):
122
+ ShowWindow(self._handle, SW_RESTORE)
123
+
124
+ def maximize(self):
125
+ ShowWindow(self._handle, SW_MAXIMIZE)
126
+
127
+ def close(self):
128
+ PostMessage(self._handle, WM_CLOSE, 0, 0)
129
+
130
+ def close(title: str):
131
+ """Close a window
132
+ """
133
+ win_man = WindowManager()
134
+ win_man.get_window_by_title(f".*?{title}.*?")
135
+ win_man.close()
136
+
137
+ def minimize(title: str):
138
+ """Minimize a window
139
+ """
140
+ win_man = WindowManager()
141
+ win_man.get_window_by_title(f".*?{title}.*?")
142
+ win_man.minimize()
143
+
144
+ def maximize(title: str):
145
+ """Minimize a window
146
+ """
147
+ win_man = WindowManager()
148
+ win_man.get_window_by_title(f".*?{title}.*?")
149
+ win_man.maximize()
150
+
151
+ def restore(title: str):
152
+ """Restore a window
153
+ """
154
+ win_man = WindowManager()
155
+ win_man.get_window_by_title(f".*?{title}.*?")
156
+ win_man.restore()
157
+
158
+ def focus(title: str):
159
+ """Bring Focus to a window
160
+ """
161
+ win_man = WindowManager()
162
+ win_man.get_window_by_title(f".*?{title}.*?")
163
+ win_man.focus()
164
+ else:
165
+ # These window-management features rely on the Win32 API and have no
166
+ # Linux equivalent. Keep the module importable on other platforms and
167
+ # fail with a clear message if one of them is actually used.
168
+ def _unsupported(name: str):
169
+ raise NotImplementedError(
170
+ f"'{name}' is only supported on Windows (Win32 API)."
171
+ )
172
+
173
+ def close(title: str):
174
+ _unsupported("close")
175
+
176
+ def minimize(title: str):
177
+ _unsupported("minimize")
178
+
179
+ def maximize(title: str):
180
+ _unsupported("maximize")
181
+
182
+ def restore(title: str):
183
+ _unsupported("restore")
184
+
185
+ def focus(title: str):
186
+ _unsupported("focus")
187
+
188
+
189
+ def open_app(app: str):
190
+ """Open an application
191
+
192
+ Args:
193
+ app_path (str): path to the application
194
+ """
195
+ if isfile(app):
196
+ Popen(app)
197
+ else:
198
+ try:
199
+ system(f"start {app}")
200
+ except Exception as err:
201
+ raise Exception(f"Could not open {app} because of {err}") from err