As I said, my knowledge of scripting isn’t the greatest. Without GPT, I couldn’t do it. That is why I am here asking for advice. That said, here are some scripts I have tried.
on sortRecordsNumerically(recordList)
tell application id “DNtp”
return (sort recordList by name)
end tell
end sortRecordsNumerically
on performSmartRule(theRecords)
tell application id “DNtp”
try
– Initialize the TOC content
set tocContent to “# Johnny Decimal Index\n\n”
-- Process top-level records
repeat with thisRecord in theRecords
set tocContent to tocContent & my processRecord(thisRecord, 0) -- Start at depth 0
end repeat
-- Define the TOC name and destination group
set tocName to "Johnny Decimal Index.md"
set theGroup to current group
set tocRecord to missing value
-- Check if a TOC file already exists
try
set tocRecord to (child named tocName of theGroup)
end try
-- Create or update the TOC
if tocRecord is missing value then
create record with {name:tocName, type:markdown, content:tocContent} in theGroup
log message "TOC created successfully."
else
set plain text of tocRecord to tocContent
log message "TOC updated successfully."
end if
on error errMsg number errNum
-- Log errors for debugging
log message "Error " & errNum & ": " & errMsg
end try
end tell
end performSmartRule
– Recursive function to process records and preserve hierarchy
on processRecord(thisRecord, depth)
tell application id “DNtp”
set indent to (my repeatString(" ", depth)) – Indentation for markdown hierarchy
set recordContent to “”
-- Add record to TOC
set theName to name of thisRecord
set theURL to reference URL of thisRecord
set recordContent to recordContent & indent & "- [" & theName & "](" & theURL & ")\n"
-- Process children if it’s a group
if type of thisRecord is group then
set childrenRecords to my sortRecordsNumerically(children of thisRecord)
repeat with childRecord in childrenRecords
set recordContent to recordContent & my processRecord(childRecord, depth + 1)
end repeat
end if
return recordContent
end tell
end processRecord
– Simplified sorting function for Johnny Decimal order
on sortRecordsNumerically(recordList)
set sortedList to {}
repeat with aRecord in recordList
set end of sortedList to {item:aRecord, key:(my extractNumber(name of aRecord))}
end repeat
set sortedList to my sortList(sortedList)
return (item of sortedList) – Return only the sorted records
end sortRecordsNumerically
– Extract the numeric prefix for sorting
on extractNumber(recordName)
set AppleScript’s text item delimiters to “.”
try
return (text item 1 of recordName) as integer
on error
return 999 – Default value if no numeric prefix exists
end try
end extractNumber
– Sort helper for alphanumeric lists
on sortList(aList)
set AppleScript’s text item delimiters to {“”}
set sortedList to (sort aList by key)
set AppleScript’s text item delimiters to {}
return sortedList
end sortList
– Utility function to repeat strings
on repeatString(theString, times)
set resultString to “”
repeat times times
set resultString to resultString & theString
end repeat
return resultString
end repeatString
on performSmartRule(theRecords)
tell application id “DNtp”
try
– Initialize the TOC content
set tocContent to “# Johnny Decimal Index\n\n”
-- Process top-level records passed by the Smart Rule
repeat with thisRecord in theRecords
set tocContent to tocContent & my processRecord(thisRecord, 0) -- Start at depth 0
end repeat
-- Define the TOC name and destination group
set tocName to "Johnny Decimal Index.md"
set theGroup to current group
set tocRecord to missing value
-- Check if a TOC file already exists
try
set tocRecord to (child named tocName of theGroup)
end try
-- Create or update the TOC
if tocRecord is missing value then
create record with {name:tocName, type:markdown, content:tocContent} in theGroup
log message "TOC created successfully."
else
set plain text of tocRecord to tocContent
log message "TOC updated successfully."
end if
on error errMsg number errNum
-- Log errors for debugging
log message "Error " & errNum & ": " & errMsg
end try
end tell
end performSmartRule
– Recursive function to process records and preserve hierarchy
on processRecord(thisRecord, depth)
tell application id “DNtp”
set indent to (my repeatString(" ", depth)) – Indentation for markdown hierarchy
set recordContent to “”
-- Add record to TOC
set theName to name of thisRecord
set theURL to reference URL of thisRecord
set recordContent to recordContent & indent & "- [" & theName & "](" & theURL & ")\n"
-- Process children if it’s a group
if type of thisRecord is group then
set childrenRecords to my sortRecordsNumerically(children of thisRecord)
repeat with childRecord in childrenRecords
set recordContent to recordContent & my processRecord(childRecord, depth + 1)
end repeat
end if
return recordContent
end tell
end processRecord
– Helper function: Sort records numerically by name
on sortRecordsNumerically(recordList)
tell application id “DNtp”
set sortedList to {}
repeat with aRecord in recordList
set end of sortedList to {item:aRecord, key:(name of aRecord as text)}
end repeat
set sortedList to my sortListNumerically(sortedList)
return (item of sortedList)
end tell
end sortRecordsNumerically
– Generic sorting function for Johnny Decimal order
on sortListNumerically(aList)
set AppleScript’s text item delimiters to {“”}
set sortedList to (sort aList by key)
set AppleScript’s text item delimiters to {}
return sortedList
end sortListNumerically
– Utility function to repeat strings
on repeatString(theString, times)
set resultString to “”
repeat times times
set resultString to resultString & theString
end repeat
return resultString
end repeatString
There is cca 30 more tries where that came from, if you’d like I can send them all to you.