-
Notifications
You must be signed in to change notification settings - Fork 3
/
ZoteroWindowsPicker_v.0.9.2.ahk
439 lines (366 loc) · 11.1 KB
/
ZoteroWindowsPicker_v.0.9.2.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#SingleInstance, Force
#NoEnv
; Menu, Tray, Icon, ZoteroWinPicker.ico
#Persistent
Menu, Tray, NoStandard
Menu, Tray, Add, Zotero Picker Settings, openSettings
Menu, Tray, Add
Menu, Tray, Add, Restart, reload
Menu, Tray, Add
Menu, Tray, Add, About, about
Menu, Tray, Add
Menu, Tray, Add, Exit, exit
v = v.0.9.2
iniFileName = ZoteroWindowsPicker.ini
pickerWindowTitle = Quick Format Citation
formatOptions := {"Latex":"latex","Biblatex":"biblatex","MultiMarkdown":"mmd","Pandoc":"pandoc","Zotero ODF Scan":"scannable-cite","Formatted Zotero Quick Citation":"formatted-citation","Formatted Zotero Quick Bibliography":"formatted-bibliography","JSON":"json"}
odtLocatorOptions := {"Article":"art.","Chapter":"ch.","Subchapter":"subch.","Column":"col.","Figure":"fig.","Line":"l.","Note":"n.","Issue":"no.","Opus":"op.","Page":"p.","Paragraph":"para.","Subparagraph":"subpara.","Part":"pt.","Rule":"r.","Section":"sec.","Subsection":"subsec.","Section":"Sec.","Sub verbo":"sv.","Schedule":"sch.","Title":"tit.","Verse":"vrs.","Volume":"vol."}
Start := A_TickCount
loop, 26
{
letterList .= chr(A_Index + 64) . "|"
}
loop, 10
{
letterList .= chr(A_Index + 47) . "|"
}
loop, 12
{
letterList .= "F" . A_Index . "|"
}
for key, value in formatOptions
{
formatOptionList .= key . "|"
}
letterListArray := StrSplit(letterList,"|")
formatOptionListArray := StrSplit(formatOptionList,"|")
IfNotExist, %iniFileName%
{
MsgBox, ,Zotero Windows Picker,
(
Initialial Settings
)
currentFormat := "Formatted Zotero Quick Bibliography"
currentShortcut := "+!F"
locatorCheck := 0
insertCheck := 1
notificationCheck := 1
openSettings()
} else {
readIni()
}
if (currentFormat = "Zotero ODF Scan")
{
for key, value in odtLocatorOptions
{
odtLocatorOptionsList .= key . "|"
}
}
formatedShortcut := formatShortcut(currentShortcut)
Menu, Tray, Tip , Format:`n %currentFormat%`n`nShortcut:`n %formatedShortcut%`n
currentFormatString := "format=" . formatOptions[currentFormat]
requestString := "http://127.0.0.1:23119/better-bibtex/cayw?" . currentFormatString
try{
Hotkey, %currentShortcut%, getRef
} catch {
MsgBox, , Error,
(
Error with %iniFileName% settings file.
Delete %iniFileName% and restart.
)
}
return
getRef:
IfWinExist, Quick Format Citation
WinActivate, Quick Format Citation
global tempClipboard := Clipboard
Clipboard := ""
IfWinNotExist, ahk_exe zotero.exe
{
MsgBox, , Zotero Not Found, Please Launch Zotero for Windows First.
Return
}
WinGetActiveTitle, activeWindow
req := ComObjCreate("WinHttp.WinHttpRequest.5.1")
req.SetTimeouts(0, 60000, 30000, 120000)
req.Open("GET", requestString, true)
req.SetRequestHeader("Content-Type", "application/json")
try
{
req.Send()
req.WaitForResponse(120)
} catch {
WinClose, %pickerWindowTitle%
return
}
if (!req.ResponseText){
return
}
if (InStr(req.ResponseText, "CAYW failed: Error: scannable-cite")) {
errorMessage := req.ResponseText
MsgBox,, Remainder,
(
To use ODF format, you need to install "RTF/ODF Scan for Zotero" plugin.
See: https://zotero-odf-scan.github.io/zotero-odf-scan/
To switch to other formats, go to Settings.
Error Message: %errorMessage%
)
return
} else if (InStr(req.ResponseText, "CAYW failed: Error: formatted")) {
errorMessage := req.ResponseText
MsgBox,, Remainder,
(
To use formatted citation, set Zotero default quick-copy format to a citation style first.
If you want to switch to other formats, go to Settings.
Error Message: %errorMessage%
)
return
}
else if (InStr(req.ResponseText, "No endpoint")) {
errorMessage := req.ResponseText
MsgBox,, Remainder,
(
Cannot connect to Better BibTeX for Zotero add-on. Install it and restart Zotero.
See: https://retorque.re/zotero-better-bibtex/
Error Message: %errorMessage%
)
return
}
else if (InStr(req.ResponseText, "CAYW failed: translation")) {
errorMessage := req.ResponseText
MsgBox,, Remainder,
(
Cannot find ODT translator. Check or reinstall Zotero ODT Scan add-on.
See: https://zotero-odf-scan.github.io/zotero-odf-scan/
Error Message: %errorMessage%
)
return
}
citationStrings := req.ResponseText
if (currentFormat = "Zotero ODF Scan" && locatorCheck = 1)
{
IfWinExist, Locator Information
Gui,2:Destroy
rList := formatResult(citationStrings)
for index, value in rList
{
Gui, 2: Add, Text, , %index%. %value%
Gui, 2: Add, DropDownList, vlocator%index% Choose9, %odtLocatorOptionsList%
Gui, 2: Add, Edit, vlocatorNumber%index%
}
Gui,2: Add, Button, w120 default, OK
Gui,2: Add, Button, w120 x+5 , Cancel
Gui,2: Show,, Locator Information
return
2GuiClose:
2ButtonCancel:
Gui 2: Destroy
return
2ButtonOK:
Gui,2: Submit
Gui 2: Destroy
pList := []
for index, value in rList
{
locatorString := ""
locator := "locator" index
locator := %locator%
ln := "locatorNumber" index
ln := %ln%
if (ln){
locatorString := "|" . " " . odtLocatorOptions[locator] . " " . ln
value := RegExReplace(value, "\|", locatorString,, 1, InStr(value, "|",,, 2))
pList.Push(value)
}
; Add an alternative if the user it not inputing any page numbers
else {
locatorString := "|"
value := RegExReplace(value, "\|", locatorString,, 1, InStr(value, "|",,, 2))
pList.Push(value)
}
}
citationStrings := ""
for key, value in pList
{
citationStrings .= value
}
outputResult(citationStrings)
return
} else if (currentFormat = "Pandoc") {
; Added '[' ']' around Pandoc format output.
citationStrings := "[" . citationStrings . "]"
outputResult(citationStrings)
return
}
else {
outputResult(citationStrings)
return
}
return
outputResult(citationStrings) {
global
Clipboard := ""
Clipboard := citationStrings
ClipWait
; WinActivate, %activeWindow%
; WinWaitActive, %activeWindow%
if (insertCheck = 1)
{
if (notificationCheck = 1)
{
TrayTip, Citation Inserted, %citationStrings%, 4,
}
Sleep, 500
Send, ^v
WinWait A
Clipboard := tempClipboard
}else {
if (notificationCheck = 1)
{
TrayTip, Saved to Clipboard, %citationStrings%, 4,
}
}
}
return
formatResult(r) {
r := StrReplace(r,"}{","};{")
rList := StrSplit(r,";")
return rList
}
return
About() {
MsgBox, , Zotero Windows Picker %v%,
(
Requirement:
1. Zotero For Window
2. Better BibTeX for Zotero
3. RTF/ODF Scan for Zotero (for ODF format, optional)
Bo An
2019-20
Scripted in AHK.
)
}
readIni() {
global
IniRead, currentFormat, %iniFileName%, Settings, currentFormat
IniRead, currentShortcut, %iniFileName%, Settings, currentShortcut
IniRead, locatorCheck, %iniFileName%, Settings, locatorCheck
IniRead, insertCheck, %iniFileName%, Settings, insertCheck
IniRead, notificationCheck, %iniFileName%, Settings, notificationCheck
}
formatShortcut(s){
s := StrReplace(s, "+", "Shift + ")
s := StrReplace(s, "!", "Alt + ")
s := StrReplace(s, "^", "Ctrl + ")
return s
}
openSettings(){
global
IfWinExist, Zotero Windows Picker Settings
{
; WinActivate, Zotero Windows Picker Settings
return
}
IfExist, %iniFileName%
readIni()
checkCtrl := InStr(currentShortcut, "^") ? "Checked" : ""
checkAlt := InStr(currentShortcut, "!") ? "Checked" : ""
checkShift := InStr(currentShortcut, "+") ? "Checked" : ""
currentKeyChoice := RegExReplace(currentShortcut,"[!+^]")
checkLocator := locatorCheck ? "Checked" : ""
checkInsert := insertCheck ? "Checked" : ""
checkNotification := notificationCheck ? "Checked": ""
for index, value in letterListArray
if (value = currentKeyChoice)
checkKey := "Choose" . index
for index, value in formatOptionListArray
if (value = currentFormat)
checkFormat := "Choose" . index
; GUI for Settings
Gui, Add, Text, w75 y10 , Hotkey:
Gui, Add, Checkbox, w65 x+15 vctrlCheck %checkCtrl% , Ctrl
Gui, Add, Checkbox, w65 x+1 vshiftCheck %checkShift% , Shift
Gui, Add, Checkbox, w65 x+1 valtCheck %checkAlt% , Alt
Gui, Add, Text, w45 x+1 , +
Gui, Add, DropDownList, w90 x+1 vchosenLetter %checkKey% , %letterList%
Gui, Add, Text, x10,
Gui, Add, Text, w75 x10, Citation Format:
Gui, Add, DropDownList, w200 x+25 vchosenFormat %checkFormat% , %formatOptionList%
Gui, Add, Link, x110, * Use the ODT format if you want link and update your citations with Zotero.
Gui, Add, Link, x110, * For details of different formats, see <a href="https://retorque.re/zotero-better-bibtex/citing/cayw/">Better BibTex documentation</a>.
Gui, Add, Text, x10,
Gui, Add, Text, w75 x10, Zotero ODT Scan:
Gui, Add, Checkbox, x+15 vlocatorCheck %checkLocator%, Input Locator Information (add informations like pages etc during insertion)
Gui, Add, Link, x110, * For detailed Zotero ODT Scan instructions, see <a href="https://zotero-odf-scan.github.io/zotero-odf-scan/">the Add-on website</a>
Gui, Add, Text, x10,
Gui, Add, Text, w75 x10, Export:
Gui, Add, Checkbox, x+15 vinsertCheck %checkInsert%, Automatically Insert Citations (uncheck to save to clipboard only)
Gui, Add, Text, x10,
Gui, Add, Text, w75 x10, Notification:
Gui, Add, Checkbox, x+15 vnotificationCheck %checkNotification%, Show Notification
Gui, Add, Text, x10,
Gui, Add, Button, w160 default, Save
Gui, Add, Button, w140 x+10, Cancel
Gui, Add, Link, x+10 w130 Right, %v% by Bo An via <a href="https://www.autohotkey.com">AHK</a>.
Gui, Show,, Zotero Windows Picker Settings
return
ButtonCancel:
GuiClose:
Gui, Destroy
return
ButtonSave:
Gui, Submit
Gui, Destroy
if(ctrlCheck){
chosenShortcut .= "^"
}
if(shiftCheck){
chosenShortcut .= "+"
}
if(altCheck){
chosenShortcut .= "!"
}
chosenShortcut .= chosenLetter
IniWrite, %chosenFormat%, %iniFileName%, Settings, currentFormat
IniWrite, %chosenShortcut%, %iniFileName%, Settings, currentShortcut
IniWrite, %locatorCheck%, %iniFileName%, Settings, locatorCheck
IniWrite, %insertCheck%, %iniFileName%, Settings, insertCheck
IniWrite, %notificationCheck%, %iniFileName%, Settings, notificationCheck
IfNotExist, %iniFileName%
{
MsgBox, , Error,
(
Cannot create %iniFileName% to save settings.
Please run again as Administrator.
)
ExitApp
} else {
MsgBox, Settings Saved
}
Reload
}
OnWin(Event, Hwnd)
{
Static This_Func_Name := "OnWin"
Static RunAtScriptExecution1 := DllCall( "RegisterShellHookWindow", UInt, A_ScriptHwnd)
Static SH_MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
Static RunAtScriptExecution2 := OnMessage(SH_MsgNum, Func(This_Func_Name), 1000)
; ((event = 32772) || (event = 4))
if (event = 1)
{
WinGetTitle, Title_Found, % "ahk_id" Hwnd
if (Title_Found = "Quick Format Citation")
{
IfWinNotActive, Quick Format Citation
{
WinActivate, Quick Format Citation
}
}
}
}
reload(){
reload
}
exit() {
ExitApp
}