-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetadatatoFiles
More file actions
120 lines (106 loc) · 3.76 KB
/
Copy pathMetadatatoFiles
File metadata and controls
120 lines (106 loc) · 3.76 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application id "DNtp"
set processedCount to 0
set errorCount to 0
repeat with theRecord in (selected records whose (type is markdown))
try
-- Store the current name for logging
set originalName to name of theRecord
set foundTitle to "" -- Move this up so we can store it for later
-- First clear all existing custom metadata
set custom meta data of theRecord to {}
-- Clear existing tags
set tags of theRecord to {}
set src to plain text of theRecord
set mdMarker to false
set od to AppleScript's text item delimiters
set paraCount to count paragraphs of src
set i to 1
-- Process metadata and tags first
repeat until i > paraCount
set theParagraph to paragraph i of src
if (theParagraph is "---") and (not mdMarker) then
set mdMarker to true
else if (theParagraph is "---") and mdMarker then
exit repeat
else if mdMarker then
if theParagraph contains ":" then
try
-- Switch to current application for string operations
tell current application
set colonOffset to offset of ":" in theParagraph
set theKey to text 1 thru (colonOffset - 1) of theParagraph
if length of theParagraph > colonOffset then
set theValue to text (colonOffset + 1) thru -1 of theParagraph
else
set theValue to ""
end if
end tell
set theKey to my trimWhitespace(theKey)
set theValue to my trimWhitespace(theValue)
if (theKey is "title") then
set foundTitle to theValue
end if
-- Process non-title metadata first
if (theKey is not "title") and (theKey is not "Tags") and (theValue is not "") then
add custom meta data theValue for theKey to theRecord
else if (theKey is "Tags") and (theValue is not "") then
-- Handle tags
set AppleScript's text item delimiters to ","
set rawTags to text items of theValue
set cleanTags to {}
repeat with aTag in rawTags
set cleanTag to my trimWhitespace(aTag)
if cleanTag is not "" then
copy cleanTag to end of cleanTags
end if
end repeat
set tags of theRecord to cleanTags
end if
set AppleScript's text item delimiters to od
on error errMsg
log "Error processing line: " & theParagraph & " - " & errMsg
end try
end if
end if
set i to i + 1
end repeat
-- Add a small delay to let DEVONthink process the metadata changes
delay 0.2
-- Handle filename change last
if (foundTitle is not "") then
try
log "Attempting to rename record: " & originalName & " to: " & foundTitle
set name of theRecord to foundTitle
on error errMsg
log "Error renaming record " & originalName & ": " & errMsg
end try
end if
set processedCount to processedCount + 1
log "Successfully processed file " & processedCount
on error errMsg
set errorCount to errorCount + 1
log "Error on file " & (processedCount + errorCount) & ": " & errMsg
end try
end repeat
log "Processing complete. Successfully processed " & processedCount & " files with " & errorCount & " errors."
end tell
on trimWhitespace(theText)
-- Remove leading/trailing whitespace
repeat while theText begins with " " or theText begins with tab
if theText begins with " " then
set theText to text 2 thru -1 of theText
else
set theText to text 2 thru -1 of theText
end if
end repeat
repeat while theText ends with " " or theText ends with tab
if theText ends with " " then
set theText to text 1 thru -2 of theText
else
set theText to text 1 thru -2 of theText
end if
end repeat
return theText
end trimWhitespace