Re: Writing Text files
Re: Writing Text files
- Subject: Re: Writing Text files
- From: Andreas Kiel <email@hidden>
- Date: Sat, 17 Nov 2012 16:16:29 +0100
John,
Thanks for the explanation.
The problem is that the application is used by many people on many machines.
So on each individual machine it works fine in most cases - unfortunately only in most but not all.
So for example 2 people work on the same kind of machine setup (OS, Applications (and version of them), Language, Keyboard etc).
For both of them the "example" text (created on each machine with my app) opens fine in TextEdit. For one of them it will be misinterpreted in Apple's DVD SP - even though both use the same version of the app. And there is no way to set up decoding for the latter app.
Another user with a different version of the OS (other stuff the same) creates the same file with my app - but the other people (first two) will have problems with this text file.
So in any case you have to do a kind of "force encoding" when opening the text file and re-save it.
This behavior does not apply only to the text files created by my app. You can create a text file in TextEdit with some special characters, save it - and the behavior is the same.
Thanks
Andreas
On Nov 17, 2012, at 4:03 PM, John Delacour wrote:
> On 05/11/2012 15:17, Andreas Kiel wrote:
>
>> I'm trying to bring some sense into a problem saving text files either in UTF8 or UTF16.
>>
>> so I use either a
>> call method "writeToFile:atomically:encoding:error:" of someText with parameters {savePath, true, 4, 1}
>> or a
>> call method "writeToFile:atomically:encoding:error:" of someText with parameters {savePath, true, 16, 1}
>
> I’m not too familiar with the use of AppleScript within XCode. So far as the ObjC version goes, if you give a value of 1 for error, it will fail; nil or 0 are OK.
>
> - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
> {
> NSString *some_text = @"你好";
> NSString *file_path = @"/tmp/temp.txt";
> [some_text writeToFile:file_path
> atomically:1
> encoding:4 /* or NSUTF8StringEncoding */
> error:0 /* or nil */];
> }
>
> As regards the ‘call method...’ AppleScript, if you can’t resolve the problem, you can always use the traditional way as below (tested and verified). Whether you are using BBEdit or TextEdit to display the files, either app will open in the default encoding you have specified in your preferences. In BBEdit you can “reopen using encoding...”; in TextEdit you will need to change your open and save prefs and then reopen. You should not use a BOM with UTF-8 files, but if BBEdit (and probably TextEdit) reads a BOM in a UTF-16.. file it will respect the encoding. I recommend you set your default encoding in BBEdit to UTF-8.
>
>
> script ASAppDelegate
> property parent : class "NSObject"
>
> on applicationWillFinishLaunching_(aNotification)
> set _path to "/tmp/temp.txt"
> set _text to "你好"
> try
> close access _path
> end try
> set _fd to open for access _path with write permission
> set eof _fd to 0
> write _text to _fd as «class utf8» -- or as unicode text (for UTF-16)
> close _fd
> end applicationWillFinishLaunching_
> -- ...
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> applescriptobjc-dev mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
applescriptobjc-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden