This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordScript.applescript
1389 lines (1023 loc) · 50 KB
/
WordScript.applescript
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- WordScript for BibDesk
---
-- MIT License
-- Copyright (c) 2020 David Wingate
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
---
-- Acknowledgements
-- The basic functionality of this software was inspired by three existing projects:
-- - BibFuse by Colin A. Smith (http://bibfuse.sourceforge.net)
-- - BibDeskToWord by Conan C. Albrecht
-- - NatBib Reference Sheet by Sébastien Merkel (http://merkel.texture.rocks/Latex/natbib.php)
-- Many thanks to the authors of these projects for sharing their hard work.
---
-- check that Word and BibDesk are ready
tell application "System Events"
set {countWord, countBibDesk} to {0, 0}
if (exists of process "Microsoft Word") is true then
set countWord to (count of documents in application "Microsoft Word")
end if
if (exists of process "BibDesk") is true then
set countBibDesk to (count of documents in application "BibDesk")
end if
end tell
if {countWord, countBibDesk} contains 0 then
activate
try
display dialog "Documents must be open in both Microsoft Word and BibDesk" buttons {"Cancel", "Open Now"} default button "Open Now"
if button returned of result contains "Open Now" then
tell application "BibDesk"
launch
end tell
delay 3
end if
on error
return
end try
end if
-- check for selection in Word to trigger quick format
tell application "Microsoft Word"
if content of text object of first paragraph of selection contains "ADDIN" then
select text object of first paragraph of selection -- select recently edited reference
end if
set selectionStart to (selection start of selection)
set selectionEnd to (selection end of selection)
end tell
if selectionStart is not selectionEnd then
readBibliographySettings()
set bibliographySettings to result
if bibliographySettings is {} then
defaultSettings()
set bibliographySettings to result
writeBibliographySettings(bibliographySettings)
set bibliographySettings to result
if bibliographySettings is {} then
return
end if
end if
formatReferences(bibliographySettings)
return
end if
-- open WordScript Menu
activate
with timeout of 0 seconds
choose from list {"Format References", "Unformat References", "Fill Empty References", "Tools", "Document Settings"} with title "WordScript" with prompt "You should save your work first!" default items "Format References" cancel button name {"Exit"} OK button name {"Choose"}
set menuChoice to result
end timeout
if menuChoice is false then
return
end if
if menuChoice contains "Format References" then
readBibliographySettings()
set bibliographySettings to result
if bibliographySettings is {} then
defaultSettings()
set bibliographySettings to result
writeBibliographySettings(bibliographySettings)
set bibliographySettings to result
if bibliographySettings is {} then
return
end if
end if
formatReferences(bibliographySettings)
else if menuChoice contains "Unformat References" then
unformatReferences()
else if menuChoice contains "Fill Empty References" then
fillEmptyReferences()
else if menuChoice contains "Document Settings" then
readBibliographySettings()
set bibliographySettings to result
if bibliographySettings is {} then
defaultSettings()
set bibliographySettings to result
writeBibliographySettings(bibliographySettings)
set bibliographySettings to result
if bibliographySettings is {} then
return
end if
end if
settingsMenu(bibliographySettings)
if result is not bibliographySettings then
set bibliographySettings to result
writeBibliographySettings(bibliographySettings)
set bibliographySettings to result
if bibliographySettings is {} then
return
end if
try
tell application "Microsoft Word"
activate
display dialog "Settings have been changed, do you wish to format the references now?"
end tell
formatReferences(bibliographySettings)
on error
return
end try
else
return
end if
else if menuChoice contains "Tools" then
try
display dialog "References must be unformatted, do you wish to unformat now?"
unformatReferences()
on error
return
end try
toolsMenu()
try
activate
display dialog "Operation complete, do you wish to format the references now?"
readBibliographySettings()
set bibliographySettings to result
formatReferences(bibliographySettings)
on error
return
end try
end if
---
on defaultSettings()
set bibDeskDocumentName to ""
set bibliographyTemplatePath to ""
set citepTemplatePath to ""
set citetTemplatePath to ""
set parenthesisOpen to "("
set parenthesisClose to ")"
set bibliographySortBy to "Cite Key" -- BibDesk field (leave blank for order of appearance)
set bibliographyStyle to "Bibliography" -- leave blank to use formatting of template file
set italicLatin to {"et al.", "ibid.", "cf.", "inter alia", "circa"}
set italicPublicationTypes to {"film", "broadcast"}
set superscriptReferences to "No"
set numberedReferences to "No" -- overrides templates and sorting
set numberSeperator to ","
return {bibDeskDocumentName, bibliographyTemplatePath, citepTemplatePath, citetTemplatePath, parenthesisOpen, parenthesisClose, bibliographySortBy, bibliographyStyle, italicLatin, italicPublicationTypes, superscriptReferences, numberedReferences, numberSeperator}
end defaultSettings
---
on readBibliographySettings()
-- global setup
tell application "Microsoft Word"
set wordDocument to active document
set selectionRange to create range wordDocument start (selection start of selection) end (selection end of selection)
set status bar to "Reading bibliography..."
end tell
-- look for a formatted bibliography
set AppleScript's text item delimiters to ""
tell application "Microsoft Word"
set fieldCount to (count of fields in wordDocument)
repeat until fieldCount is 0
set currentField to (field fieldCount of wordDocument)
set fieldCode to content of field code of currentField
if field type of currentField is field addin and word 2 of fieldCode contains "bibliography" then
set bibliographySettings to characters 21 thru -2 of fieldCode as string
exit repeat
end if
set fieldCount to fieldCount - 1
end repeat
if fieldCount is 0 then -- look for an unformatted bibliography
set findObject to find object of selection -- set up the find system in Word
set match wildcards of findObject to true
set wrap of findObject to find continue
set content of findObject to "\\\\bibliography*\\}"
select (create range wordDocument start 0 end 0)
try -- count the number of instances found
set findCount to 0
repeat while (execute find findObject)
set findCount to findCount + 1
end repeat
on error -- this can break if advanced find and replace has been used in Word
display dialog "Something is wrong with the find system in Microsoft Word, please quit Microsoft Word then re-open" buttons {"OK"} cancel button {"OK"} default button {"OK"}
return
end try
if the findCount is not 0 then -- extract raw bibiography settings
set bibliographySettings to characters 15 thru -2 of (content of selection as string) as string
else if findCount is 0 then -- no bibliography found
return {}
end if
end if
end tell
try -- extract bibliography settings as a list
set AppleScript's text item delimiters to {"<bibDeskDocumentName=", "><bibliographyTemplatePath=", "><citepTemplatePath=", "><citetTemplatePath=", "><parenthesisOpen=", "><parenthesisClose=", "><bibliographySortBy=", "><bibliographyStyle=", "><italicLatin=", "><italicPublicationTypes=", "><superscriptReferences=", "><numberedReferences=", "><numberSeperator=", ">"}
set {bibliographyCommand, bibDeskDocumentName, bibliographyTemplatePath, citepTemplatePath, citetTemplatePath, parenthesisOpen, parenthesisClose, bibliographySortBy, bibliographyStyle, italicLatin, italicPublicationTypes, superscriptReferences, numberedReferences, numberSeperator} to text items of bibliographySettings
on error -- if anything is amiss, recreate the bibliography field
tell application "Microsoft Word"
display dialog "There is something wrong with the bibliography data, please delete the bibliography and try again" buttons {"OK"} default button {"OK"}
end tell
return
end try
set AppleScript's text item delimiters to "," -- recover lists from settings
set italicLatin to text items of italicLatin
set italicPublicationTypes to text items of italicPublicationTypes
set AppleScript's text item delimiters to ""
try -- check to make sure that the template files exist
tell application "Finder"
get (exists of (POSIX file (bibliographyTemplatePath as string) as alias))
get (exists of (POSIX file (citepTemplatePath as string) as alias))
get (exists of (POSIX file (citetTemplatePath as string) as alias))
end tell
on error -- if not, recreate the bibliography field
tell application "Microsoft Word"
display dialog "Template files not found, please delete the bibliography and try again" buttons {"OK"} default button {"OK"}
end tell
return
end try
-- restore Word to its original position
tell application "Microsoft Word"
try
select selectionRange
on error -- produces error if cursor was at the end of the document
select (create range wordDocument start (selection end of selection) end (selection end of selection))
end try
set status bar to "Finished reading bibliography"
end tell
return {bibDeskDocumentName, bibliographyTemplatePath, citepTemplatePath, citetTemplatePath, parenthesisOpen, parenthesisClose, bibliographySortBy, bibliographyStyle, italicLatin, italicPublicationTypes, superscriptReferences, numberedReferences, numberSeperator}
end readBibliographySettings
---
on writeBibliographySettings(bibliographySettings)
-- global setup
tell application "Microsoft Word"
set wordDocument to active document
set selectionRange to create range wordDocument start (selection start of selection) end (selection end of selection)
set status bar to "Writing bibliography..."
end tell
tell application "BibDesk"
set bibDeskDocument to front document
end tell
-- parse bibliography settings
set AppleScript's text item delimiters to ""
set {bibDeskDocumentName, bibliographyTemplatePath, citepTemplatePath, citetTemplatePath, parenthesisOpen, parenthesisClose, bibliographySortBy, bibliographyStyle, italicLatin, italicPublicationTypes, superscriptReferences, numberedReferences, numberSeperator} to bibliographySettings
-- parse custom lists from the bibliography settings
set AppleScript's text item delimiters to ","
set italicLatin to text items of italicLatin
set italicPublicationTypes to text items of italicPublicationTypes
set AppleScript's text item delimiters to ""
-- look for formatted bibliography
tell application "Microsoft Word"
set fieldCount to (count of fields in wordDocument)
if fieldCount is not 0 then
repeat until fieldCount is 0
if content of field code of field fieldCount of wordDocument contains "ADDIN bibliography" then
set bibliographyField to field fieldCount of wordDocument
set show codes of bibliographyField to true
exit repeat
end if
set fieldCount to fieldCount - 1
end repeat
end if
end tell
-- if none found, create the bibliography field
if fieldCount is 0 then
try
tell application "Microsoft Word"
display dialog "No bibliography found, do you wish to create one now?"
set bibliographyField to make new field at end of text object of wordDocument with properties {field type:field quote, show codes:true, field text:"*"}
end tell
on error -- return the bibliography settings empty
return {}
end try
if bibDeskDocumentName is "" then -- if default settings were used, set the BibDesk library
tell application "BibDesk"
set bibDeskDocumentName to name of bibDeskDocument
end tell
end if
if bibliographyTemplatePath is "" then -- if default settings were used, set the template paths
tell application "Microsoft Word"
set bibliographyTemplatePath to POSIX path of (choose file with prompt "Please choose WordScript Template Bibliography" of type {"public.rtf", "com.microsoft.word.doc"})
end tell
end if
set AppleScript's text item delimiters to "/" -- guess the other template paths
set assumedTemplatePath to (text items 1 through -2 of bibliographyTemplatePath) as string
set AppleScript's text item delimiters to ""
tell application "Finder"
try
set citepTemplatePath to POSIX path of (POSIX file (assumedTemplatePath & "/" & "WordScript Template Author-Year (citep).txt" as string) as alias)
set citetTemplatePath to POSIX path of (POSIX file (assumedTemplatePath & "/" & "WordScript Template Year Only (citet).txt" as string) as alias)
end try
end tell
if citepTemplatePath is "" then -- otherwise choose manually
tell application "Microsoft Word"
set citepTemplatePath to POSIX path of (choose file with prompt "Please choose WordScript Template Author-Year (citep)" of type {"public.plain-text"})
end tell
end if
if citetTemplatePath is "" then
tell application "Microsoft Word"
set citetTemplatePath to POSIX path of (choose file with prompt "Please choose WordScript Template Year Only (citet)" of type {"public.plain-text"})
end tell
end if
end if
-- write the bibiography field in Word
tell application "Microsoft Word"
set AppleScript's text item delimiters to ","
set content of field code of bibliographyField to " ADDIN bibliography{" & "<bibDeskDocumentName=" & bibDeskDocumentName & "><bibliographyTemplatePath=" & bibliographyTemplatePath & "><citepTemplatePath=" & citepTemplatePath & "><citetTemplatePath=" & citetTemplatePath & "><parenthesisOpen=" & parenthesisOpen & "><parenthesisClose=" & parenthesisClose & "><bibliographySortBy=" & bibliographySortBy & "><bibliographyStyle=" & bibliographyStyle & "><italicLatin=" & italicLatin & "><italicPublicationTypes=" & italicPublicationTypes & "><superscriptReferences=" & superscriptReferences & "><numberedReferences=" & numberedReferences & "><numberSeperator=" & numberSeperator & ">}"
set AppleScript's text item delimiters to ""
try -- restore the cursor to its original position
select selectionRange
on error -- produces error if cursor was at the end of the document
select (create range wordDocument start (selection end of selection) end (selection end of selection))
end try
end tell
-- restore Word to its original position
tell application "Microsoft Word"
try
select selectionRange
on error -- produces error if cursor was at the end of the document
select (create range wordDocument start (selection end of selection) end (selection end of selection))
end try
set status bar to "Finished writing bibliography"
end tell
return {bibDeskDocumentName, bibliographyTemplatePath, citepTemplatePath, citetTemplatePath, parenthesisOpen, parenthesisClose, bibliographySortBy, bibliographyStyle, italicLatin, italicPublicationTypes, superscriptReferences, numberedReferences, numberSeperator}
end writeBibliographySettings
---
on formatReferences(bibliographySettings)
-- global setup
tell application "Microsoft Word"
set wordDocument to active document
set selectionRange to create range wordDocument start (selection start of selection) end (selection end of selection)
set status bar to "Formatting references..."
end tell
tell application "BibDesk"
set bibDeskDocument to front document
end tell
-- parse bibliography settings
set AppleScript's text item delimiters to ""
set {bibDeskDocumentName, bibliographyTemplatePath, citepTemplatePath, citetTemplatePath, parenthesisOpen, parenthesisClose, bibliographySortBy, bibliographyStyle, italicLatin, italicPublicationTypes, superscriptReferences, numberedReferences, numberSeperator} to bibliographySettings
-- parse custom lists from the bibliography settings
set AppleScript's text item delimiters to ","
set italicLatin to text items of italicLatin
set italicPublicationTypes to text items of italicPublicationTypes
set AppleScript's text item delimiters to ""
-- read the plain text template data (should this move to 'format author-year references'?)
set citepTemplate to read citepTemplatePath
set citetTemplate to read citetTemplatePath
-- intitialise document lists
set citeKeyList to {}
set publicationList to {}
set ibidCiteKeyList to {}
set italicReferences to {}
-- begin formatting the references
tell application "Microsoft Word"
activate
-- establish the format range
if (selection start of selection) is equal to (selection end of selection) then
if content of selection is "]" then -- just edited page number
set formatRange to words of sentences of selection
else
set formatRange to wordDocument -- format entire document
select (create range wordDocument start 0 end 0)
end if
else
set formatRange to selectionRange -- format selection only
end if
-- set up the find system in Word
set findObject to find object of selection
set match wildcards of findObject to true
set wrap of findObject to find continue
-- convert unformatted cite commands to ADDIN fields
repeat with citeCommand in {"citep", "citet", "citealp", "citealt", "bibliography"}
set content of findObject to "\\\\" & citeCommand & "*\\}"
select formatRange
try
repeat while (execute find findObject)
set citeCommand to content of selection
set content of selection to ""
make new field at (text object of selection) with properties {field type:field quote, show codes:true, field text:"*"}
set currentField to first field of (create range wordDocument start ((selection start of selection) - 1) end (selection end of selection))
set content of field code of currentField to " ADDIN " & characters 2 thru -1 of citeCommand
end repeat
on error -- this can break if advanced find and replace has been used in Word
display dialog "Something is wrong with the find system in Microsoft Word, please quit Microsoft Word then re-open" buttons {"OK"} default button {"OK"}
return
end try
end repeat
repeat with fieldNumber from 1 to count of fields of formatRange -- convert ADDIN fields to formatted references
set currentField to field fieldNumber of formatRange
set fieldCode to content of field code of currentField
if field type of currentField is field addin and {"citep", "citet", "citealp", "citealt"} contains word 2 of fieldCode then -- extract cite key and prefix/suffix
select currentField
set AppleScript's text item delimiters to {"{", "}"} -- parse cite key
set citeKey to text item 2 of fieldCode
set AppleScript's text item delimiters to {"[", "]"} -- parse prefix/suffix
set referencePrefix to ""
set referenceSuffix to ""
if (count of text items of fieldCode) is 5 then -- two pairs of square brackets
set referencePrefix to text item 2 of fieldCode
set referenceSuffix to text item 4 of fieldCode
else if (count of text items of fieldCode) is 3 then -- one pair of square brackets
set referenceSuffix to text item 2 of fieldCode
end if
tell application "BibDesk" -- match cite keys to publications
set multiplePublications to {} -- intitialise the multiple reference lists
set multipleCiteKeys to {}
set AppleScript's text item delimiters to "," -- multiple cite keys must be seperated by commas in BibDesk
repeat with eachInteger from 1 to count of text items of citeKey -- loop through every cite key and match to a publication
set currentCiteKey to item eachInteger of text items of citeKey
set currentPublication to (publications in bibDeskDocument whose cite key is currentCiteKey)
if (count of currentPublication) is 0 then -- no match found
tell application "Microsoft Word"
set show codes of currentField to true
select field code of currentField
select (create range wordDocument start ((selection start of selection) - 1) end (selection end of selection) + 1)
display dialog "No BibDesk reference matches this cite key: " & currentCiteKey buttons {"OK"} default button {"OK"}
return
end tell
end if
if ibidCiteKeyList is not {} and last item of ibidCiteKeyList is currentCiteKey then -- check for ibid. references
set ibidReference to true
else
set ibidReference to false
end if
set ibidCiteKeyList to ibidCiteKeyList & currentCiteKey
if citeKeyList does not contain currentCiteKey then -- list all cite keys
set citeKeyList to citeKeyList & currentCiteKey
end if
repeat with eachInteger from 1 to the count of citeKeyList -- get number for numbered references
if item eachInteger of citeKeyList is currentCiteKey then
set citeKeyNumber to eachInteger
end if
end repeat
set multiplePublications to multiplePublications & currentPublication -- list multiple publications in current field
-- list of all publications
if publicationList does not contain currentPublication then set publicationList to publicationList & currentPublication
-- list references to be made italic such as films
if italicPublicationTypes contains type of (publications in bibDeskDocument whose cite key is citeKey) then
set italicReferenceTitle to title of (publications in bibDeskDocument whose cite key is citeKey)
if italicReferences does not contain italicReferenceTitle then
set italicReferences to italicReferences & italicReferenceTitle
end if
end if
set multipleCiteKeys to multipleCiteKeys & citeKeyNumber
set citeKeyNumberText to multipleCiteKeys
end repeat
end tell
if (numberedReferences as boolean) is false then
-- format author-year references
if fieldCode contains "citep" then
if ibidReference is true then
set templatedText to "ibid."
else
tell application "BibDesk"
set templatedText to (templated text document bibDeskDocumentName using text citepTemplate for multiplePublications)
end tell
end if
set formattedReference to parenthesisOpen & referencePrefix & templatedText & referenceSuffix & parenthesisClose
else if fieldCode contains "citet" then
if ibidReference is true then
set templatedText to "ibid."
else
tell application "BibDesk"
set templatedText to (templated text document bibDeskDocumentName using text citetTemplate for multiplePublications)
end tell
end if
set formattedReference to parenthesisOpen & referencePrefix & templatedText & referenceSuffix & parenthesisClose
else if fieldCode contains "citealp" then
if ibidReference is true then
set templatedText to "ibid."
else
tell application "BibDesk"
set templatedText to (templated text document bibDeskDocumentName using text citepTemplate for multiplePublications)
end tell
end if
set formattedReference to referencePrefix & templatedText & referenceSuffix
else if fieldCode contains "citealt" then
if ibidReference is true then
set templatedText to "ibid."
else
tell application "BibDesk"
set templatedText to (templated text document bibDeskDocumentName using text citetTemplate for multiplePublications)
end tell
end if
set formattedReference to referencePrefix & templatedText & referenceSuffix
end if
set content of result range of currentField to formattedReference
else if (numberedReferences as boolean) is true then
-- format numbered references
set formattedReference to ""
set AppleScript's text item delimiters to numberSeperator
set content of result range of currentField to parenthesisOpen & citeKeyNumberText & parenthesisClose
end if
-- set superscript for current field
if superscriptReferences is false then
set superscript of font object of result range of currentField to false
else if superscriptReferences is true then
set superscript of font object of result range of currentField to true
end if
set show codes of currentField to false
-- set italics for current field
set italicCombined to italicLatin & italicReferences
repeat with eachItalic in italicCombined
if formattedReference contains eachItalic then
set AppleScript's text item delimiters to ","
repeat with italicFind in italicCombined
set findObject to find object of selection
set content of findObject to italicFind
set content of replacement of findObject to italicFind
set italic of font object of replacement of findObject to true
execute find findObject replace replace all
end repeat
set AppleScript's text item delimiters to ""
end if
end repeat
end if
end repeat
-- format bibliography
set fieldCount to count of fields of formatRange
repeat until fieldCount is 0
set currentField to field fieldCount of formatRange
set fieldCode to content of field code of currentField
if field type of currentField is field addin and word 2 of fieldCode contains "bibliography" then
set bibliographyField to currentField
select bibliographyField
-- compile the bibliography text and export to a temp file
tell application "System Events"
set tempDirectory to path to temporary items from user domain
set bibliographyTemplateFile to POSIX file bibliographyTemplatePath as string
set bibliographyTemplateFileName to name of file bibliographyTemplatePath
set tempfile to (tempDirectory as string) & bibliographyTemplateFileName
end tell
tell application "BibDesk"
if bibliographySortBy is not "" and (numberedReferences as boolean) is false then
sort publicationList by bibliographySortBy
set publicationList to result
end if
export bibDeskDocument using template file (bibliographyTemplateFile as string) to file tempfile for publicationList
end tell
-- insert exported text into the bibliography field
set show codes of bibliographyField to false
set content of result range of bibliographyField to "*"
insert file at (create range wordDocument start (start of content of result range of bibliographyField) end (start of content of result range of bibliographyField)) file name tempfile
tell application "System Events" to delete file tempfile
-- set Word style of bibliography and force font object
set style of result range of bibliographyField to bibliographyStyle
set name of font object of result range of bibliographyField to name of font object of Word style bibliographyStyle of wordDocument
-- remove superfluous characters
delete (create range wordDocument start ((end of content of result range of bibliographyField) - 2) end (end of content of result range of bibliographyField))
exit repeat
end if
set fieldCount to fieldCount - 1
end repeat
end tell
-- restore Word to its original position
tell application "Microsoft Word"
try
select selectionRange
on error -- produces error if cursor was at the end of the document
select (create range wordDocument start (selection end of selection) end (selection end of selection))
end try
set status bar to "Finished formatting references"
end tell
return bibliographySettings
end formatReferences
---
on unformatReferences()
-- global setup
tell application "Microsoft Word"
set wordDocument to active document
set selectionRange to create range wordDocument start (selection start of selection) end (selection end of selection)
set status bar to "Unformatting references..."
end tell
set AppleScript's text item delimiters to ""
-- unformat references
tell application "Microsoft Word"
activate
if (selection start of selection) is equal to (selection end of selection) then
set formatRange to wordDocument
try
display dialog "Are you sure? This will unformat the entire document!" buttons {"Cancel", "OK"} default button "OK"
on error
return
end try
else
set formatRange to selectionRange
end if
set fieldList to fields of formatRange
if (count of fieldList) is 0 then
display dialog "There are no formatted references in this selection" buttons {"OK"} default button "OK"
return
end if
repeat with currentField in reverse of fieldList
set fieldCode to content of field code of currentField
if field type of currentField is field addin and {"cite", "citep", "citet", "citealp", "citealt", "bibliography"} contains word 2 of fieldCode then
if fieldCode contains "bibliography" then set style of result range of currentField to "Normal"
set fieldCode to characters 8 thru -1 of fieldCode as string
select currentField
insert text "\\" & fieldCode at end of text object of selection
delete currentField
end if
end repeat
end tell
-- restore Word to its original position
tell application "Microsoft Word"
try
select selectionRange
on error -- produces error if cursor was at the end of the document
select (create range wordDocument start (selection end of selection) end (selection end of selection))
end try
set status bar to "Finished unformatting references"
end tell
end unformatReferences
---
on fillEmptyReferences()
-- find empty references contained by {}
set AppleScript's text item delimiters to ""
tell application "Microsoft Word"
set wordDocument to active document
set selectionRange to (create range wordDocument start (selection start of selection) end (selection end of selection))
set status bar to "Filling empty references..."
-- start from the beginning of the document
select (create range wordDocument start 0 end 0)
-- set up the find system in Word
set findObject to find object of selection
set match wildcards of findObject to true
-- prevent wrapping to enable skip option
set wrap of findObject to find stop
-- find empty references and contents but not cite commands
set content of findObject to "[!\\]]\\{*\\}"
set theCounter to 0
repeat while (execute find findObject)
set status bar to "Searching for empty references... (" & theCounter & ")"
set emptyReferenceText to content of (create range wordDocument start (selection start of selection) + 1 end (selection end of selection))
set emptyReferenceContext to "\"" & characters 1 through -2 of (content of sentences of selection as string) & "\""
try
set {citepList, citetList, citealpList, citeSkip} to {"(Author, YEAR)", "(YEAR)", "(Custom...)", "Skip this instance"}
choose from list {citepList, citetList, citealpList, citeSkip} with title "Fill Empty References {}" with prompt "Please select reference[s] in BibDesk for:
" & emptyReferenceContext & "
" default items citepList
set theResult to result
if theResult does not contain citeSkip then
if theResult contains citepList then
tell application "BibDesk" to set templatedText to templated text front document using text "<$publications><$#=1?>\\citep[][]{<$citeKey/><?$#?>,<$citeKey/></$#?></$publications>}" for selection of front document
else if theResult contains citetList then
tell application "BibDesk" to set templatedText to templated text front document using text "<$publications><$#=1?>\\citet[][]{<$citeKey/><?$#?>,<$citeKey/></$#?></$publications>}" for selection of front document
else if theResult contains citealpList then
set emptyReferenceContext to emptyReferenceContext & "
(Use 'citealp' for Author, YEAR, and 'citealt' for YEAR)"
tell application "BibDesk" to set templatedText to templated text front document using text "<$publications><$#=1?>\\citealp[][]{<$citeKey/>}<?$#?>\\citealp[; ][]{<$citeKey/>}</$#?></$publications>" for selection of front document
else if theResult is false then
exit repeat
end if
display dialog "Please confirm the cite command for:
" & emptyReferenceContext with title "Fill Empty References {}" default answer templatedText default button "OK"
set templatedText to text returned of result
set content of (create range wordDocument start (selection start of selection) + 1 end (selection end of selection)) to (templatedText as string)
set theCounter to theCounter + 1
end if
on error
-- prevent "no empty references" dialog
set theCounter to -1
-- exit repeat instead of return to avoid breaking the find system in Word
exit repeat
end try
-- continue search from end of filled reference onwards
select (create range wordDocument start (selection end of selection) end (selection end of selection))
end repeat
try
select selectionRange
end try
end tell
if theCounter is 0 then
tell application "Microsoft Word"
display dialog "No empty references filled" buttons {"OK"} default button "OK"
end tell
else if theCounter is greater than 0 then
try
tell application "Microsoft Word"
display dialog "Empty references filled, format references now?" buttons {"No", "Yes"} cancel button "No" default button "Yes"
if button returned of result contains "Yes" then menuChoiceFormat()
end tell
end try
end if
tell application "Microsoft Word"
set status bar to "Finished filling empty references"
end tell
return
end fillEmptyReferences
---
on toolsMenu()
-- global setup
tell application "Microsoft Word"
set wordDocument to active document
set selectionRange to create range active document start (selection start of selection) end (selection end of selection)
end tell
-- open tools menu
repeat
tell application "Microsoft Word"
activate
choose from list {"Convert from \\cite to \\citep", "Covert from \\citep to \\cite", "Convert p./pp. to :", "Convert : to p./pp.", "Add Leading Space", "Remove Leading Space"} with title "WordScript Tools" with prompt "You should save your work first!" default items "Convert from \\cite to \\citep" cancel button name {"Exit"} OK button name {"Choose"}
set menuChoice to result
end tell
if menuChoice is false then
exit repeat
end if
if menuChoice contains "Export to Static Group" then
-- rewrite from scratch
else if menuChoice contains "Convert from \\cite to \\citep" then
set AppleScript's text item delimiters to ""
tell application "Microsoft Word"
activate
set findObject to find object of text object of wordDocument
set match wildcards of findObject to false
repeat with citeCommand in {"\\cite{", "\\cite["}
set content of findObject to citeCommand
if citeCommand as string is "\\cite{" then