-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoldenDict Select To Translate.ahk
132 lines (108 loc) · 4.46 KB
/
GoldenDict Select To Translate.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
; Translate Selected text using GoldenDict
; Ignore attempts to launch when the script is already running
#SingleInstance Ignore
; Utility functions definitions: START
FindGoldenDictPath() {
; Find a installed GoldenDict
PreviousRegView := A_RegView
SetRegView, 32
RegRead, FullFilePath, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\GoldenDict, UninstallString
SetRegView, %PreviousRegView%
if (!ErrorLevel) {
SplitPath, FullFilePath,, InstallPath
Return, InstallPath . "\GoldenDict.exe"
}
; If GoldenDict is not installed normally, check whether a scoop installed version exist
ScoopFilePath := "C:\Users\" . A_UserName . "\scoop\apps\goldendict\current\GoldenDict.exe"
if FileExist(ScoopFilePath) {
Return, ScoopFilePath
}
Throw, "No GoldenDict installation could be found. You could still modify this .ahk file manually to provide a valid GoldenDictPath."
}
CloseGoldenDict() {
Process, Close, GoldenDict.exe
}
; You can optionally comment out all following lines contains `ClipboardSave` to let the copied word remain in the clipboard
TranslateRoutine() {
global GoldenDictPath
; Don't lookup word in previously specified window classes
if WinActive("ahk_group IgnoreWindowsGroup") {
Return
}
ClipboardSave := ClipboardAll
Clipboard :=
SendInput, ^c
ClipWait, 0
if (!ErrorLevel) {
Selected := Trim(Clipboard, " `t`r`n")
}
; Clipboard := ClipboardSave
Clipboard := Selected ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; My Customization
; Free the memory in case the Clipboard was very large
ClipboardSave :=
if (ErrorLevel) {
return
}
SelectedLength := StrLen(Selected)
if (SelectedLength <= 0 or SelectedLength > 50) {
return
}
Selected := """" . Selected . """"
Run, %GoldenDictPath% %Selected%
}
; Utility functions definitions: END
; Main Thread: START
GoldenDictPath := FindGoldenDictPath()
; Run GoldenDict if it's not running
Process, Exist, GoldenDict.exe
if (!ErrorLevel) {
Run, %GoldenDictPath%
; Unfortunately, run options like Max|Min|Hide won't work for GoldenDict
}
; Register clean up function to be called on exit
OnExit("CloseGoldenDict")
; Use Window Spy shipped with AutoHotKey installation to find other windows you want this script to ignore, then add them below
GroupAdd, IgnoreWindowsGroup, ahk_class ConsoleWindowClass ; Console
GroupAdd, IgnoreWindowsGroup, ahk_exe Code - Insiders.exe ; Visual Studio Code - Insiders
GroupAdd, IgnoreWindowsGroup, ahk_exe Code.exe ; Visual Studio Code
GroupAdd, IgnoreWindowsGroup, ahk_exe Explorer.EXE ; File Explorer
GroupAdd, IgnoreWindowsGroup, ahk_exe GoldenDict.exe ; GoldenDict
GroupAdd, IgnoreWindowsGroup, ahk_exe mpv.exe ; MPV media player
GroupAdd, IgnoreWindowsGroup, ahk_exe ScreenClippingHost.exe ; Snip & Sketch
GroupAdd, IgnoreWindowsGroup, ahk_exe vlc.exe ; VLC media player
GroupAdd, IgnoreWindowsGroup, ahk_exe WindowsTerminal.exe ; Windows Terminal
GroupAdd, IgnoreWindowsGroup, ahk_exe qbittorrent.exe ; qBittorrent
; Set coordinate mode for mouse to be relative to the entire screen to correctly handle the position of clicks' which activate another window
CoordMode, Mouse, Screen
; Initialize global variables
MouseDownTime := 0
MouseDownPositionX := 0
MouseDownPositionY := 0
DoubleClickTime := DllCall("GetDoubleClickTime")
DoubleClicked := False
; Optional hotkey to toggle this script's suspending. Remove the semicolon in the next line if you need it
; F8::Suspend
; Main thread: END
~LButton::
MouseGetPos MousePositionX, MousePositionY
; If Clicked within DoubleClickTime and mouse didn't moved
if (A_TickCount - MouseDownTime < DoubleClickTime and Abs(MousePositionX - MouseDownPositionX) + Abs(MousePositionY - MouseDownPositionY) < 5) {
DoubleClicked := True
}
; Record this click
MouseDownTime := A_TickCount
MouseDownPositionX := MousePositionX
MouseDownPositionY := MousePositionY
return
~LButton Up::
if (DoubleClicked) {
DoubleClicked := False
TranslateRoutine()
return
}
MouseGetPos MouseUpPositionX, MouseUpPositionY
; If mouse moved
if (Abs(MouseUpPositionX - MouseDownPositionX) + Abs(MouseUpPositionY - MouseDownPositionY) > 5) {
TranslateRoutine()
}
return