· 5 years ago · May 14, 2020, 03:24 AM
1/**
2 * clipboard manager
3 * by Blauhirn eochgls@web.de
4 * 20160301_07.54
5 * .___.
6 * {o,o}
7 * /)___)
8 * --"-"-
9 */
10#NoEnv
11#Persistent
12SetWorkingDir %A_ScriptDir%
13#include SQLiteDB.ahk
14db_name := "schinken_sqlite_data.db"
15init()
16return
17
18
19;****************************************
20;********** CUSTOM HOTKEYS **************
21
22
23
24 ; OPEN CLIPBOARD CHOICE DIALOG
25^+c::
26 show_and_choose_clipboards()
27return
28
29
30 ; SAVE COMPLETE CLIPBPOARD HISTORY INTO FILE as csv
31^+x::
32 exportClipboards("clipboards.csv")
33return
34
35
36
37
38;****************************************
39;****************************************
40
41
42
43
44init() {
45 global db, db_name
46 db := new SQLiteDB
47 db_existed := true
48 ifnotexist,%db_name%
49 db_existed := false
50 if(! db.openDB(db_name) ) {
51 err(db.errormsg " Could not create Database. Program will exit")
52 exitapp
53 }
54 onExit, onExittt
55 if(!db_existed) {
56 goSub, setupClipboardsTable
57 msgbox, Database was set up
58 }
59}
60onExittt:
61 db.closeDB()
62exitapp
63
64setupClipboardsTable:
65 /*
66 0 ID
67 1 time
68 2 processExe
69 3 clp
70 */
71 result := ""
72 if(db.getTable("SELECT * FROM clipboards LIMIT 1 `;", result) ) {
73 if(result["rows"][1]!="") {
74 msgbox, 4,, table clipboards already exists!!! DELETE and OVERWRITE?
75 ifmsgbox, no
76 return
77 if(! db.exec("DROP TABLE clipboards`;") ) {
78 err(db.errormsg " Could not delete clipboards table. Program will exit")
79 exitapp
80 }
81 }
82 }
83 db.exec("CREATE TABLE clipboards ( ID Integer PRIMARY KEY AUTOINCREMENT NOT NULL, time Integer NOT NULL DEFAULT (strftime('%s','now')), processExe Text, clp Text NOT NULL )`;")
84
85 db.exec("CREATE INDEX time_ind ON clipboards (time)`; CREATE INDEX clp_ind ON clipboards (clp)`; CREATE INDEX processExe_ind ON clipboards (processExe)`;")
86
87 loop, 10
88 db.exec("INSERT INTO clipboards (ID, processExe, clp) VALUES (" a_index ", 'x', 'empty')`;")
89return
90
91assignClps_current() {
92 global db, clps_current
93 clps_current := []
94 result := ""
95 if(! db.getTable("SELECT * FROM clipboards WHERE ID BETWEEN 1 AND 10 ORDER BY ID ASC`;", result) ) {
96 err(db.errormsg)
97 exit
98 }
99 for i,row in result["rows"]
100 clps_current.push( [ row[2], row[3], row[4] ] )
101}
102
103exportClipboards(filename) {
104 global db
105
106 recordSet := ""
107 if(! db.query("SELECT * FROM clipboards`;", recordSet) ) {
108 err(db.errormsg)
109 return
110 }
111 file := fileOpen(filename, "w")
112 if(!isobject(file)) {
113 msgbox, err("invalid file name")
114 return
115 }
116 tooltip, WRITING TO CLIPBOAORDS.TXT...................,0,0
117 header := ""
118 for i,column in recordSet.columnNames
119 header .= column ", "
120 file.writeLine(header)
121 file.writeLine("")
122 file.writeLine("")
123 while(recordSet.next(row) == true) {
124 line := ""
125 for k,v in row {
126 line .= v ", "
127 }
128 file.writeLine(line)
129 }
130 file.close()
131 run %filename%
132 tooltip
133}
134
135~^x::
136~^c::
137newClipboard_()
138return
139newClipboard_() {
140 global db, clps_current
141
142 sleep, 100
143 if(clipboard=="") ; picture, file, ..
144 return
145
146 if(clps_current=="")
147 assignClps_current()
148
149 winget, processExe, processname, A
150 clp_new_short := substr(clipboard,1,2000)
151
152 newElem := [0, processExe, clp_new_short] ; newElem[0] is not set
153
154 ; already in top 10?
155 for i,clp_current in clps_current {
156 if(newElem[3]==clp_current[3]) {
157 clps_current.removeAt(i)
158 clps_current.insertAt(1, newElem) ; shifts
159 return
160 }
161 }
162 ; new clipboard value
163
164 proc := db.EscapeStr(newElem[2])
165 clip := db.EscapeStr(newElem[3])
166 ; append to table
167 if(! db.exec("INSERT INTO clipboards (processExe, clp) VALUES (" proc ", " clip ")`;") ) {
168 err(db.errormsg)
169 return
170 }
171 result := ""
172 db.getTable("SELECT time FROM clipboards WHERE ID = (SELECT MAX(ID) FROM clipboards)`;", result)
173 ; set time from db for new entry
174 newElem[1] := result["rows"][1][1]
175
176 ; put new entry in top 10
177 clps_current.insertAt(1, newElem)
178 clps_current.pop() ; 11th
179
180 ; update top 10 in table
181 for i, clp_current in clps_current {
182 proc := db.EscapeStr(clp_current[2])
183 clip := db.EscapeStr(clp_current[3])
184 db.exec("UPDATE clipboards SET time = " clp_current[1] ", processExe = " proc ", clp = " clip " WHERE ID = " i "`;")
185 }
186}
187
188show_and_choose_clipboards() {
189 global db, clps_current
190
191 if(clps_current=="")
192 assignClps_current()
193
194 winget, activeWindow, ID, A
195
196 bla := ""
197 for i, clp_current in clps_current {
198 clp_preview := regexreplace(substr(clp_current[3], 1, 130), "[`r`n`t]", " ")
199 time_formatted := time_unix2human(clp_current[1])
200 formattime, time_formatted, %time_formatted%, hh:mm
201 bla .= "`r`n((" i ")) " time_formatted "`t" clp_preview ; clp_current[2] clp_current[3]
202 }
203 inputbox, clpbNbr, CHOOSE YOUR CLIPBOARD, %bla%,,1100,400
204 winActivate ahk_id %activeWindow%
205 if errorlevel = 1
206 return
207 if(clpbNbr < 1) {
208 run clipboards.ini
209 return
210 }
211 chosenElem := clps_current[clpbNbr]
212
213 clps_current.removeAt(clpbNbr)
214 clps_current.insertAt(1, chosenElem)
215
216 clipboard := chosenElem[3]
217
218 winActivate, ahk_id %activeWindow%
219}
220
221err(s) {
222 out := "STACK TRACE:"
223 for i, info in Traceback()
224 out .= "`r`nCalled by: " info.caller ", Line: " info.line
225 msgbox % s "`r`n`r`n" out
226}
227Traceback(actual:=false)
228{
229 r := [], i := 0, n := actual ? 0 : A_AhkVersion<"2" ? 1 : 2
230 Loop {
231 e := Exception(".", offset := -(A_Index + n))
232 if (e.What == offset)
233 break
234 r[++i] := { "file": e.file, "line": e.Line, "caller": e.What, "offset": offset + n }
235 }
236 return r
237}
238Time_unix2human(time)
239
240{
241
242 human=19700101000000
243
244 time-=((A_NowUTC-A_Now)//10000)*3600 ;时差/Time lag
245
246 human+=%time%,Seconds
247
248 return human
249
250 }
251
252Time_human2unix(time)
253
254{
255
256 time-=19700101000000,Seconds
257
258 time+=((A_NowUTC-A_Now)//10000)*3600 ;时差/Time lag
259
260 return time
261
262 }