I like to read documents on screen, still the damn white backgrounds bother and tires me a lot. Isn’t it possible to change the background of rtf / rtfd documents to some more friendly color tone, maybe to gray? Just to read the document, OK but mostly I don’t need to print it to need white as background color…
In the past I “wrote” rtf documents with our simple "write " standard additions-command but I think it’s no good idea to rewrite downloaded documents as we might lose the embedded picture links (rtfd) and other formatting which our simple standard additions -write doesn’t support.
So is it possible to change in batch the background colour of rtf/rtfd documents without to lose any formatting or embedded link?
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
set posixPath to POSIX path of (choose file with prompt "Choose an RTF or RTFD file" of type {"rtf", "rtfd"})
-- read file
set theData to current application's NSData's dataWithContentsOfFile:posixPath
-- create attributed string from the data
if posixPath ends with ".rtf" then
set {styledString, docAttributes} to current application's NSAttributedString's alloc()'s initWithRTF:theData documentAttributes:(reference)
else
set {styledString, docAttributes} to current application's NSAttributedString's alloc()'s initWithRTFD:theData documentAttributes:(reference)
end if
if styledString is missing value then error "Could not read RTF/RTFD file"
-- make copy you can modify
set styledString to styledString's mutableCopy()
-- make color
set theColor to current application's NSColor's lightGrayColor()
-- apply background attribute to all text
styledString's addAttribute:(current application's NSBackgroundColorAttributeName) value:theColor range:{0, styledString's |length|()}
-- turn attributed string back into RTF data
if posixPath ends with ".rtf" then
set theData to styledString's RTFFromRange:{0, styledString's |length|()} documentAttributes:docAttributes
else
set theData to styledString's RTFDFromRange:{0, styledString's |length|()} documentAttributes:docAttributes
end if
-- build path for new file
set posixPath to current application's NSString's stringWithString:posixPath
set newPath to (posixPath's stringByDeletingPathExtension()'s stringByAppendingString:"-copy")'s stringByAppendingPathExtension:(posixPath's pathExtension())
-- write the data to the file
theData's writeToFile:newPath atomically:true