-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeadingPages
More file actions
104 lines (84 loc) · 3.84 KB
/
Copy pathHeadingPages
File metadata and controls
104 lines (84 loc) · 3.84 KB
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
use AppleScript version "2.4"
use scripting additions
tell application id "DNtp"
try
-- Get the current database and selected records
set theDB to current database
if theDB is missing value then error "Please open a database first."
set selectedRecords to selected records
if (count of selectedRecords) is 0 then error "Please select some documents to process."
-- Get or create the headings group
set headingsGroup to get record at "/Heading Pages" in theDB
if headingsGroup is missing value then
-- Create the Heading Pages group
set headingsGroup to create record with {name:"Heading Pages", type:group} in theDB
set comment of headingsGroup to "This group contains automatically generated pages for all wikilinks found in your documents. Each page tracks which documents reference that particular heading."
display alert "Created Heading Pages Group" message "Created a new group to store wikilink pages."
end if
-- Show progress
show progress indicator "Processing Records" steps (count of selectedRecords)
-- Process each selected record
repeat with theRecord in selectedRecords
if type of theRecord is in {markdown, txt, rtf, rtfd} then
step progress indicator ("Processing " & (name of theRecord as string))
-- Get content and look for wikilinks
set theContent to plain text of theRecord
-- Save original delimiters
set savedDelimiters to AppleScript's text item delimiters
-- Find all wikilinks in the content
set wikilinks to {}
set currentPos to 1
set contentLength to length of theContent
repeat while currentPos ≤ contentLength
-- Find next "[[" marker
set startPos to offset of "[[" in (texts currentPos thru contentLength of theContent)
if startPos is 0 then exit repeat
-- Adjust position to actual location
set startPos to currentPos + startPos + 1
-- Find closing "]]"
set remainingText to texts startPos thru contentLength of theContent
set endPos to offset of "]]" in remainingText
if endPos is 0 then exit repeat
-- Extract the wikilink text
set linkText to texts 1 thru (endPos - 1) of remainingText
-- Add to wikilinks if not empty and not already found
if linkText is not "" and linkText is not in wikilinks then
set end of wikilinks to linkText
end if
-- Move past this wikilink
set currentPos to startPos + endPos + 1
end repeat
-- Process found wikilinks
repeat with linkText in wikilinks
-- Create or get wikilink page
set wikiPage to get record at ("/Heading Pages/" & linkText) in theDB
if wikiPage is missing value then
set wikiPage to create record with {name:linkText, type:markdown} in headingsGroup
set plain text of wikiPage to "# " & linkText & return & return & "## Documents referencing this heading" & return
end if
-- Add reference only if it doesn't exist
set docURL to reference URL of theRecord
set docLink to "- [" & (name of theRecord as string) & "](" & docURL & ")"
set pageContent to plain text of wikiPage
if pageContent does not contain docLink then
if last character of pageContent is not return then
set pageContent to pageContent & return
end if
set plain text of wikiPage to pageContent & docLink & return
end if
end repeat
-- Restore delimiters
set AppleScript's text item delimiters to savedDelimiters
end if
end repeat
hide progress indicator
display alert "Complete" message "Finished processing selected documents."
on error error_message number error_number
-- Restore delimiters in case of error
set AppleScript's text item delimiters to savedDelimiters
hide progress indicator
if error_number is not -128 then
display alert "Error" message error_message
end if
end try
end tell