Creating NSURL from Apple Event string

  • I have this really annoying issue that keeps coming up over and over
    and I'm at my wits end.  Hopefully I can get some help.

    I have a Cocoa application that receives a URL in a string in an
    Apple Event and opens a web view of the page.  I have two different
    applications that are sending this Apple Event, one is a Carbon C++
    program, the other a Carbon Pascal app (written in FPC, compiled in
    xCode.)

    The C++ program seems to send the string fine, and the Cocoa app
    opens the page fine.

    The Pascal program, on the other hand, just seems to send over bad
    strings (which I've converted from Pascal Str255 to zero-terminated C
    strings).  They SEEM to be valid, but when I run this:

    NSURL *myURL = [NSURL URLWithString: urlString];

    I get a nil myURL.  Documentation says it will be nil if "urlString"
    is invalid.  In the debugger, it sure LOOKS valid, and if I replace
    that line with:

    NSURL *myURL = [NSURL URLWithString: @"http://www.apple.com"];

    it works fine.

    How do I figure out what's wrong with "http://www.apple.com" in a
    NSString, that isn't wrong when I use @"http://www.apple.com" directly?

    Thanks!

    dale

    --
    <adjensen...>  http://kandsmil.blogspot.com

    Only a fool turns away from an angel,
    Vision in white steps through the door,
    Holds out her hand, I say 'No I'm not ready'
    I open my eyes and let the dream fade away.
      -- echolyn, "on any given nite"
  • On 10/10/06, a.d. jensen <adjensen...> wrote:
    > I have this really annoying issue that keeps coming up over and over
    > and I'm at my wits end.  Hopefully I can get some help.
    >
    > I have a Cocoa application that receives a URL in a string in an
    > Apple Event and opens a web view of the page.  I have two different
    > applications that are sending this Apple Event, one is a Carbon C++
    > program, the other a Carbon Pascal app (written in FPC, compiled in
    > xCode.)
    >
    > The C++ program seems to send the string fine, and the Cocoa app
    > opens the page fine.
    >
    > The Pascal program, on the other hand, just seems to send over bad
    > strings (which I've converted from Pascal Str255 to zero-terminated C
    > strings).  They SEEM to be valid, but when I run this:
    >
    > NSURL *myURL = [NSURL URLWithString: urlString];
    >
    > I get a nil myURL.  Documentation says it will be nil if "urlString"
    > is invalid.  In the debugger, it sure LOOKS valid, and if I replace
    > that line with:

    Can you outline how you construct the NSString you feed into
    URLWithString:? Also how you convert from a Str255 to a C string?

    It sure sounds like the problem exists in that area of things.

    -Shawn
  • On Oct 10, 2006, at 9:08 PM, a.d. jensen wrote:

    > How do I figure out what's wrong with "http://www.apple.com" in a
    > NSString, that isn't wrong when I use @"http://www.apple.com"
    > directly?

    "" is a C-string, while @"<A href="http://www.apple.com">http://www.apple.com"
    is a NSString (actually private subclass for constant strings).

    To create a string from C-string, use stringWithCString:encoding:.
    Since URL must be ASCII, you can use here NSASCIIStringEncoding.

    Best Regards,

    Nir Soffer
  • On Oct 10, 2006, at 2:21 PM, Shawn Erickson wrote:
    > Can you outline how you construct the NSString you feed into
    > URLWithString:? Also how you convert from a Str255 to a C string?

    Sure, from the Cocoa end of things:

    NSAppleEventDescriptor *directObjectDescriptor = [event
    paramDescriptorForKeyword:keyDirectObject];
    NSAppleEventDescriptor *myAEDescriptor = [directObjectDescriptor
    descriptorAtIndex:1];
    NSString *urlString = [myAEDescriptor stringValue];

    and in the Pascal app:

    procedure ConvertPStringToCString(var returnValue: Str255);

    var
    count: integer;
    utilStr: Str255;

    begin
    utilStr:= returnValue;
    for count:= 0 to 255
      do returnValue[count]:= chr(0);
    for count:= 1 to length(utilStr)
      do returnValue[count - 1]:= utilStr[count];
    returnValue[length(utilStr)]:= chr(0);
    end;  { ConvertPStringToCString }

    and in the create Apple Event function:

    string1:= 'http://www.apple.com';
    ConvertPStringToCString(string1);
    iErr:= AECreateDesc(typeCString, @string1, 256, docDesc);

    If I look at string1 in the debugger, it is indeed (effectively) an
    array of char, first bit being the URL, last bit all zeros.  I'm
    operating on the assumption that data is data, I'm just dealing with
    pointers here, so it doesn't matter how it's being stored
    internally.  Maybe that's my problem.

    dale

    --
    <adjensen...>  http://kandsmil.blogspot.com

    Only a fool turns away from an angel,
    Vision in white steps through the door,
    Holds out her hand, I say 'No I'm not ready'
    I open my eyes and let the dream fade away.
      -- echolyn, "on any given nite"
  • On Tue, 10 Oct 2006 14:08:34 -0500, "a.d. jensen" <adjensen...>
    said:
    > I have this really annoying issue that keeps coming up over and over
    > and I'm at my wits end.  Hopefully I can get some help.
    >
    > I have a Cocoa application that receives a URL in a string in an
    > Apple Event and opens a web view of the page.  I have two different
    > applications that are sending this Apple Event, one is a Carbon C++
    > program, the other a Carbon Pascal app (written in FPC, compiled in
    > xCode.)
    >
    > The C++ program seems to send the string fine, and the Cocoa app
    > opens the page fine.
    >
    > The Pascal program, on the other hand, just seems to send over bad
    > strings (which I've converted from Pascal Str255 to zero-terminated C
    > strings).  They SEEM to be valid, but when I run this:
    >
    > NSURL *myURL = [NSURL URLWithString: urlString];
    >
    > I get a nil myURL.  Documentation says it will be nil if "urlString"
    > is invalid.  In the debugger, it sure LOOKS valid, and if I replace
    > that line with:
    >
    > NSURL *myURL = [NSURL URLWithString: @" http://www.apple.com "];
    >
    > it works fine.
    >
    > How do I figure out what's wrong with " http://www.apple.com " in a
    > NSString, that isn't wrong when I use @" http://www.apple.com " directly?

    You say you're getting this value originally from an Apple event, but you
    don't say what Apple event it is. It might be crucial to know that. I ran
    into a situation recently where I was getting back a UTF-16 bytestring and
    the receiving program (parallel to your Pascal situation?) didn't know what
    to do with that. Like your example, it *looked* right, but that was because
    I couldn't see the null characters between the visible characters. m.

    --
    matt neuburg, phd = <matt...>, <http://www.tidbits.com/matt/>
    A fool + a tool + an autorelease pool = cool!
    AppleScript: the Definitive Guide - Second Edition!
    <http://www.amazon.com/gp/product/0596102119>
previous month october 2006 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