This should be an easy thing I think, but I can’t get it to work. All I want to do is open a text file as one encoding (Windows Latin 1) and save it as another (Mac Roman). I found this code:
tell application "TextEdit"
set myPath to (path to desktop as string) & "junk.txt"
-- save document 1 as "window-1252" in file "test.txt"
set fut to "Western (Windows Latin 1)" as Unicode text
save document 1 as fut in file myPath
end tell
in this http://bbs.applescript.net/viewtopic.php?id=9797 post but it does’t seem to actually save in the format specified.
Any help would be greatly appreciated.
Hi,
save as specifies the file type like TEXT or RTF, not the text encoding.
You can use the iconv shell command to do this without textedit
set myPath to (path to desktop as Unicode text) & "junk.txt"
set d to (do shell script "iconv -f CP1252 -t MacRoman " & quoted form of POSIX path of myPath)
set ff to open for access file myPath with write permission
write d to ff
close access ff
Note: the shell script throws an error, if there are characters in the CP1252 text, which have no equivalent in MacRoman
Thanks very much, this is fanfrickintastic, and does exactly what I wanted.
PS: still shorter 
set myPath to (path to desktop as Unicode text) & "junk.txt"
do shell script "/usr/bin/textutil -convert txt -encoding macintosh -inputencoding CP1252 " & quoted form of POSIX path of myPath
As I previously stated this works great, but… After I convert the file I am trying to read it into a variable with a tab delimiter, and this seems to work sporadically at best. Anyone know of any problems with reading a converted .tab file?
here is the code line to read the file
set fileSize to get eof expFile
set dataChunk to read expFile as {text} using delimiter {tab} from 1 for fileSize
I then set a variable to one of the items
set theType to item 2 of dataChunk as text -- let's say it's Sample Text
If I do a display dialog it shows me that this is set, but then when I try to act on theType it isn’t reading the variable correctly.
if theType contains "Sample Text" then
--do something
end if
but it doesn’t do anything
Thanks Very Much
Hi,
if you don’t need to write the text back to disk after converting,
you can use this
set myPath to (path to desktop as Unicode text) & "junk.txt"
set dataChunk to (do shell script "iconv -f CP1252 -t MacRoman " & quoted form of POSIX path of myPath)
set {TID, text item delimiters} to {text item delimiters, tab}
set theType to text item 2 of dataChunk
set text item delimiters to TID
if theType contains "Sample Text" then
--do something
end if
Note:
don’t use the braces in this line:
set dataChunk to read expFile as {text} using delimiter {tab} from 1 for fileSize
this does exactly the same:
set dataChunk to read expFile using delimiter tab