• 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: Read a text file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Read a text file


  • Subject: Re: Read a text file
  • From: Gil Dawson <email@hidden>
  • Date: Wed, 06 Aug 2014 08:12:01 -0700

On 31 Jul 2014, at 5:05 am, Gil Dawson <email@hidden> wrote:
Where is TextWrangler getting this information?

On Aug 1, 2014, at 5:00 AM, Shane Stanley wrote:
From an extended attribute saved with the file, under the name com.apple.TextEncoding.

So, when I used TextEdit to create those three ".txt" files, each with a different encoding in the "Save As..." dialog...

One to UTF-8
One to Western (MacOS Roman)
One to Western (Windows Latin 1)

...the three files were not "plain text" files, but each must have had had an extended attribute saved with it (somewhere).

I was wondering is there any way to pick up this extended attribute from within ASObj-C.  Then I looked through the NSString class methods and I found:

stringWithContentsOfFile:usedEncoding:error:

Returns a string created by reading data from the file at a given path and returns by reference the encoding used to interpret the file.

+ (id)stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error
Parameters
path
A path to a file.
enc
Upon return, if the file is read successfully, contains the encoding used to interpret the file atpath.
error
If an error occurs, upon returns contains an NSError object that describes the problem. If you are not interested in possible errors, you may pass in NULL.

And then...

The following constants are provided by NSString as possible string encodings.

enum {
   NSASCIIStringEncoding = 1,
   NSNEXTSTEPStringEncoding = 2,
   NSJapaneseEUCStringEncoding = 3,
   NSUTF8StringEncoding = 4,
   NSISOLatin1StringEncoding = 5,
   NSSymbolStringEncoding = 6,
   NSNonLossyASCIIStringEncoding = 7,
   NSShiftJISStringEncoding = 8,
   NSISOLatin2StringEncoding = 9,
   NSUnicodeStringEncoding = 10,
   NSWindowsCP1251StringEncoding = 11,
   NSWindowsCP1252StringEncoding = 12,
   NSWindowsCP1253StringEncoding = 13,
   NSWindowsCP1254StringEncoding = 14,
   NSWindowsCP1250StringEncoding = 15,
   NSISO2022JPStringEncoding = 21,
   NSMacOSRomanStringEncoding = 30,
   NSUTF16StringEncoding = NSUnicodeStringEncoding,
   NSUTF16BigEndianStringEncoding = 0x90000100,
   NSUTF16LittleEndianStringEncoding = 0x94000100,
   NSUTF32StringEncoding = 0x8c000100,
   NSUTF32BigEndianStringEncoding = 0x98000100,
   NSUTF32LittleEndianStringEncoding = 0x9c000100,
};

Perfect!

Thanks for the suggestion, Shane!

--Gil

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Read a text file
      • From: Shane Stanley <email@hidden>
References: 
 >Re: Read a text file (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: Applescript AeroSnap
  • Next by Date: Re: Applescript AeroSnap
  • Previous by thread: Re: Read a text file
  • Next by thread: Re: Read a text file
  • Index(es):
    • Date
    • Thread