App-specific URLs and the pasteboard. Need help.

  • I'm having problems adding my application's app-specific URL to the
    pasteboard.

    My application, FractalWorks, creates fractal images. It has the
    option to create a URL that encodes all the parameters to recreate the
    image. (The app is available as a free trial if you'd like to try it
    out. Click the above link to download it from dotMac.)

    I use the URL prefix "fractalworks://"

    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];

    My goal is to be able to paste the URL into different target
    applications (Apple's mail client, TextEdit, iChat, etc) and have it
    appear as a clickable link, with the document title as the displayed
    name, e.g. Fractalworks plot Mesh Julia. I'd also like to be able to
    paste the URL onto websites using Safari, FireFox, Sea Monkey, etc.
    Browsers generally want one of a couple of markup formats:

        <u><a href="link">clickable text</a></u>

    or

        [url=link]clickable text[/url]

    I could certainly add options to generate a simple string object in
    either of those formats.

    The clickable link, though, has me stuck. TextEdit is the only app
    that recognizes my URLs and creates a clickable link like want it to.
    Some apps only paste the URL title as plain text, and others paste the
    body of the URL as plain text.

    If I select the clickable link in TextEdit and copy it, I can paste it
    into other apps just fine. I can't figure out what format to use when
    adding my URL to the pasteboard.

    Can somebody help?

    Duncan Champney
  • On Jan 18, 2008, at 10:27 AM, Duncan Champney wrote:

    > I'm having problems adding my application's app-specific URL to the
    > pasteboard.
    >
    [ snip ]

    > My goal is to be able to paste the URL into different target
    > applications (Apple's mail client, TextEdit, iChat, etc) and have
    > it appear as a clickable link,

    --- I don't know about the others, but MAIL has a feature: when you
    are COMPOSING a message, the links you enter are NOT clickable.  This
    is a good thing, since it lets you click on the words and edit them,
    without activating the link itself.  When you look at a SENT or
    RECEIVED message, the link is clickable.

    ----------------------------------------------------------------
    Steve Bird
    Culverson Software - Elegant software that is a pleasure to use.
    www.Culverson.com                    (toll free) 1-877-676-8175
  • 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
  • On 18 Jan 2008, at 17:05, Duncan Champney wrote:

    >> Have you tried NSURLPboardType?
    >
    > Thanks for your reply. I forgot to include the code where i list my
    > types. I declare my types as NSURLPboardType.

    That's not what the code you sent to the list says; it says
    "NSStringPboardType".

    >> Or NSHTMLPboardType with HTML containing a link?
    >
    > That sounds like a real possibility. Do you know what you have to
    > write to the pasteboard for an NSHTMLPboardType? An NSString
    > containing the HTML?

    I *think* it wants an NSData containing HTML, rather than an
    NSString.  i.e. if you had an NSString, you would need to use -
    dataUsingEncoding: to create an NSData using a suitable string
    encoding (which you might consider indicating in your HTML using
    either the <?xml?> tag or a <meta> tag).

    > The documentation on using the pasteboard is a little thin, truth be
    > told.

    I agree that it's a little vague in places.

    The most compatible option would probably be RTF, actually, but HTML
    *should* work I think.

    Kind regards,

    Alastair.

    --
    http://alastairs-place.net
  • On 18 Jan 2008, at 20:17, Duncan Champney wrote:

    > I'm not sure I'm reading the documentation correctly, however.
    > Here's the code I'm trying to use:
    >
    > 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.
    > theRTF_string= [[NSAttributedString alloc] initWithString:
    > theURLName];
    > theStringRange.location = 0;
    > theStringRange.length = [theURLName length];
    > theAttributesDict =
    > [NSDictionary  dictionaryWithObjects:[NSArray
    > arrayWithObjects: theFW_URL, nil]
    > forKeys: [NSArray
    > arrayWithObjects:NSLinkAttributeName, nil]
    > ];
    > NSData* the_RTF_data = [theRTF_string RTFFromRange:
    > theStringRange documentAttributes:theAttributesDict];

    No, that's not right. The attributes need to be set in the attributed
    string. The document attributes are something else entirely (more
    specifically, they contain information about the *document*, not text
    attributes which is what NSLinkAttributeName is). You probably want to
    do -initWithString:attributes: rather than just -initWithString:, then
    pass nil for the documentAttributes: argument.

    Also, consider using -dictionaryWithObjectsAndKeys: or -
    dictionaryWithObject:forKey: rather than -
    dictionaryWithObjects:forKeys:.  They're more efficient and involve
    less typing too :-)

    Kind regards,

    Alastair.

    p.s. Please copy the list on replies; that way others with similar
    questions will be able to find the answers they need.

    --
    http://alastairs-place.net
  • 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
  • Hi Duncan,

    On 2008-01-19, at 01:57, Duncan Champney wrote:

    > The clickable link, though, has me stuck.

    I'd suggest dragging or copying a link from Safari or Mail (something
    with your desired behavior) and firing up Clipboard Viewer to see the
    types it put on the pasteboard. It's included in the developer tools.

    Jonathon Mah
    <me...>
  • Oh wow. That tool will be a huge help. Thanks for pointing it out to me.

    I wish I had known about it when I started implementing this feature.
    It would have saved me a lot of time.

    Duncan C
    ---
    On Jan 20, 2008, at 4:49 AM, Jonathon Mah wrote:

    > Hi Duncan,
    >
    > On 2008-01-19, at 01:57, Duncan Champney wrote:
    >
    >> The clickable link, though, has me stuck.
    >
    >
    > I'd suggest dragging or copying a link from Safari or Mail
    > (something with your desired behavior) and firing up Clipboard
    > Viewer to see the types it put on the pasteboard. It's included in
    > the developer tools.
    >
    >
    >
    > Jonathon Mah
    > <me...>
    >
    >
  • Hmm. Curiouser and curiouser.

    My app creates custom URLs to encode the settings for fractal plots.
    Then, when the user clicks a link, my app launches, parses the
    parameters out of the URL, and recreates the plot.

    Based on Alastair's advice (copied below), my app now creates an
    NSAttributedString and uses it to create RTF data, which I copy to the
    clipboard. In general, it works well.

    However, some of the URLs my program creates are really long. (the URL
    encodes 20 or more parameters for every plot, plus 18 parameters for
    each of an arbitrary number of color gradients).

    The URL shows up as correct in the clipboard when I look at it using
    the Clipboard viewer (thanks to Jonathon Mah for that suggestion!)

    However, when I paste the URL into TextEdit or Mail, it gets truncated
    at slightly less than 1000 characters. (The exact count seems to vary
    by a few bytes, but seems to be around 988 to 992 characters for the
    actual URL text.)

    Is there a limitation on how big an URL can be in an RTF tag? These
    long URLs work if my app copies them to the clipboard and then reads
    them back from the clipboard, so I know they are well formatted.
    Further, if the length of the URL is short enough, the clickable link
    in TextEdit or mail works perfectly. It's only when the length of the
    URL approaches 1000 characters that there's a problem.

    Duncan
    ---
    On Jan 19, 2008, at 7:59 AM, Duncan Champney wrote:

    >
    > 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
    >
    > =
  • On Jan 22, 2008, at 2:44 PM, Duncan Champney wrote:
    > However, when I paste the URL into TextEdit or Mail, it gets
    > truncated at slightly less than 1000 characters. (The exact count
    > seems to vary by a few bytes, but seems to be around 988 to 992
    > characters for the actual URL text.)

    According to RFC 2822 (Internet Message Format):
    There are two limits that this standard places on the number of
    characters in a line. Each line of characters MUST be no more than 998
    characters, and SHOULD be no more than 78 characters, excluding the
    CRLF.

    So since a URL needs to be a single line, it will need to observe
    these limits...  There are a few other limitations on the parts of a
    URLas well - for example, when embedded in a HTML <A> anchor: 1024
    characters in a single attribute value literal, 2100 total sum of all
    lengths of all attribute value specifications and a similar limit of
    2100 for the overall length of the tag.

    Glenn Andreas                      <gandreas...>
      <http://www.gandreas.com/> wicked fun!
    quadrium | prime : build, mutate, evolve, animate : the next
    generation of fractal art
  • A few years back we did a statistical study of millions of actual URLs
    and never found any > 512 characters.
    If you are trying to pass data in the url itself, you may want to
    rethink this policy and consider writing a temp file with the real
    data and passing a smaller url that points to it.
previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today