-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDeepLDesktop.ahk
163 lines (141 loc) · 5.04 KB
/
DeepLDesktop.ahk
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
DeepLDesktop(dqDialogText)
{
Process, Exist, DeepL.exe
if ErrorLevel
{
;; Check if user has DeepL minimized. If so, open it and send it to the back of the z-axis.
;; If it's in the system tray and not open, alert the user and exit the app. Re-activating
;; the window doesn't work with DeepL - you just get a permanent black box until you relaunch.
Gui, 2:Default
WinActivate, ahk_exe DeepL.exe
if !WinActive("ahk_exe DeepL.exe")
{
MsgBox DeepL is in your system tray, but not your taskbar, which makes it impossible for us to talk to it.`n`nOpen DeepL from the system tray and ensure it isn't minimized and try again.`n`nExiting app.
ExitApp
}
else
WinActivate, ahk_exe DQXGame.exe
WinGet, DeepLWindowState, MinMax, DeepL
if (DeepLWindowState == -1)
{
WinRestore, DeepL
WinActivate, ahk_exe DeepL.exe
sleep 50
WinSet, Bottom,,A
}
;; If HideDeepL enabled, moves the window completely off the screen so that it doesn't pop up
;; for the user during translations.
if (HideDeepL = 1)
WinMove, DeepL,, -2000, -2000
;; Open database connection
dbFileName := A_ScriptDir . "\dqxtrl.db"
db := New SQLiteDB
;; Show overlay if AutoHideOverlay enabled
if (AutoHideOverlay = 1)
Gui, Show, NA
fullDialog :=
for index, sentence in StrSplit(dqDialogText, "`n`n", "`r")
{
;; See if we have an entry available to grab from before sending the request to DeepL.
result :=
query := "SELECT " . Language . " FROM dialog WHERE jp = '" . sentence . "';"
if !db.OpenDB(dbFileName)
MsgBox, 16, SQLite Error, % "Msg:`t" . db.ErrorMsg . "`nCode:`t" . db.ErrorCode
if !db.GetTable(query, result)
MsgBox, 16, SQLite Error, % "Msg:`t" . db.ErrorMsg . "`nCode:`t" . db.ErrorCode
result := result.Rows[1,1]
;; If no matching line was found in the database, query DeepL.
if !result
{
;; Get the full window's position as user could have resized
;; DPI scaling can mess up hardcoded coords, so multiply where
;; to move.
WinGetPos, X, Y, W, H, DeepL
cNewW := (W * .20)
cNewH := (H * .23)
Clipboard := sentence
;; Interact with DeepL window to send Japanese text
SetControlDelay -1
SetKeyDelay, 10, 10
WinGetClass, class, DeepL
ControlClick, x%cNewW% y%cNewH%, ahk_class %class%,,,, Pos
ControlSend, Chrome_WidgetWin_01, ^a, ahk_class %class%
ControlSend, Chrome_WidgetWin_01, {Backspace}, ahk_class %class%
Sleep 100
ControlSend, Chrome_WidgetWin_01, ^v, ahk_class %class%
;; Clear clipboard so we know when it changes
Clipboard =
sleep 100
Loop
{
;; If DeepL takes too long to return a translation, time the attempt out.
if (A_Index > 15)
{
Gui, Font, cYellow Bold, %FontType%
GuiControl, Font, Clip
GuiControl, Text, Clip, DeepL did not return a translation in time.
Sleep 2000
Gui, Font, c%FontColor% Norm, %FontType%
GuiControl, Font, Clip
break
}
;; If the clipboard is in use, force whatever has it to let go
if (DllCall("OpenClipboard", Ptr,A_ScriptHwnd))
DllCall("CloseClipboard")
;; Click in the top left corner of DeepL to reset our tab position
ControlClick, x%cNewW% y%cNewH%, ahk_class %class%,,,, NA
ControlSend, Chrome_WidgetWin_01, {Tab}, ahk_class %class%
ControlSend, Chrome_WidgetWin_01, {Tab}, ahk_class %class%
ControlSend, Chrome_WidgetWin_01, {Enter}, ahk_class %class%
Sleep 300
clipboardContents := Clipboard
} Until clipboardContents
if (ShowFullDialog = 1)
{
fullDialog .= clipboardContents "`n`n"
GuiControl, Text, Clip, %fullDialog%
}
else
{
GuiControl, Text, Clip, %clipboardContents%
}
}
;; If we found a result in the database, use that instead
else
{
if (ShowFullDialog = 1)
{
fullDialog .= result "`n`n"
GuiControl, Text, Clip, %fullDialog%
}
else
{
GuiControl, Text, Clip, %result%
}
}
if (ShowFullDialog != 1)
{
;; Determine whether to listen for joystick or keyboard keys to continue the dialog.
if (JoystickEnabled = 1)
{
WinActivate, ahk_class AutoHotkeyGUI
Input := GetKeyPress(JoystickKeys)
}
else
{
Input := GetKeyPress(KeyboardKeys)
}
}
if (Log = 1)
FileAppend, JP: %sentence%`nEN: %clipboardContents%`n`n, txtout.txt, UTF-8
}
;; Close database connection
db.CloseDB()
;; Re-focus DQX Window
WinActivate, ahk_exe DQXGame.exe
return
}
else
msgBox Unable to locate DeepL Translate. Make sure it's installed and running, then try again.`n`nExiting app.
ExitApp
}