• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Writing Text files
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Writing Text files


  • Subject: Re: Writing Text files
  • From: John Delacour <email@hidden>
  • Date: Sat, 17 Nov 2012 15:03:15 +0000

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


  • Follow-Ups:
    • Re: Writing Text files
      • From: Andreas Kiel <email@hidden>
References: 
 >Writing Text files (From: Andreas Kiel <email@hidden>)

  • Prev by Date: Re: Writing Text files
  • Next by Date: Re: Writing Text files
  • Previous by thread: Re: Writing Text files
  • Next by thread: Re: Writing Text files
  • Index(es):
    • Date
    • Thread