-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsDialogHandler.py
More file actions
104 lines (83 loc) · 3.13 KB
/
WindowsDialogHandler.py
File metadata and controls
104 lines (83 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
"""
Windows Dialog Handler - Python Edition
Opensource automation tool for setting file paths in Windows file dialogs
"""
import sys
import time
import ctypes
from ctypes import wintypes
# Windows API functions
FindWindow = ctypes.windll.user32.FindWindowW
GetDlgItem = ctypes.windll.user32.GetDlgItem
SendMessage = ctypes.windll.user32.SendMessageW
Sleep = ctypes.windll.kernel32.Sleep
# Windows constants
WM_SETTEXT = 12
BM_CLICK = 245
IDOK = 1
def main():
print("Welcome to Windows Dialog Handler! Opensource automation tool by Meij Racpan")
print("You have to follow the argument descriptors as below:")
print("<window class name> <window title> <edit box control ID> <file path to set> <set delay in milliseconds>")
print("Default values will be used if no arguments are provided.")
print("Example: WindowsDialogHandler.py #32770 \"Choose File to Upload\" 1148 \"C:\\Path\\To\\File.txt\" 500")
print(f"Argument count is {len(sys.argv)}")
# Default values
class_name = "#32770"
window_title = "Choose File to Upload"
edit_box_id = 0x47C
file_path = "C:\\WorkingFolder\\Programming\\IE2\\ForUpload.txt"
delay = 500
# Check if arguments are provided and use them, otherwise use defaults
if len(sys.argv) >= 2:
class_name = sys.argv[1]
print(f"Using window class from argument: {class_name}")
if len(sys.argv) >= 3:
window_title = sys.argv[2]
print(f"Using window title from argument: {window_title}")
if len(sys.argv) >= 4:
try:
edit_box_id = int(sys.argv[3], 0) # 0 base allows hex with 0x prefix
print(f"Using edit box control ID from argument: {sys.argv[3]}")
except ValueError:
print(f"Invalid edit box ID: {sys.argv[3]}")
return 0
if len(sys.argv) >= 5:
file_path = sys.argv[4]
print(f"Using file path from argument: {file_path}")
if len(sys.argv) >= 6:
try:
delay = int(sys.argv[5])
print(f"Using delay from argument: {sys.argv[5]} ms")
except ValueError:
print(f"Invalid delay: {sys.argv[5]}")
return 0
# Find the window
hwnd = FindWindow(class_name, window_title)
if hwnd:
print("Dialog Found!")
# Get the edit box handle
hdit = GetDlgItem(hwnd, edit_box_id)
if hdit:
# Add a small delay to ensure the dialog is ready
Sleep(delay)
# Set the file path to the Edit Box using SendMessage
SendMessage(hdit, WM_SETTEXT, 0, file_path)
print("File Path Set to Edit Box")
# Click the Open Button
hbtn = GetDlgItem(hwnd, IDOK)
if hbtn:
# Send Click Message to the Button
SendMessage(hbtn, BM_CLICK, 0, 0)
else:
print("Message: Open Button Not Found!")
else:
print("Message: Edit Box Not Found!")
print("Message: Successful!")
return 1
else:
print("Message: Dialog Not Found!")
return 0
if __name__ == "__main__":
sys.exit(main())