Re: NSString from Carbon by AppleEvent
Re: NSString from Carbon by AppleEvent
- Subject: Re: NSString from Carbon by AppleEvent
- From: John Nairn <email@hidden>
- Date: Fri, 30 May 2003 09:23:07 -0600
I was finally able to pass NSString parameter from Carbon to Cocoa
using an Apple event, but I never found Apple documentation on what
must be common (and non-obvious, except in hindsight) task. I found
similar solution buried in MoreAppleEvents sample code and got help
from this list (thanks).
Here is code fragment in case others need the same task:
Byte locDate[256]; // Pascal string to become NSString
CFStringRef dateRef;
long length;
UniChar *buffer;
OSErr myErr;
AEDesc argDesc;
AppleEvent theEvent; // already defined to send command to a
process
// Step 1: convert Pascal String to CFString
dateRef=CFStringCreateWithPascalString(NIL,locDate,kCFStringEncodingMacR
oman);
if(dateRef==NIL) goto bail;
// Step 2: extract unicode text to buffer
length=CFStringGetLength(dateRef);
buffer=(UniChar *)NewPtr(length*sizeof(UniChar));
if((myErr=MemError())!=noErr)
{ CFRelease(dateRef);
goto bail;
}
CFStringGetCharacters(dateRef,CFRangeMake(0,length),buffer);
CFRelease(dateRef);
// Step 3: create AEDesc as typeUnicodeText
myErr=AECreateDesc(typeUnicodeText,buffer,length*sizeof(UniChar),&argDes
c);
DisposePtr((Ptr)buffer);
if(myErr!=noErr) goto bail;
// Step 4: put AEDesc into existing event
// 2nd parameter as AE code defined in Cocoa app for this
NSString argument
myErr=AEPutParamDesc(&theEvent,'eDat',&argDesc);
if(myErr!=noErr) goto bail;
On Thursday, May 29, 2003, at 09:58 PM, Brian Webster wrote:
>
On Thursday, May 29, 2003, at 03:07 PM,
>
email@hidden wrote:
>
>
> 3. Turn CFString into AEDesc and add as parameter to AppleEvent (this
>
> is NOT working)
>
>
To make an AEDesc for a string, you must pass it as a raw array of
>
Unicode characters. So, you want to use CFStringGetCharacters() to
>
put the characters into a UniChar* and then create an AEDesc with type
>
typeUnicodeText, passing in the UniChar* as the descriptor's data.
>
>
--
>
Brian Webster
------------
John Nairn (1-801-581-3413, FAX: 1-801-581-4816)
Web page:
http://www.mse.utah.edu/~nairn
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.