[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to write a string
From: |
Fred Kiefer |
Subject: |
Re: How to write a string |
Date: |
Sat, 14 Jun 2003 18:36:48 +0200 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021204 |
Hi Christopher,
Christopher Culver wrote:
I have a feeling that this will have a painfully obvious answer, but
after looking through the API I'm still not sure how to do this.
I have a string, NSString *aString. How do I put this string on the
pasteboard? Must I initialise an NSPasteboard and write to it (which
Cocoa docs seem to suggest), or can I just pass a message to NSApp? In
any event, please let me know the code necessary.
the best answer to such a question is allways to check the sources! In
this case it is very simple the object that handles the sending of text
to the pasteboard is of course NSTextView (ok NSText should do this just
the same, but our implementation now made this an empty super class with
all code in NSTextView). This class puts text in the selection
pasteboard as soon as it is selected ([copySelection]) and also into the
general pasteboard ([copy:]). If you have a NSTextView lying around just
let this do the work for you. If not, the method
[writeSelectionToPasteboard:types:] shows the way. You first get a
pasteboard, than declare the types you want to provide and than fill in
the data. In your simple case:
NSPasteboard *pboard = [NSPasteboard generalPasteboard];
NSArray *types = [NSArray arrayWithObject: NSStringPboardType];
[pboard declareTypes: types owner: self];
[pboard setString: string forType: NSStringPboardType];
If you don't have a usable obejct to own the pasteboard data (here
self!) than supply the string itself or nil for it.
Fred