In my learning of NSAttributedString I had a idea to store attributes as JSON. The approach was to have different json files to feed a text… JSON is very simple to edit and change…
I do not have a finish solution yet… but I have other approach that use RTF to store attributes.
The idea is… create rtf file/files with different styles with a least 1 character and save it on disk.
Open TextEdit and activate Make Rich Text…Write your text and select any text you like to be
styled from your library of styled rtf files. Copy the text string command + c
Now run the AS Script below… after you could use command + v to paste the new styled attributes on the text you had copy before.
I find this kind of approach very useful.
PS. This script do not work with Pages… it looks like many things are very strange with Pages. But you could open a RTF in Pages and save its as a pages document with attributes. And use Format → Copy Styles and Paste Styles. In other words you have a separate document that store your attributes.
use framework "Foundation"
use framework "AppKit"
use scripting additions
set thePath to POSIX path of (choose file of type {"public.rtf"})
getAttributedStringFromRTF(thePath)
on getAttributedStringFromRTF(thePath)
set theData to current application's NSData's dataWithContentsOfFile:thePath
set {styledString, attributes} to current application's NSAttributedString's alloc()'s initWithRTF:theData documentAttributes:(reference)
if styledString is missing value then error "RTF data is missing..."
set styledString to styledString's mutableCopy()
set pasteboard to current application's NSPasteboard's generalPasteboard()
set thePlainText to (pasteboard's stringForType:"public.utf8-plain-text") as text
styledString's replaceCharactersInRange:{0, styledString's |length|()} withString:thePlainText
my setStyledStringToPasteboard(styledString, attributes)
end getAttributedStringFromRTF
on setStyledStringToPasteboard(styleString, attributes)
set theData to styleString's RTFFromRange:{0, styleString's |length|()} documentAttributes:attributes
set pasteboard to current application's NSPasteboard's generalPasteboard()
pasteboard's clearContents()
pasteboard's setData:theData forType:"public.rtf"
end setStyledStringToPasteboard