Convert Rich Text to Plain Text

Summary: Converts the selected RTF or RTFD files to plain text files.
Requires: EagleFiler
Install Location: ~/Library/Scripts/Applications/EagleFiler/
Last Modified: 2019-10-02


This script converts the selected “.rtf” or “.rtfd” files into plain text (“.txt”) files in the UTF-8 encoding.

Installation Instructions · Download in Compiled Format · Download in Text Format


on run
tell application "EagleFiler"
set _selection to selected records of browser window 1
repeat with _record in _selection
set _uti to _record's universal type identifier
if _uti is in {"public.rtf", ""} then
my convertRecordToPlainText(_record)
end if
end repeat
end tell
end run

on convertRecordToPlainText(_record)
tell application "EagleFiler"
set _newName to my plainTextNameForRecord(_record)
set _newFile to my convertFileToPlainText(_record's file, _newName)
set _newRecords to import files {_newFile} with deleting afterwards
set _newRecord to item 1 of _newRecords
my copyMetadata(_record, _newRecord)
set _trash to trash of _record's library document
set _record's container to _trash
end tell
end convertRecordToPlainText

on plainTextNameForRecord(_record)
tell application "EagleFiler"
set _name to _record's filename
if _name ends with ".rtf" then
set _end to (length of ".rtf") + 1
set _base to characters 1 thru -_end of _name
else if _name ends with ".rtfd" then
set _end to (length of ".rtfd") + 1
set _base to characters 1 thru -_end of _name
set _base to _name
end if
return _base & ".txt"
end tell
end plainTextNameForRecord

on convertFileToPlainText(_source, _destName)
tell application "EagleFiler"
set _tempFolder to POSIX path of (create temporary folder name "EFRichTextToPlainText")
end tell
set _dest to _tempFolder & "/" & _destName
set _convert to "textutil -convert txt"
set _outputArg to " -output " & _dest's quoted form
set _sourceArg to " " & quoted form of _source's POSIX path
do shell script _convert & _outputArg & _sourceArg
return POSIX file _dest
end convertFileToPlainText

on copyMetadata(_source, _dest)
tell application "EagleFiler"
set _sourceURL to _source's source URL
set source URL of _dest to _sourceURL
set container of _dest to _source's container
set _noteText to _source's note text
set note text of _dest to _noteText
set _tags to _source's assigned tags
set assigned tags of _dest to _tags
set _title to _source's title
set title of _dest to _title
set _fromName to _source's from name
set from name of _dest to _fromName
set _labelIndex to _source's label index
set label index of _dest to _labelIndex
set _creationDate to _source's creation date
set creation date of _dest to _creationDate
set _modificationDate to _source's modification date
set modification date of _dest to _modificationDate
end tell
end copyMetadata