Convert Plain Text to Rich Text

Summary: Converts the selected plain text files to RTF.
Requires: EagleFiler
Install Location: ~/Library/Scripts/Applications/EagleFiler/
Last Modified: 2016-01-18

Description

This script converts the selected UTF-8 text files into RTF format so that you can edit the fonts and styles.

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

Script

on run
    
tell application "EagleFiler"
        
set _selection to selected records of browser window 1
        
repeat with _record in _selection
            
if _record's universal type identifier is "public.plain-text" then
                
my convertRecordToRichText(_record)
            
end if
        
end repeat
    
end tell
end run

on convertRecordToRichText(_record)
    
tell application "EagleFiler"
        
set _newName to my richTextNameForRecord(_record)
        
set _newFile to my convertFileToRichText(_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 convertRecordToRichText

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

on convertFileToRichText(_source, _destName)
    
tell application "EagleFiler"
        
set _tempFolder to POSIX path of (create temporary folder name "EFPlainTextToRichText")
    
end tell
    
set _dest to _tempFolder & "/" & _destName
    
set _convert to "textutil -convert rtf"
    
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 convertFileToRichText

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