Wednesday, November 22, 2017

#51 2017-03-16 11:51:57 am

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

Re: Change file encoding of TextEdit by Applescript

Yvan Koenig wrote:

If I made no error we may also use this 4th version:


Yes.  smile  Or even:

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 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.
   set stringData to theString's dataUsingEncoding:(current application's NSUTF16BigEndianStringEncoding)
   
   -- Write the BOM data to a new file using the NSData method. If that works, open the file for access with write permission, move the file pointer to the end of it, write the text data, and close the file again.    
   set theResult to false
   set newPath to my modifyPath:thePath adding:"-new"
   if ((BOMData's writeToFile:newPath atomically:true) as boolean) then
       set fileAccess to current application's NSFileHandle's fileHandleForWritingAtPath:newPath
       try
           tell fileAccess to seekToEndOfFile()
           tell fileAccess to writeData:stringData
           set theResult to true
       end try
       tell fileAccess to closeFile()
   end if
   
   return {newPath, theResult as boolean}
end decodeFile:

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


NG

Offline

 

#52 2017-03-16 12:15:23 pm

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

Re: Change file encoding of TextEdit by Applescript

Thanks Nigel

It seems that now I have every infos needed to drop old fashioned cote for writing datas smile


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

Offline

 

#53 2017-03-19 06:31:22 am

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

Re: Change file encoding of TextEdit by Applescript

By the way, this version of the modifyPath handler uses fewer methods and works properly for paths both with and without extensions:

Applescript:

on modifyPath:thePath adding:addString
   set pathString to current application's class "NSString"'s stringWithString:(thePath)
   set insertionPoint to pathString's rangeOfString:("(?=(\\.[^./]*+)?/*+$)") options:(current application's NSRegularExpressionSearch)
   set newPath to pathString's stringByReplacingCharactersInRange:(insertionPoint) withString:(addString)
   -- set newPath to newPath's stringByReplacingOccurrencesOfString:("/*+$") withString:("") options:(current application's NSRegularExpressionSearch) range:({0, newPath's |length|()})
   return newPath as text
end modifyPath:adding:

Edit: Regex modified to allow for the possibility of trailing slashes. (Thanks, Yvan.) And the commented-out line will remove any if it's re-enabled.

Last edited by Nigel Garvey (2017-03-19 11:40:52 am)


NG

Offline

 

#54 2017-03-19 10:39:45 am

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

Re: Change file encoding of TextEdit by Applescript

It appears - thank you Nigel - that El Capitan doesn't take care of the case : theExtension is empty string when it's asked to append it to a path so the original handler must be edited as :

Applescript:

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:

For safe I will edit my messages containing the old version.


Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) dimanche 19 mars 2017 16:39:39

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)