I am trying to write some Russian text, or Cyrillic text, to a .txt file. I can successfully do so, but when I open the file all that is written in place of the text are a bunch of question marks. I was thinking it was an encoding problem but couldn't find anything in that area to help. I have written a little script that demonstrates the issue.
do shell script "> $HOME/Desktop/Russian\\ Text.txt"
set text_path to ((path to home folder) & "Desktop:Russian Text.txt" as string) as alias
set write_text to "Привет"
tell application "Finder"
write write_text to text_path
set read_text to text of (read text_path)
end tell
If anyone has any ideas as to why this is happening please let 开发者_如何学Pythonme know. Thank you.
I can't answer your question. You do have lots of applescript coding issues in your code but none of them are causing your problem. Applescript handles non-ascii text fine for me. I write in Danish some times and it works. However when I tried my script using Russian I got the same results as you. I can't explain why. Just so you can see the proper syntax for reading and writing a file here's my code. Note that I do not use the Finder to perform those tasks and also note how I set the path for the output file...
set outpath to (path to desktop as text) & "danish.txt"
set theText to "primær"
-- write the file
set openFile to open for access file outpath with write permission
write theText to openFile
close access openFile
-- read the file
set readText to read file outpath
UPDATE: I found an answer to your problem. It seems that if you write the utf-16 byte order mark (BOM) to the file then it works properly for Russian. As such I made two handlers so you can read and write these files...
set filePath to (path to desktop as text) & "russian.txt"
set theText to "Привет"
write_UnicodeWithBOM(filePath, theText, true)
read_UnicodeWithBOM(filePath)
on write_UnicodeWithBOM(filePath, theText)
try
set openFile to open for access file (filePath as text) with write permission
write (ASCII character 254) & (ASCII character 255) to openFile starting at 0
write theText to openFile starting at eof as Unicode text
end try
try
close access openFile
end try
end write_UnicodeWithBOM
on read_UnicodeWithBOM(filePath)
read file (filePath as text) as Unicode text
end read_UnicodeWithBOM
精彩评论