The version above is old!
This script converts RTF and RTFD to MultiMarkdown.
In case of RTFDs only Images are preserved.
It needs Pandoc and RegexAndStuffLib (see above).
-- Convert RTF to MultiMarkdown (via textutil and pandoc)
-- This script needs Pandoc (https://pandoc.org/installing.html) and RegexAndStuffLib (https://latenightsw.com/support/freeware/).
-- This version converts RTF and RTFD - but only images are preserved, other attachments are not supported!
use scripting additions
use script "RegexAndStuffLib" version "1.0.6"
property moveMarkdownRecord : false -- set to true if you want markdown and image records in one group
property removeEmptyLines : false
tell application id "DNtp"
try
set windowClass to class of window 1
if {viewer window, search window} contains windowClass then
set currentRecord_s to selection of window 1
else if windowClass = document window then
set currentRecord_s to content record of window 1 as list
end if
set theDestinationGroup to display group selector
set tempPath to POSIX path of (path to temporary items folder)
show progress indicator "Converting... " steps (count of currentRecord_s) with cancel button
repeat with thisRecord in currentRecord_s
if (type of thisRecord) is in {rtf, rtfd} then
set theName to my recordName(name of thisRecord, filename of thisRecord)
step progress indicator theName
set tempName to do shell script "date \"+%Y%m%d%H%M%S\""
if (type of thisRecord) = rtf then
try
set thePath to path of thisRecord
set theOutputPath to (tempPath & tempName & ".md") as string
set convertToMultiMarkdown to do shell script "textutil " & quoted form of thePath & " -convert html -stdout | /usr/local/bin/pandoc -f html-native_divs-native_spans -t markdown_mmd --wrap=preserve -o " & quoted form of theOutputPath
set newRecord to indicate theOutputPath to theDestinationGroup
tell application "Finder" to delete file (POSIX file theOutputPath as alias)
on error
set label of thisRecord to 1
end try
else
try
set theSource to source of thisRecord
set theSourcePath to (tempPath & tempName & ".html") as string
set theOutputPath to (tempPath & tempName & ".md") as string
set theExtractionPath to (tempPath & tempName) as string
set createExtractionFolder to do shell script "mkdir -p " & quoted form of theExtractionPath
set sourceFile to open for access theSourcePath with write permission
write theSource as «class utf8» to sourceFile
close access sourceFile
set convertToMultiMarkdown to do shell script "/usr/local/bin/pandoc -f html-native_divs-native_spans -t markdown_mmd --wrap=preserve --extract-media=" & quoted form of theExtractionPath & " -o " & quoted form of theOutputPath & " " & quoted form of theSourcePath
set newRecord to indicate theOutputPath to theDestinationGroup
set theGroup to indicate theExtractionPath to theDestinationGroup
tell application "Finder"
delete folder (POSIX file theExtractionPath as alias)
delete file (POSIX file theSourcePath as alias)
delete file (POSIX file theOutputPath as alias)
end tell
set name of theGroup to (theName & ".md") as string
if moveMarkdownRecord = true then move record newRecord to theGroup
set theText to plain text of newRecord
set theParagraphs to paragraphs of theText
set theText_List to {}
repeat with thisParagraph in theParagraphs
set thisParagraph to thisParagraph as string
if thisParagraph contains theExtractionPath then
set theFilename to item 1 of (regex search thisParagraph search pattern "(?<=/)[a-z|0-9]{40}\\.(.*?)(?=\\))" as string)
repeat with thisChild in (children of theGroup)
if (filename of thisChild) = theFilename then
set replaceLink to regex change thisParagraph search pattern "(?<=!?\\[\\]\\()(.*?)" & theFilename & "(?=\\))" replace template (reference URL of thisChild)
set end of theText_List to replaceLink
exit repeat
end if
end repeat
else
set end of theText_List to thisParagraph
end if
end repeat
set plain text of newRecord to my string_From_List(theText_List, linefeed)
on error
set label of thisRecord to 1
end try
end if
tell newRecord
set name to (theName & ".md") as string
set URL to (URL of thisRecord)
set creation date to (creation date of thisRecord)
set addition date to (addition date of thisRecord)
set modification date to (modification date of thisRecord)
set comment to (comment of thisRecord)
set theText to plain text
set firstLine to paragraph 1 in theText
if firstLine contains ":" then
set escapedFirstLine to regex change firstLine search pattern ("(?<!\\\\):(?!//)") replace template ("\\\\:")
set escapedText_List to ((escapedFirstLine as list) & paragraphs 2 thru -1 in theText) as list
set escapedText to my string_From_List(escapedText_List, linefeed)
set plain text to escapedText
set theText to plain text
end if
if removeEmptyLines = true then
set cleanText_1 to regex change theText search pattern ("\\n\\n") replace template (space & space & linefeed)
set cleanText_2 to regex change cleanText_1 search pattern ("^ +$") replace template ("")
set plain text to cleanText_2
end if
end tell
end if
end repeat
hide progress indicator
open window for record theDestinationGroup
activate
on error error_message number error_number
hide progress indicator
tell application "Finder"
try
delete folder (POSIX file theExtractionPath as alias)
delete file (POSIX file theSourcePath as alias)
delete file (POSIX file theOutputPath as alias)
end try
end tell
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
return
end try
end tell
on recordName(theName, theFilename)
set revName to reverse of (characters of theName) as string
set suffixName to reverse of (characters 1 thru ((character offset of "." in revName) - 1) in revName) as string
set revFileName to reverse of (characters of theFilename) as string
set suffixFileName to reverse of (characters 1 thru ((character offset of "." in revFileName) - 1) in revFileName) as string
if suffixName = suffixFileName then set theName to reverse of (characters ((character offset of "." in revName) + 1) thru -1 in revName) as string
return theName
end recordName
on string_From_List(theList, theDelimiter)
set theString to ""
set theCount to 0
repeat with thisItem in theList
set theCount to theCount + 1
set thisItem to thisItem as string
if theCount ≠ (count of theList) then
set theString to theString & thisItem & theDelimiter
else
set theString to theString & thisItem
end if
end repeat
return theString
end string_From_List