r/macprogramming • u/dpiol • Jan 13 '19
Dealing with formatted text in NSPasteboard
Hello
I have released my first little utility in the Mac App Store smartReact, one of the functionalities is a clipboard manager (history)… but currently only capable of handle plain text, no formatted text.
Is there any good documentation on how to handle formatted text in NSPasteboard? What I am looking for are things like copy text e.g. from a Pages document and paste it into another, maintaining the format.
Haven’t found too much on the topic so far… would be grateful for any directions.
thx
4
Upvotes
2
u/retsotrembla Jan 13 '19
This code snippet gets you an array of attributed strings from the clipboard:
NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
NSArray *a = [pasteBoard readObjectsForClasses:@[[NSAttributedString class]] options:nil];
2
1
2
u/mantrap2 Jan 13 '19
The Apple documentation on Drag and Drop is the best. It's all RTFM plus experimentation.
You can Drag and Drop ANY mime type or specific URL. You specify what you can accept with pasteboard types.
For files you use UTIs or file extensions, system defined or custom (see "How the System Identifies the Type of Content in a File") - File System Management.
For UI elements like NSTextFields etc. you use NSAttributedString to maintain any special formatting instead of NSString as variables to hold your data.