FROM : Duncan Champney
DATE : Sat Jan 19 13:59:08 2008
On Jan 18, 2008, at 11:05 AM, Alastair Houghton wrote:
> On 18 Jan 2008, at 15:27, Duncan Champney wrote:
>
>> I build my URL string in the variable theURLString, then add it to
>> the general pasteboard using the following code:
>>
>> theFW_URL = [NSURL URLWithString:theURLString];
>> [theFW_URL writeToPasteboard: thePasteBoard];
>> NSString* theURLName = [NSString stringWithFormat:
>> NSLocalizedString(@FW_URL_PREFIX ,nil), theDocumentName];
>> success = [thePasteBoard setString:theURLName forType:
>> NSStringPboardType];
>
> Have you tried NSURLPboardType?
>
> Or NSHTMLPboardType with HTML containing a link?
>
> Or, for that matter, NSRTFPboardType, which I think you'll find can
> hold links too; you can make RTF fairly easily from an
> NSAttributedString, and if that string happened to contain a link
> (hint: see NSLinkAttributeName), I think you'll get the behaviour
> you want.
>
> At present, I think you're relying on the end application to parse
> your text into a link, which won't work in many cases.
>
> Kind regards,
>
> Alastair.
>
> --
> http://alastairs-place.net
>
>
Alastair,
I was using NSURLPboardType, but I don't think I was using it correctly.
Based on your advice, I tried using NSHTMLPboardType. That worked
better, but still not ideally.
As you say, NSRTFPboardType seems to work with the widest variety of
applications. What I do now is to write RTF to the pasteboard, and
also write some HTML as a string. The HTML string is useful for
posting links to websites. Here's the code I came up with:
NSArray* types = [NSArray arrayWithObjects: NSRTFPboardType,
NSStringPboardType, nil];
[thePasteBoard declareTypes:types owner: nil];
theFW_URL = [NSURL URLWithString:theURLString]; //theURLString
contains the text of the URL
[theFW_URL writeToPasteboard: thePasteBoard];
//Create string "Fractalworks plot <theDocumentName>
NSString* theURLName = [NSString stringWithFormat:
NSLocalizedString(@FW_URL_PREFIX ,nil), theDocumentName];
theFW_URL = [NSURL URLWithString:theURLString];
//Create an NSAttributedString to hold the RTF description of the URL.
theStringRange.location = 0;
theStringRange.length = [theURLName length];
theAttributesDict = [NSDictionary dictionaryWithObject: theFW_URL
forKey: NSLinkAttributeName];
theRTF_string= [[NSAttributedString alloc] initWithString:
theURLName attributes:theAttributesDict];
NSData* the_RTF_data = [theRTF_string RTFFromRange: theStringRange
documentAttributes: nil];
success = [thePasteBoard setData:the_RTF_data forType:
NSRTFPboardType];
//Also write a string with HTML text into the pasterboard, for apps
that can't deal with the RTF data.
theURLString = [NSString stringWithFormat: @"<u><a href=\"%@\">%@</
a></u>", theURLString, theURLName];
success = [thePasteBoard setString:theURLString forType:
NSStringPboardType];
Older Carbon apps like Appleworks don't seem to work with this code,
though. They take the "vanilla" text from the RTF, rather than using
the NSStringPboardType like you'd expect.
I have a preference to "write application URLS as text" and in that
case, I write just the NSStringPboardType data containing the URL to
the pasteboard. (That code isn't shown for the sake of brevity.)
Thanks again for your help.
Duncan
DATE : Sat Jan 19 13:59:08 2008
On Jan 18, 2008, at 11:05 AM, Alastair Houghton wrote:
> On 18 Jan 2008, at 15:27, Duncan Champney wrote:
>
>> I build my URL string in the variable theURLString, then add it to
>> the general pasteboard using the following code:
>>
>> theFW_URL = [NSURL URLWithString:theURLString];
>> [theFW_URL writeToPasteboard: thePasteBoard];
>> NSString* theURLName = [NSString stringWithFormat:
>> NSLocalizedString(@FW_URL_PREFIX ,nil), theDocumentName];
>> success = [thePasteBoard setString:theURLName forType:
>> NSStringPboardType];
>
> Have you tried NSURLPboardType?
>
> Or NSHTMLPboardType with HTML containing a link?
>
> Or, for that matter, NSRTFPboardType, which I think you'll find can
> hold links too; you can make RTF fairly easily from an
> NSAttributedString, and if that string happened to contain a link
> (hint: see NSLinkAttributeName), I think you'll get the behaviour
> you want.
>
> At present, I think you're relying on the end application to parse
> your text into a link, which won't work in many cases.
>
> Kind regards,
>
> Alastair.
>
> --
> http://alastairs-place.net
>
>
Alastair,
I was using NSURLPboardType, but I don't think I was using it correctly.
Based on your advice, I tried using NSHTMLPboardType. That worked
better, but still not ideally.
As you say, NSRTFPboardType seems to work with the widest variety of
applications. What I do now is to write RTF to the pasteboard, and
also write some HTML as a string. The HTML string is useful for
posting links to websites. Here's the code I came up with:
NSArray* types = [NSArray arrayWithObjects: NSRTFPboardType,
NSStringPboardType, nil];
[thePasteBoard declareTypes:types owner: nil];
theFW_URL = [NSURL URLWithString:theURLString]; //theURLString
contains the text of the URL
[theFW_URL writeToPasteboard: thePasteBoard];
//Create string "Fractalworks plot <theDocumentName>
NSString* theURLName = [NSString stringWithFormat:
NSLocalizedString(@FW_URL_PREFIX ,nil), theDocumentName];
theFW_URL = [NSURL URLWithString:theURLString];
//Create an NSAttributedString to hold the RTF description of the URL.
theStringRange.location = 0;
theStringRange.length = [theURLName length];
theAttributesDict = [NSDictionary dictionaryWithObject: theFW_URL
forKey: NSLinkAttributeName];
theRTF_string= [[NSAttributedString alloc] initWithString:
theURLName attributes:theAttributesDict];
NSData* the_RTF_data = [theRTF_string RTFFromRange: theStringRange
documentAttributes: nil];
success = [thePasteBoard setData:the_RTF_data forType:
NSRTFPboardType];
//Also write a string with HTML text into the pasterboard, for apps
that can't deal with the RTF data.
theURLString = [NSString stringWithFormat: @"<u><a href=\"%@\">%@</
a></u>", theURLString, theURLName];
success = [thePasteBoard setString:theURLString forType:
NSStringPboardType];
Older Carbon apps like Appleworks don't seem to work with this code,
though. They take the "vanilla" text from the RTF, rather than using
the NSStringPboardType like you'd expect.
I have a preference to "write application URLS as text" and in that
case, I write just the NSStringPboardType data containing the URL to
the pasteboard. (That code isn't shown for the sake of brevity.)
Thanks again for your help.
Duncan
| Related mails | Author | Date |
|---|---|---|
| Duncan Champney | Jan 18, 16:27 | |
| Steve Bird | Jan 18, 16:51 | |
| Alastair Houghton | Jan 18, 17:05 | |
| Alastair Houghton | Jan 18, 18:21 | |
| Alastair Houghton | Jan 19, 00:30 | |
| Duncan Champney | Jan 19, 13:59 | |
| Jonathon Mah | Jan 20, 10:49 | |
| Duncan Champney | Jan 20, 15:29 | |
| Duncan Champney | Jan 22, 21:44 | |
| glenn andreas | Jan 22, 23:46 | |
| William Bates | Jan 23, 13:22 |






Cocoa mail archive

