MacOS – Inserting blank lines into RTF file with AppleScript

applescriptmacostext;

Can someone tell me what is the best way that I can automatically insert a blank line at the top and bottom of these newly-created .rtf files, for these two separate pieces of code?

First piece of code (for when the clipboard contains rich text):

try
    set richTextfromClipboard to get the clipboard as «class RTF »
on error eStr number eNum
    display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with icon caution
    return
end try

try
    set fileHandle to open for access theRichTextFilepath with write permission
    write richTextfromClipboard to fileHandle
    close access fileHandle
on error eStr number eNum
    display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with title "File I/O Error..." with icon caution
    try
        close access fileHandle
    end try
end try

Second piece of code (for when the clipboard contains HTML):

try
    do shell script "osascript -e 'try' -e 'get the clipboard as «class HTML»' -e 'end try' | awk '{sub(/«data HTML/, \"3C68746D6C3E\") sub(/»/, \"3C2F68746D6C3E\")} {print}' | xxd -r -p | textutil -convert rtf -stdin -stdout > " & quoted form of theRichTextFilepath
on error eStr number eNum
    display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with icon caution
end try

The blank lines do not have to be of a predetermined font type or font size; the blank lines should simply match the style of the surrounding .rtf file (that is, the style of the rich text on the clipboard).

Best Answer

You can use this Cocoa-AppleScript:

-- Cocoa-AppleScript-- you need these lines in your script
use framework "Foundation"
use scripting additions

on insertTextInRTFFile(f) -- the parameter must be the posix path of an existing file, example  --> "/Users/someName/Desktop/xyz.rtf"
    set myText to linefeed -- the text to insert in the rtf (a blank line as example)
    tell current application
        set fileURL to its (NSURL's fileURLWithPath:f)
        set myOption to its (NSDictionary's alloc()'s initWithObjectsAndKeys_(its NSRTFTextDocumentType, its NSDocumentTypeDocumentOption, missing value))
        set {attrString, docAttrib, err} to its ((NSMutableAttributedString's alloc())'s initWithURL:fileURL options:myOption documentAttributes:(reference) |error|:(reference)) -- get the attributed string from the RTF file
        if attrString is missing value then error err's localizedDescription() as text -- not a RTF file or not a valid path

        -- it use the string in the 'myText' variable to create an attributed string 
        set firstline to its ((NSAttributedString's alloc())'s initWithString:myText attributes:(attrString's attributesAtIndex:0 effectiveRange:(missing value))) --  create an attributed string with the style of the first character
        set lastline to its ((NSAttributedString's alloc())'s initWithString:myText attributes:(attrString's attributesAtIndex:((attrString's |length|()) - 1) effectiveRange:(missing value))) --  create an attributed string with the style of the last character
    end tell
    attrString's insertAttributedString:firstline atIndex:0 -- insert an attributed string (blank line) at the top
    attrString's appendAttributedString:lastline -- insert an attributed string (blank line) at bottom
    set rtfData to attrString's RTFFromRange:{0, attrString's |length|()} documentAttributes:docAttrib -- create the data from an attributed string
    rtfData's writeToFile:f atomically:true -- write the data to the RTF file
end insertTextInRTFFile

After your script will have written to the RTF file: just call the insertTextInRTFFile() handler with the posix path of the RTF file as parameter.