forked from 7plus/7plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clipboard.ahk
374 lines (355 loc) · 13.6 KB
/
Clipboard.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
;Called when clipboard changes, used for "Paste text/image as file" functionality and for clipboard manager
;To use the clipboard without triggering these features, set MuteClipboardList := true before writing to clipboard
OnClipboardChange:
if(!ApplicationState.ClipboardListenerRegistered)
OnClipboardChange()
return
OnClipboardChange()
{
global MuteClipboardList, ClipboardList
RaiseEvent("ClipboardChange")
if(MuteClipboardList)
{
FileAppend, %A_Now%: Clipboard changed to %Clipboard% but it's muted`n, %A_Temp%\7plus\Log.log
return
}
; Some programs can be ignored to protect privacy of passwords, ...
; Note: This fails in certain cases where AHK needs to temporarily use the clipboard while sensitive content is on it.
; An example is the GetSelectedText() function. It might be useful to try waiting in this function to skip the event
; which apparently doesn't work right now.
owner := GetProcessName(DllCall("GetClipboardOwner"))
Debug("owner: " owner " index: " ToArray(Settings.Misc.IgnoredPrograms, "|").indexOf(owner))
if(ToArray(Settings.Misc.IgnoredPrograms, "|").indexOf(owner))
return
if(WinActive("ahk_group ExplorerGroup") || WinActive("ahk_group DesktopGroup")|| IsDialog())
CreateFileFromClipboard()
else
ShowTip({Min : 1, Max : 2})
text := ReadClipboardText()
FileAppend, %A_Now%: Clipboard changed to %text%`n, %A_Temp%\7plus\Log.log
if(text && IsObject(ClipboardList))
ClipboardList.Push(text)
return
}
;Creates a file for pasting text/image in explorer
CreateFileFromClipboard()
{
global MuteClipboardList
static CF_HDROP := 0xF
MuteClipboardList := true
if(!DllCall("IsClipboardFormatAvailable", "Uint", CF_HDROP))
{
If(DllCall("IsClipboardFormatAvailable", "Uint", 2) && Settings.Explorer.PasteImageAsFileName !="" )
{
ShowTip(3)
PasteImageAsFilePath := A_Temp "\" Settings.Explorer.PasteImageAsFileName
success := WriteClipboardImageToFile(PasteImageAsFilePath, Settings.Misc.ImageQuality)
if(success)
CopyToClipboard(PasteImageAsFilePath, false)
}
else if (DllCall("IsClipboardFormatAvailable", "Uint", 1) && Settings.Explorer.PasteTextAsFileName !="" )
{
ShowTip(3)
PasteTextAsFilePath := A_Temp "\" Settings.Explorer.PasteTextAsFileName
success := WriteClipboardTextToFile(PasteTextAsFilePath)
if(success)
CopyToClipboard(PasteTextAsFilePath, false)
}
}
else
{
ShowTip({Min : 14, Max : 15})
Debug("a file is already in the clipboard")
}
WaitForEvent("ClipboardChange", 100)
MuteClipboardList := false
}
;Read real text (=not filenames, when CF_HDROP is in clipboard) from clipboard
ReadClipboardText()
{
if((!A_IsUnicode && DllCall("IsClipboardFormatAvailable", "Uint", 1)) || (A_IsUnicode && DllCall("IsClipboardFormatAvailable", "Uint", 13))) ;CF_TEXT = 1 ;CF_UNICODETEXT = 13
{
DllCall("OpenClipboard", "Ptr", 0)
htext:=DllCall("GetClipboardData", "Uint", A_IsUnicode ? 13 : 1, "Ptr")
ptext := DllCall("GlobalLock", "Ptr", htext)
text := StrGet(pText, A_IsUnicode ? "UTF-16" : "cp0")
DllCall("GlobalUnlock", "Ptr", htext)
DllCall("CloseClipboard")
}
return text
}
;Reads an image from clipboard, saves it to a file, and puts CF_HDROP structure in clipboard for file pasting
WriteClipboardImageToFile(path,Quality="")
{
if(!Quality)
Quality := Settings.Misc.ImageQuality
pBitmap := Gdip_CreateBitmapFromClipboard()
if(pBitmap > 0)
{
Gdip_SaveBitmapToFile(pBitmap, path, Settings.Misc.ImageQuality)
return 1
}
return -1
}
WriteClipboardTextToFile(path)
{
text:=ReadClipboardText()
if(!text)
return -1
if(FileExist(path))
FileDelete, %path%
FileAppend , %text%, %path%, % A_IsUnicode ? "UTF-8" : ""
return 1
}
;Copies a list of files (separated by new line) to the clipboard so they can be pasted in explorer
/* Example clipboard data:
00000000 14 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000010 01 00 00 00 43 00 3A 00 5C 00 62 00 6F 00 6F 00 ....C.:.\.b.o.o. <-- I believe the 01 byte at the start of this line could indicate unicode?
00000020 74 00 6D 00 67 00 72 00 00 00 43 00 3A 00 5C 00 t.m.g.r...C.:.\.
00000030 62 00 6C 00 61 00 2E 00 6C 00 6F 00 67 00 00 00 b.l.a...l.o.g...
00000040 00 00 ..
typedef struct _DROPFILES {
DWORD pFiles;
POINT pt;
BOOL fNC;
BOOL fWide;
} DROPFILES, *LPDROPFILES;
_DROPFILES struct: 20 characters at the start
null-terminated filename list, and double-null termination at the end
*/
CopyToClipboard(files, clear, cut=0){
static CF_HDROP := 0xF
FileCount:=0
PathLength:=0
;Count files and total string length
Loop, Parse, files, `n,`r ; Rows are delimited by linefeeds (`r`n).
{
FileCount++
PathLength+=StrLen(A_LoopField)
}
pid:=DllCall("GetCurrentProcessId","Uint")
hwnd:=WinExist("ahk_pid " . pid)
DllCall("OpenClipboard", "Ptr", hwnd)
hPath := DllCall("GlobalAlloc", "uint", 0x42, "uint", 20 + (PathLength + FileCount + 1) * 2, "Ptr") ; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
pPath := DllCall("GlobalLock", "Ptr", hPath) ; Lock the moveable memory, retrieving a pointer to it.
NumPut(20, pPath+0), pPath += 16 ; DROPFILES.pFiles = offset of file list
NumPut(1, pPath+0), pPath += 4 ;fWide = 0 -->ANSI, fWide = 1 -->Unicode
Offset:=0
Loop, Parse, files, `n,`r ; Rows are delimited by linefeeds (`r`n).
offset += StrPut(A_LoopField, pPath+offset,StrLen(A_LoopField)+1,"UTF-16") * 2
DllCall("GlobalUnlock", "Ptr", hPath)
;hPath must not be freed! ->http://msdn.microsoft.com/en-us/library/ms649051(VS.85).aspx
if clear
{
DllCall("EmptyClipboard") ; Empty the clipboard, otherwise SetClipboardData may fail.
Clipwait, 1, 1
}
result:=DllCall("SetClipboardData", "uint", CF_HDROP, "Ptr", hPath) ; Place the data on the clipboard. CF_HDROP=0xF
Clipwait, 1, 1
;Write Preferred DropEffect structure to clipboard to switch between copy/cut operations
mem := DllCall("GlobalAlloc","UInt",0x42,"UInt",4, "Ptr") ; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
str := DllCall("GlobalLock","Ptr",mem)
if(!cut)
DllCall("RtlFillMemory","UInt",str,"UInt",1,"UChar",0x05)
else
DllCall("RtlFillMemory","UInt",str,"UInt",1,"UChar",0x02)
DllCall("GlobalUnlock","Ptr",mem)
cfFormat := DllCall("RegisterClipboardFormat","Str","Preferred DropEffect")
result:=DllCall("SetClipboardData","UInt",cfFormat,"Ptr",mem)
Clipwait, 1, 1
DllCall("CloseClipboard")
;mem must not be freed! ->http://msdn.microsoft.com/en-us/library/ms649051(VS.85).aspx
return
}
;Appends files to CF_HDROP structure in clipboard
AppendToClipboard( files, cut=0) {
DllCall("OpenClipboard", "Ptr", 0)
if(DllCall("IsClipboardFormatAvailable", "Uint", 1)) ;If text is stored in clipboard, clear it and consider it empty (even though the clipboard may contain CF_HDROP due to text being copied to a temp file for pasting)
DllCall("EmptyClipboard")
DllCall("CloseClipboard")
txt:=clipboard (clipboard = "" ? "" : "`n") files
Sort, txt , U ;Remove duplicates
CopyToClipboard(txt, true, cut)
return
}
Class CClipboardList extends CQueue
{
Persistent := Array()
MaxSize := 10
__new()
{
this.Load()
}
Load()
{
if(FileExist(Settings.ConfigPath "\Clipboard.xml"))
{
FileRead, xml, % Settings.ConfigPath "\Clipboard.xml"
XMLObject := XML_Read(xml)
;Convert empty and single arrays to real array
if(!XMLObject.List.MaxIndex())
XMLObject.List := IsObject(XMLObject.List) ? Array(XMLObject.List) : Array()
if(!XMLObject.Persistent.MaxIndex())
XMLObject.Persistent := IsObject(XMLObject.Persistent) ? Array(XMLObject.Persistent) : Array()
Loop % min(XMLObject.List.MaxIndex(), 10)
this.Insert(Decrypt(XMLObject.List[A_Index])) ;Read encrypted clipboard history
Loop % XMLObject.Persistent.MaxIndex()
{
Clip := RichObject()
Clip.Name := XMLObject.Persistent[A_Index].Name
Clip.Text := XMLObject.Persistent[A_Index].Text
this.Persistent.Insert(Clip)
}
}
}
Save()
{
FileDelete, % Settings.ConfigPath "\Clipboard.xml"
for index, Event in EventSystem.Events ;Check if clipboard history is actually used and don't store the history when it isn't
{
Action := Event.Actions.GetItemWithValue("Type", "Show menu")
if((Action.Menu = "ClipboardMenu" || Event.Actions.GetItemWithValue("Type", "ClipPaste")) && Event.Enabled)
{
XMLObject := Object("List", Array(), "Persistent", Array())
Loop % min(this.MaxIndex(), 10)
XMLObject.List.Insert(Encrypt(this[A_Index])) ;Store encrypted
Loop % this.Persistent.MaxIndex()
XMLObject.Persistent.Insert({Name : this.Persistent[A_Index].Name, Text : this.Persistent[A_Index].Text})
XML_Save(XMLObject, Settings.ConfigPath "\Clipboard.xml")
return
}
}
}
}
;Need separate handlers because menu index doesn't have to match array index
ClipboardHandler1:
ClipboardMenuClicked(1)
return
ClipboardHandler2:
ClipboardMenuClicked(2)
return
ClipboardHandler3:
ClipboardMenuClicked(3)
return
ClipboardHandler4:
ClipboardMenuClicked(4)
return
ClipboardHandler5:
ClipboardMenuClicked(5)
return
ClipboardHandler6:
ClipboardMenuClicked(6)
return
ClipboardHandler7:
ClipboardMenuClicked(7)
return
ClipboardHandler8:
ClipboardMenuClicked(8)
return
ClipboardHandler9:
ClipboardMenuClicked(9)
return
ClipboardHandler10:
ClipboardMenuClicked(10)
return
ClipboardMenuClicked(index)
{
global ClipboardList
if(ClipboardList[index])
PasteText(ClipboardList[index])
}
PersistentClipboardHandler:
PersistentClipboard(A_ThisMenuItemPos)
return
EditClips:
ShowSettings("Clipboard")
return
AddClip:
AddClip()
return
AddClip(Text = "")
{
iw := new CInputWindow("Enter name for clip:")
iw.Text := Text ? Text : GetSelectedText()
iw.WaitForInputAsync(new Delegate("AddClip_Callback"))
return
}
AddClip_Callback(Sender)
{
global ClipboardList
if(Sender.Result)
ClipboardList.Persistent.Insert({Name : Sender.Result, Text : Sender.Text})
}
PersistentClipboard(index)
{
global ClipboardList
text := ClipboardList.Persistent[index].Text
if(InStr(text, "%") && InStr(text, "%", false, 1, 2) && SubStr(text, InStr(text, "%") + 1, InStr(text, "%", false, 1, 2) - InStr(text, "%") - 1))
ClipVariableWindow := new CClipVariableWindow(ClipboardList.Persistent[index].DeepCopy())
else
PasteText(text)
}
Class CClipVariableWindow extends CGUI
{
editText := this.AddControl("Edit", "editText", "x10 y10 w300", "")
btnOK := this.AddControl("Button", "btnOK", "x180 y+10 Default w50", "&OK")
btnCancel := this.AddControl("Button", "btnCancel", "x+10 w70", "&Cancel")
__new(Clip)
{
static EM_SETSEL := 0x00B1
this.Clip := Clip
this.Variable := SubStr(Clip.Text, InStr(Clip.Text, "%") + 1, InStr(Clip.Text, "%", false, 1, 2) - InStr(Clip.Text, "%") - 1)
this.ActiveControl := this.editText
this.Show()
this.editText.Text := "Text for """ this.Variable """"
SendMessage, EM_SETSEL, 0, -1, , % "ahk_id " this.editText.hwnd
this.DestroyOnClose := true
this.CloseOnEscape := true
}
btnCancel_Click()
{
this.Close()
}
btnOK_Click()
{
text := this.Clip.Text
StringReplace, text, text, % "%" this.Variable "%", % this.editText.Text
this.Clip.Text := text
this.Hide()
this.Close()
if(InStr(this.Clip.Text, "%") && InStr(this.Clip.Text, "%", false, 1, 2) && SubStr(this.Clip.Text, InStr(this.Clip.Text, "%") + 1, InStr(this.Clip.Text, "%", false, 1, 2) - InStr(this.Clip.Text, "%") - 1))
ClipVariableWindow := new CClipVariableWindow(this.Clip)
else
PasteText(text)
}
}
; Write text at cursor position, overwriting selected text
PasteText(Text)
{
global MuteClipboardList
ClipboardBackup := ClipboardAll
MuteClipboardList := true
Clipboard := Text
if(WaitForEvent("ClipboardChange", 100))
{
Sleep 100 ;Some extra sleep to increase reliability
if(WinActive("ahk_class ConsoleWindowClass"))
{
CoordMode, Mouse, Screen
MouseGetPos, mx, my
CoordMode, Mouse, Relative
Click Right 40, 40
CoordMode, Mouse, Screen
MouseMove, %mx%, %my%
Send {Down 3}{Enter}
}
else
Send ^v
Sleep 20
}
else
SendInput, {Raw}%Text%
Clipboard := ClipboardBackup
WaitForEvent("ClipboardChange", 100)
MuteClipboardList := false
}