Sunday, November 19, 2017

#26 2017-03-14 06:18:38 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4421

Re: Change file encoding of TextEdit by Applescript

Yvan Koenig wrote:

I tried to use an awful scheme to insert the BOM.


Mmm….  hmm  How about this?

Applescript:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

on modifyPath:thePath adding:addString
   set pathString to current application's NSString's stringWithString:thePath
   set theExtension to pathString's pathExtension()
   set thePathNoExt to pathString's stringByDeletingPathExtension()
   set newPath to (thePathNoExt's stringByAppendingString:addString)'s stringByAppendingPathExtension:theExtension
   return newPath as string
end modifyPath:adding:

on decodeFile:thePath
   -- Get the BOM value as a two-character string. (The single character id (254 * 256 + 255) gets lost in the conversion to NSString.)
   set theUTF16BEBOM to current application's NSString's stringWithString:(string id {254, 255})
   -- Convert it to two bytes of data.
   set theData to (theUTF16BEBOM's dataUsingEncoding:(current application's NSISOLatin1StringEncoding))'s mutableCopy()
   -- Read the contents of the ISO Latin 1 text file.
   set theString to current application's NSString's stringWithContentsOfFile:thePath encoding:(current application's NSISOLatin1StringEncoding) |error|:(missing value)
   -- Convert that to data too, but encoded as UTF-16 big-endian, and append it to the BOM data.
   tell theData to appendData:(theString's dataUsingEncoding:(current application's NSUTF16BigEndianStringEncoding))
   -- Write the lot to a new file.
   set newPath to my modifyPath:thePath adding:"-new"
   set theResult to theData's writeToFile:newPath atomically:true
   
   return {newPath, theResult as boolean}
end decodeFile:

set theSource to (choose file)
set {newPath, bof} to my decodeFile:(POSIX path of theSource)

Last edited by Nigel Garvey (2017-03-14 06:28:30 pm)


NG

Offline

 

#27 2017-03-14 06:51:39 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5175

Re: Change file encoding of TextEdit by Applescript

Nigel Garvey wrote:

How about this?


Nice smile

An alternative that might work would be create the BOM as a zero width no-break space. Unfortunately this only works in 10.11 and above:

Applescript:

set theString to current application's NSString's stringWithString:"\\N{ZERO WIDTH NO-BREAK SPACE}"
set theString to theString's stringByApplyingTransform:(current application's NSStringTransformToUnicodeName) |reverse|:true

You could then append the contents of the file to that string, and save. I don't have a suitable sample to test it.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#28 2017-03-14 07:49:31 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4421

Re: Change file encoding of TextEdit by Applescript

Shane Stanley wrote:
Nigel Garvey wrote:

How about this?


Nice smile


Thank you.  smile  And for your previous reply.

An alternative that might work would be create the BOM as a zero width no-break space. Unfortunately this only works in 10.11 and above:

Applescript:

set theString to current application's NSString's stringWithString:"\\N{ZERO WIDTH NO-BREAK SPACE}"
set theString to theString's stringByApplyingTransform:(current application's NSStringTransformToUnicodeName) |reverse|:true

You could then append the contents of the file to that string, and save. I don't have a suitable sample to test it.


That works for me if I convert both to data before appending them, as in my version above. But if I append them as NSStrings and save the result, the resulting file crashes anything that tries to open or read it. (Well. TextEdit and a 'read (choose file) as Unicode text'' script anyway.) Sounds similar to what Yvan was getting with his version. I'll have another look in the morning (GMT).


NG

Offline

 

#29 2017-03-15 04:13:42 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3201

Re: Change file encoding of TextEdit by Applescript

Thank you Nigel and Shane.
I just ignored the way to define the string containing the BOM.

About the problem which I describe.
May it due to the fact that the system keep the fact that the late write operation applied to the file was a write «class data one in the file's metadatas ?
When the late action write text data like the late Nigel's proposal or the shell version, the metadatas would record that and so TextEdit is satisfied.

I must add that when I compare the hexadecimal contents of the different attempts, they are identical.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) mercredi 15 mars 2017 10:13:37

Last edited by Yvan Koenig (2017-03-15 04:18:59 am)

Offline

 

#30 2017-03-15 04:24:27 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2724
Website

Re: Change file encoding of TextEdit by Applescript

Shane Stanley wrote:
Nigel Garvey wrote:

According the the Xcode documentation, this enum was only introduced with MacOS 10.12, but it works on my 10.11 system.


The documentation is wrong (it says the same thing about NSASCIIStringEncoding mad). I believe it was introduced in 10.4.


That's right, for these kind of things I prefer to read the CoreFoundation frameworks rather than the Cocoa frameworks. CoreFoundation team wrote it and their documentation seems more accurate. At least it says that kCFStringEncodingUTF16BE is introduced in Mac OS 10.4+ while k​CFString​Encoding​Unicode is since the first release of Mac OS X.

When stepping to the x86 architecture the native endianness changed which caused problems with UTF16 encoded files back then, and that was around 10.4. While the PowerPC could run in both endianness mode it ran in big endian for Macintosh systems, therefore UTF16 was big endian by default. When the Intel processor was introduced the native endianness was little endian and a lot of software had trouble reading the PPC written UTF16 files. Therefore to read PPC written UTF16 files that followed PPC's native endianness on an x86 machine you could use the key kCFStringEncodingUTF16BE.

Last edited by DJ Bazzie Wazzie (2017-03-15 04:28:05 am)

Offline

 

#31 2017-03-15 06:01:23 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5175

Re: Change file encoding of TextEdit by Applescript

Yvan Koenig wrote:

About the problem which I describe.
May it due to the fact that the system keep the fact that the late write operation applied to the file was a write «class data one in the file's metadatas ?


Yes, it's possible something is being written as an extended attribute, and TextEdit might well look at that.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#32 2017-03-15 06:09:09 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5175

Re: Change file encoding of TextEdit by Applescript

DJ Bazzie Wazzie wrote:

for these kind of things I prefer to read the CoreFoundation frameworks rather than the Cocoa frameworks. CoreFoundation team wrote it and their documentation seems more accurate. At least it says that kCFStringEncodingUTF16BE is introduced in Mac OS 10.4+ while k​CFString​Encoding​Unicode is since the first release of Mac OS X.


Yes, although kCFStringEncodingUTF16BE is not the same value as NSUTF16BigEndianStringEncoding, and it's at least theoretically possible that the encoding was supported earlier in CoreFoundation -- the transform constants are an example of that.

The problem, I suspect, is that mistakes are being made because some enums and constants are being renamed to a naming scheme that fits better with Swift.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#33 2017-03-15 06:19:11 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4421

Re: Change file encoding of TextEdit by Applescript

Yvan Koenig wrote:

About the problem which I describe.


Hi Yvan.

I've just been fooling around with your script in various ways.

1. The crashing only occurs if the data has been written to the file. Not if it hasn't or if something else of the same length been written instead.
2. The crashing appears to be a system problem, rather than just TextEdit. Merely selecting the file in a 'choose file' dialog crashes the host application before the "OK" button can be clicked.
3. A workaround seems to be to write the BOM as a short integer instead of as data. Fortunately, UTF-16 BOMs can be represented in this way.

Applescript:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

on modifyPath:thePath adding:addString
   set pathString to current application's NSString's stringWithString:thePath
   set theExtension to pathString's pathExtension()
   set thePathNoExt to pathString's stringByDeletingPathExtension()
   set newPath to (thePathNoExt's stringByAppendingString:addString)'s stringByAppendingPathExtension:theExtension
   return newPath --as text
end modifyPath:adding:

on decodeFile:thePath
   set theString to (current application's NSString's stringWithString:" ")
   set moreString to current application's NSString's stringWithContentsOfFile:thePath encoding:(current application's NSISOLatin1StringEncoding) |error|:(missing value)
   set theString to theString's stringByAppendingString:moreString
   set newPath to my modifyPath:thePath adding:"-new"
   set theResult to theString's writeToFile:newPath atomically:true encoding:(current application's NSUTF16BigEndianStringEncoding) |error|:(missing value)
   set newPath to newPath as text
   write -512 as short to (get POSIX file newPath) -- Will open, start at 1, and close anyway, since the file already exists.
   return {newPath, theResult as boolean}
end decodeFile:

set theSource to (choose file)
set {newPath, bof} to my decodeFile:(POSIX path of theSource)


NG

Offline

 

#34 2017-03-15 06:48:52 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4421

Re: Change file encoding of TextEdit by Applescript

Shane Stanley wrote:

An alternative that might work would be create the BOM as a zero width no-break space. Unfortunately this only works in 10.11 and above:

Applescript:

set theString to current application's NSString's stringWithString:"\\N{ZERO WIDTH NO-BREAK SPACE}"
set theString to theString's stringByApplyingTransform:(current application's NSStringTransformToUnicodeName) |reverse|:true

You could then append the contents of the file to that string, and save.


Hi Shane.

I suspect that including the zero-width no-break space in the string that gets written to the file with NSUTF16BigEndianStringEncoding does something undesirable to that character. The encoding needs to be sorted out before the write, which is the idea behind my NSData approach. Your idea works well when plugged into that.


NG

Offline

 

#35 2017-03-15 07:14:37 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5175

Re: Change file encoding of TextEdit by Applescript

Nigel Garvey wrote:

The encoding needs to be sorted out before the write, which is the idea behind my NSData approach.


Yes, that makes sense.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#36 2017-03-15 10:07:44 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3201

Re: Change file encoding of TextEdit by Applescript

Thanks Nigel.

It's good to know that we may write the BOM as a number but I will stay with your version which does all the job with ASObjC.


Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) mercredi 15 mars 2017 16:07:39

Offline

 

#37 2017-03-15 12:55:02 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4421

Re: Change file encoding of TextEdit by Applescript

With Shane's string transform suggestion, you can either create separate data blocks from the BOM character and the string from the original file and then join the blocks, or append the string from the file to the BOM character and create a data block from the result. This version does the latter:

Applescript:

use AppleScript version "2.5" -- Mac OS 10.11 (El Capitan) or later.
use framework "Foundation"
use scripting additions

on modifyPath:thePath adding:addString
   set pathString to current application's NSString's stringWithString:thePath
   set theExtension to pathString's pathExtension()
   set thePathNoExt to pathString's stringByDeletingPathExtension()
   set newPath to (thePathNoExt's stringByAppendingString:addString)'s stringByAppendingPathExtension:theExtension
   return newPath as string
end modifyPath:adding:

on decodeFile:thePath
   -- Create a character with the same Unicode value as a UTF-16 BE BOM.
   set theUTF16BEBOM to current application's NSString's stringWithString:"\\N{ZERO WIDTH NO-BREAK SPACE}"
   set theUTF16BEBOM to theUTF16BEBOM's stringByApplyingTransform:(current application's NSStringTransformToUnicodeName) |reverse|:true
   -- Read the contents of the ISO Latin 1 text file and append it to the BOM.
   set theString to current application's NSString's stringWithContentsOfFile:thePath encoding:(current application's NSISOLatin1StringEncoding) |error|:(missing value)
   set theString to theUTF16BEBOM's stringByAppendingString:theString
   -- Convert the result to data, encoded as UTF-16 big-endian.
   set theData to theString's dataUsingEncoding:(current application's NSUTF16BigEndianStringEncoding)
   -- Write the data to a new file.
   set newPath to my modifyPath:thePath adding:"-new"
   set theResult to theData's writeToFile:newPath atomically:true
   
   return {newPath, theResult as boolean}
end decodeFile:

set theSource to (choose file)
set {newPath, bof} to my decodeFile:(POSIX path of theSource)

And since it's proved an intesting area for exploration, here's a version which writes the BOM and the text to the new file separately:

Applescript:

use AppleScript version "2.5" -- Mac OS 10.11 (El Capitan) or later.
use framework "Foundation"
use scripting additions

on modifyPath:thePath adding:addString
   set pathString to current application's NSString's stringWithString:thePath
   set theExtension to pathString's pathExtension()
   set thePathNoExt to pathString's stringByDeletingPathExtension()
   set newPath to (thePathNoExt's stringByAppendingString:addString)'s stringByAppendingPathExtension:theExtension
   return newPath as string
end modifyPath:adding:

on decodeFile:thePath
   -- Create a character with the same Unicode value as a UTF-16 BE BOM.
   set theUTF16BEBOM to current application's NSString's stringWithString:"\\N{ZERO WIDTH NO-BREAK SPACE}"
   set theUTF16BEBOM to theUTF16BEBOM's stringByApplyingTransform:(current application's NSStringTransformToUnicodeName) |reverse|:true
   -- Convert it data.
   set BOMData to theUTF16BEBOM's dataUsingEncoding:(current application's NSUTF16BigEndianStringEncoding)
   -- Read the contents of the ISO Latin 1 text file.
   set theString to current application's NSString's stringWithContentsOfFile:thePath encoding:(current application's NSISOLatin1StringEncoding) |error|:(missing value)
   -- Convert that to data too, encoded as UTF-16 big-endian.
   set stringData to theString's dataUsingEncoding:(current application's NSUTF16BigEndianStringEncoding)
   -- Create a new file.
   set theResult to false
   set newPath to my modifyPath:thePath adding:"-new"
   tell current application's NSFileManager's defaultManager() to createFileAtPath:newPath |contents|:(missing value) attributes:(missing value)
   -- Open it for access with write permission, write the two blocks of data to it, and close it again.
   set fileAccess to current application's NSFileHandle's fileHandleForWritingAtPath:newPath
   try
       tell fileAccess to writeData:BOMData
       tell fileAccess to writeData:stringData
       set theResult to true
   end try
   tell fileAccess to closeFile()
   
   return {newPath, theResult as boolean}
end decodeFile:

set theSource to (choose file)
set {newPath, bof} to my decodeFile:(POSIX path of theSource)

Last edited by Nigel Garvey (2017-03-16 02:36:46 am)


NG

Offline

 

#38 2017-03-15 01:24:57 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3201

Re: Change file encoding of TextEdit by Applescript

Thanks Nigel

I'm scrapping my head.
Isn't it a function allowing ASObjC to append datas to the already written ones ?


Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) mercredi 15 mars 2017 19:24:51

Offline

 

#39 2017-03-16 02:38:29 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4421

Re: Change file encoding of TextEdit by Applescript

Thanks, Shane. I've now sorted that out.


NG

Offline

 

#40 2017-03-16 04:18:27 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4421

Re: Change file encoding of TextEdit by Applescript

Yvan Koenig wrote:

Isn't it a function allowing ASObjC to append datas to the already written ones ?


I'm not sure if you're asking if one exists or if that's what I've used (in the second script in post #37).

As far as I can see, NSString's and NSData's writeToFile methods either create files containing just the given material or completely replace the contents of existing files. They don't have methods for editing files in-place.

The NSFileHandle class seems to be the equivalent of the file system object created by open for access in the StandardAdditions, but with a few differences in the way it's scripted. The significant differences here are:

1. Files which don't already exist have to be explicitly created first. I've used NSFileManager for this. (Files which do already exist are effectually emptied if created again with NSFileManager.)
2. NSFileHandle only writes NSData objects. (According to the documentation, anyway. I haven't put it to the test.)

Otherwise, as with write in the StandardAdditions, each successive write starts where the previous one ended unless something is done to change the file handle's file pointer.


NG

Offline

 

#41 2017-03-16 04:44:17 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Change file encoding of TextEdit by Applescript

In Cocoa you can use NSMutableData to append NSData chunks and then the writeToFile... / writeToURL... methods of NSData to write the data directly to disk. The NSFileHandle way is actually not needed in this case.

There are more roads which lead to Rome than in AppleScript wink

Last edited by StefanK (2017-03-16 04:48:18 am)


regards

Stefan

Offline

 

#42 2017-03-16 04:54:28 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3201

Re: Change file encoding of TextEdit by Applescript

Nigel Garvey wrote:
Yvan Koenig wrote:

Isn't it a function allowing ASObjC to append datas to the already written ones ?


I'm not sure if you're asking if one exists or if that's what I've used (in the second script in post #37).

As far as I can see, NSString's and NSData's writeToFile methods either create files containing just the given material or completely replace the contents of existing files. They don't have methods for editing files in-place.

The NSFileHandle class seems to be the equivalent of the file system object created by open for access in the StandardAdditions, but with a few differences in the way it's scripted. The significant differences here are:

1. Files which don't already exist have to be explicitly created first. I've used NSFileManager for this. (Files which do already exist are effectually emptied if created again with NSFileManager.)
2. NSFileHandle only writes NSData objects. (According to the documentation, anyway. I haven't put it to the test.)

Otherwise, as with write in the StandardAdditions, each successive write starts where the previous one ended unless something is done to change the file handle's file pointer.


Just an example:
I was asking if there is a way to write the BOM
then write the text itself.
At this time you append the text to the BOM in ram them write the entire data in a single instruction.

As you pointed, this may be done with old fashioned AppleScript.


Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) jeudi 16 mars 2017 10:54:14

Offline

 

#43 2017-03-16 04:55:53 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3201

Re: Change file encoding of TextEdit by Applescript

StefanK wrote:

In Cocoa you can use NSMutableData to append NSData chunks and then the writeToFile... / writeToURL... methods of NSData to write the data directly to disk. The NSFileHandle way is actually not needed in this case.


Isn't it what is done by the first script given in message #37 ?


Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) jeudi 16 mars 2017 10:55:49

Offline

 

#44 2017-03-16 05:29:43 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Change file encoding of TextEdit by Applescript

Yvan Koenig wrote:

Isn't it what is done by the first script given in message #37 ?


Basically yes, but not after putting `NSData` instances together like writing data serially to disk via NSFileHandle


regards

Stefan

Offline

 

#45 2017-03-16 05:35:27 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2724
Website

Re: Change file encoding of TextEdit by Applescript

Shane Stanley wrote:

Yes, although kCFStringEncodingUTF16BE is not the same value as NSUTF16BigEndianStringEncoding


They are not values but they are masks, who are the same. For instance the default unicode mask contains two encodings.

Nigel Garvey wrote:

As far as I can see, NSString's and NSData's writeToFile methods either create files containing just the given material or completely replace the contents of existing files. They don't have methods for editing files in-place.


It's not possible with text files in general so those methods won't exist. That is where the power of binary files comes in with data blocks but that doesn't work for NSData or NSString classes. For larger files you could also use streams to reduce the memory footprint.

StefanK wrote:

In Cocoa you can use NSMutableData to append NSData chunks and then the writeToFile... / writeToURL... methods of NSData to write the data directly to disk. The NSFileHandle way is actually not needed in this case.


Appending data to the NSData object could be resource heavy which makes a file handle (session write) much more efficient than concatenating data and write it as a whole to disk. From efficiency perspective a far better choice.

Last edited by DJ Bazzie Wazzie (2017-03-16 05:48:17 am)

Offline

 

#46 2017-03-16 06:12:23 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5175

Re: Change file encoding of TextEdit by Applescript

DJ Bazzie Wazzie wrote:
Shane Stanley wrote:

Yes, although kCFStringEncodingUTF16BE is not the same value as NSUTF16BigEndianStringEncoding


They are not values but they are masks, who are the same.


No, they're not the same -- one is 0x10000100 and the other is 0x90000100.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#47 2017-03-16 06:20:11 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2724
Website

Re: Change file encoding of TextEdit by Applescript

Shane Stanley wrote:
DJ Bazzie Wazzie wrote:
Shane Stanley wrote:

Yes, although kCFStringEncodingUTF16BE is not the same value as NSUTF16BigEndianStringEncoding


They are not values but they are masks, who are the same.


No, they're not the same -- one is 0x10000100 and the other is 0x90000100.


The mask is 0x100 the other bit is CF or NS identification but is not an encoding mask.

More specifically the NSENCODING_MASK is added to NSUTF16BigEndianStringEncoding which is 1<<31 value, note the starting 9. So while their value may not be the same, their masks are the same just what I said before.

Offline

 

#48 2017-03-16 06:21:12 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4421

Re: Change file encoding of TextEdit by Applescript

Yvan Koenig wrote:

I was asking if there is a way to write the BOM
then write the text itself.
At this time you append the text to the BOM in ram them write the entire data in a single instruction.


Hi Yvan.

The script in post #26 makes an NSMutableData version of the BOM and an NSData version of the text, appends the latter to the former, and writes the result to a new file with a single write.

The first script in post #37 appends the text to a character with the same Unicode value as the BOM, makes an NSData version of the result, and writes that to a new file with a single write.

The second script in post #37 makes NSData versions of both the BOM and the text, creates a new file, opens it for writing, writes the BOM data, then writes the text data, then closes the access.

The post #26 script's probably the best of the three for the current purpose, since it does only do one write and also works in Mac OS 10.10. The other two are simply explorations of different approaches.


NG

Offline

 

#49 2017-03-16 09:37:33 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3201

Re: Change file encoding of TextEdit by Applescript

Oops, I didn't took care that

Applescript:

try
   tell fileAccess to writeData:BOMData
   tell fileAccess to writeData:stringData
   set theResult to true
end try

is ASObjC code, not old fashioned code.

So now it seems that I understood.
If I made no error we may also use this 4th version:

Applescript:

# [url]http://macscripter.net/viewtopic.php?id=28482&p=2[/url]
# message #26 alt

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

on modifyPath:thePath adding:addString
on modifyPath:thePath adding:addString
   set pathString to current application's NSString's stringWithString:thePath
   set theExtension to pathString's pathExtension()
   set thePathNoExt to pathString's stringByDeletingPathExtension()
   set newPath to (thePathNoExt's stringByAppendingString:addString)
   if theExtension's |length|() > 0 then
       set newPath to newPath's stringByAppendingPathExtension:theExtension
   end if
   return newPath as string
end modifyPath:adding:

on decodeFile:thePath
   -- Get the BOM value as a two-character string. (The single character id (254 * 256 + 255) gets lost in the conversion to NSString.)
   set theUTF16BEBOM to current application's NSString's stringWithString:(string id {254, 255})
   -- Convert it to two bytes of data.
   set BOMData to theUTF16BEBOM's dataUsingEncoding:(current application's NSISOLatin1StringEncoding)
   
   -- Read the contents of the ISO Latin 1 text file.
   set theString to current application's NSString's stringWithContentsOfFile:thePath encoding:(current application's NSISOLatin1StringEncoding) |error|:(missing value)
   -- Convert that to data too, but encoded as UTF-16 big-endian, and append it to the BOM data.
   set stringData to theString's dataUsingEncoding:(current application's NSUTF16BigEndianStringEncoding)
   -- Write the lot to a new file.
   set newPath to my modifyPath:thePath adding:"-new"
   
   tell current application's NSFileManager's defaultManager() to createFileAtPath:newPath |contents|:(missing value) attributes:(missing value)
   -- Open it for access with write permission, write the two blocks of data to it, and close it again.
   set fileAccess to current application's NSFileHandle's fileHandleForWritingAtPath:newPath
   try
       tell fileAccess to writeData:BOMData
       tell fileAccess to writeData:stringData
       set theResult to true
   end try
   tell fileAccess to closeFile()
   
   return {newPath, theResult as boolean}
end decodeFile:

set theSource to (choose file)
set {newPath, bof} to my decodeFile:(POSIX path of theSource)

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) jeudi 16 mars 2017 15:37:24

Last edited by Yvan Koenig (2017-03-19 11:03:51 am)

Offline

 

#50 2017-03-16 11:10:14 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3201

Re: Change file encoding of TextEdit by Applescript

StefanK wrote:
Yvan Koenig wrote:

Isn't it what is done by the first script given in message #37 ?


Basically yes, but not after putting `NSData` instances together like writing data serially to disk via NSFileHandle


It's the 2nd script in message #37 which behaves this way.

I asked about the first one in message #37.


Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) jeudi 16 mars 2017 17:10:04

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)