Re: NSTextView, unicode, Applescript and Mail
Re: NSTextView, unicode, Applescript and Mail
- Subject: Re: NSTextView, unicode, Applescript and Mail
- From: Andrew Thompson <email@hidden>
- Date: Sun, 5 Oct 2003 17:33:10 -0400
On Sunday, Oct 5, 2003, at 15:27 America/New_York, Nick Morris wrote:
Hi,
The problem: Unicode text (Japanese characters) in an NSTextView
(theTextView) is passed to Mail via an Applescript in the program....
('theContents' is set first for reasons in other sections of the
program)
[theScript setString:[NSString stringWithFormat:@"set theContents to
\"%@\"\n", [theTextView string]]];
[theScript appendString:@"tell application \"Mail\"\n"];
[theScript appendString:@"activate\n"];
[theScript appendString:@"set the new_message to make new outgoing
message at beginning of every outgoing message\n"];
[theScript appendString:@"tell new_message\n"];
[theScript appendString:@"set the content to theContents\n"];
[theScript appendString:@"end tell\n"];
[theScript appendString:@"make new OLD message editor at beginning of
every OLD message editor\n"];
[theScript appendString:@"set outgoing message of last OLD message
editor to new_message\n"];
[theScript appendString:@"end tell\n"];
mailScript = [[NSAppleScript alloc] initWithSource:[NSString
stringWithString:theScript]];
result = [mailScript executeAndReturnError:&scriptErr];
All works great with text such as 'This is some text' but as soon as I
start trying to pass the Japanese characters I get '??' in Mail.
Any thoughts?
I don't know for sure, but my guess is this:
AppleScript and Cocoa use 2 different representations for unicode.
AppleScript uses a form of UCS-2 encoding, whereas Cocoa NSString uses
UTF-16.
I implemented these methods in a Category on NSString to deal with this
problem in my code:
static unichar UFI_LEFT_DOUBLE_ANGLE_QUOTATION_MARK = 0x00AB; // +
static unichar UFI_RIGHT_DOUBLE_ANGLE_QUOTATION_MARK = 0x00BB; // ;
static NSString *UFI_APPLESCRIPT_DATA_HEADER = @"data utxt";
@implementation NSString (UFIStringAdditions)
- (NSString *) toUCS2Digits {
NSMutableString *result = [[[NSMutableString alloc]
initWithCapacity: [self length]] autorelease];
unsigned int i=0;
for (i=0; i < [self length]; i++) {
unichar curr= [self characterAtIndex: i];
[result appendString: [NSString stringWithFormat: @"X",
curr]];
}
return result;
}
- (NSString *) toAppleScriptData {
return [NSString stringWithFormat: @"%@%@%@%@",
[NSString stringWithCharacters:
&UFI_LEFT_DOUBLE_ANGLE_QUOTATION_MARK length: 1],
UFI_APPLESCRIPT_DATA_HEADER,
[self toUCS2Digits],
[NSString stringWithCharacters:
&UFI_RIGHT_DOUBLE_ANGLE_QUOTATION_MARK length: 1]];
}
@end
If you call toAppleScriptData on an NSString using this Category, it
converts it to something like:
data utxt <<00112233445566778899>>
Where the digits represent your original string, naturally.
This you can use in an AppleScript in place of a regular "string".
Hope that works for you.
AndyT (lordpixel - the cat who walks through walls)
A little bigger on the inside
(see you later space cowboy ...)
_______________________________________________
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.