script that used to work fine now writes chinese, WTF?

EDIT: I posted this in the wrong forum and tried to save the post as text, the result turns out to be chinese too… I tried to restore all text edit functions to default, no change.

EDIT2: I tried to open the file mentioned in the edit above in word for mac and it chose western (mac os roman) encoding by default, so I set text edit to open and save in that encoding by default. This made this script work, but another one is still broken. I got a software update tonight, maybe that has something to do with it.

EDIT3: See my last post for what seems to have broken the script. Any comment on why it did would be appreciated.

This is the input/output:

Input: (in dialog)

qwerty

Output: (to txt)

兗䕒呙㨍ഊ

This is the script:

Open this Scriplet in your Editor:
-- initial variables
set templateFile to "Q:x:7:n7:GTD scripting:template folder:SI_Script.txt"
set copyFolder to "Q:Ø:" -- notice this path ends in ":" because it's a folder

-- get the new name
display dialog "new_name_dialogue" default answer ""
set newName to (text returned of result)
set newPath to copyFolder & newName

-- get the text of the template file
set templateText to read file templateFile

-- add the file name in CAPS, a colon, and 2 returns at the beginning of templateText
set capsName to upperCase(newName)
set newText to capsName & ":" & return & return & templateText

-- write the newText to newPath
writeTo(newPath, newText, text, false)

-- open the new file in textedit
tell application "TextEdit" to open file newPath
tell application "TextEdit"
   activate
end tell



(*============== SUBROUTINES ==============*)
on writeTo(targetFile, theData, dataType, apendData)
   -- targetFile is the path to the file you want to write
   -- theData is the data you want in the file.
   -- dataType is the data type of theData and it can be text, list, record etc.
   -- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
   try
       set targetFile to targetFile as text
       if targetFile does not contain ":" then set targetFile to POSIX file targetFile as text
       set openFile to open for access file targetFile with write permission
       if apendData is false then set eof of openFile to 0
       write theData to openFile starting at eof as dataType
       close access openFile
       return true
   on error
       try
           close access file targetFile
       end try
       return false
   end try
end writeTo

on upperCase(theText)
   set chrIDs to id of theText
   set a to {}
   repeat with i from 1 to (count of chrIDs)
       set chrID to item i of chrIDs
       if chrID ≥ 97 and chrID ≤ 122 then set chrID to (chrID - 32)
       set end of a to chrID
   end repeat
   return string id a
end upperCase

Model: macbook pro
AppleScript: NA
Browser: Safari 533.20.27
Operating System: Mac OS X (10.6)

Unintended Chinese characters usually indicate a misinterpretation of UTF-16 Unicode text data: either that little-endian data are being read as big-endian (or vice versa) or that non-UTF-16 data are being read as UTF-16.

Your script interprets the data in the template file as 8-bit ASCII text (probably Mac Roman), prepends the user input to the result, and writes everything back to the new file as 8-bit ASCII. For it to give the results you’re expecting, the template file must be in an 8-bit format like Mac Roman and the software which opens the new file must interpret it in the same form.

If the template file contains UTF-16 data, the script must read it ‘as Unicode text’. If the data begin with a suitable byte-order mark (BOM), they’ll be correctly interpreted whether they’re big-endian or little-endian. If there’s no BOM, that data will assumed to be big-endian. (This is for backwards compatibility with the Mac’s previous use of PowerPC processors.)

Similarly, if you want UTF-16 data in the new file, the script must write the data ‘as Unicode text’ and should write a big-endian BOM to the file first so that the reading software knows what to expect. (You can’t switch between little-endian and big-endian in the middle of text, so your writeTo() handler’s append function shouldn’t be used with UTF-16 text. The file should be read in its entirety and completely rewritten with a big-endian BOM and the edited text. However, that’s not relevant to you here.)

-- initial variables
set templateFile to "Q:x:7:n7:GTD scripting:template folder:SI_Script.txt"
set copyFolder to "Q:Ø:" -- notice this path ends in ":" because it's a folder

-- get the new name
display dialog "new_name_dialogue" default answer ""
set newName to (text returned of result)
set newPath to copyFolder & newName

-- get the text of the template file
set templateText to read file templateFile as Unicode text -- Assuming UTF-16 in the template file.

-- add the file name in CAPS, a colon, and 2 returns at the beginning of templateText
set capsName to uppercase(newName)
set newText to capsName & ":" & return & return & templateText

-- write the newText to newPath
writeTo(newPath, newText, Unicode text, false) -- NB. 'Unicode text' for UTF-16 output

-- open the new file in textedit
tell application "TextEdit" to open file newPath
tell application "TextEdit"
	activate
end tell



(*============== SUBROUTINES ==============*)
on writeTo(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as text
		if targetFile does not contain ":" then set targetFile to POSIX file targetFile as text
		set openFile to open for access file targetFile with write permission
		if apendData is false then
			set eof openFile to 0
			if (dataType is Unicode text) then write «data rdatFEFF» to openFile -- Big-endian UTF-16 BOM.
		end if
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end writeTo

on uppercase(theText)
	set chrIDs to id of theText
	set a to {}
	repeat with i from 1 to (count of chrIDs)
		set chrID to item i of chrIDs
		if chrID ≥ 97 and chrID ≤ 122 then set chrID to (chrID - 32)
		set end of a to chrID
	end repeat
	return string id a
end uppercase

EDIT: Dj BW (see below) is certainly right that I’m not being very clear. This is the error message I got:

The section of the applescript quoted below shows the part that broke my applescript [see the brackets].

set newText to (capsName & ":" & return & return & templateText & return & return & "URL: " & theURL & return & "Original title: " & theTitle & return & "Download date: " & (current date) as text) & "Path of folder: " & (pathOfNewFolder as text) -- [this is what broke the script] & "Path of folder: " & (pathOfNewFolder as text)

For explanation of what exactly happened, see the other comments.

the entire text of the now functioning script

--IF THIS SCRIPT STOPS WORKING THE PROBLEM MIGHT BE THE SUBROUTINE FOR WRITING TEXT: see this link; http://macscripter.net/viewtopic.php?id=37231
--this script will be launched by a hotkey
tell application "Safari" to set {theSource, theTitle, theURL} to {source, name, URL} of document 1

tell application "Finder"
	display dialog "newName_dialogue" default answer theTitle
	set newName to (text returned of result)
	set selectedFinderItem to (folder of the front window) as text
	make new folder at selectedFinderItem with properties {name:newName}
	set pathOfNewFolder to selectedFinderItem & newName as text
	duplicate file "Q:x:7:n7:GTD scripting:template folder:x.txt" to pathOfNewFolder as string
	set pathOfX to pathOfNewFolder & ":x.txt" as string
	set name of file pathOfX to (newName as text) & ".txt"
	set pathOfNewTXT to pathOfNewFolder & ":" & newName & ".txt"
	--	"display dialog pathOfNewTXT" was useful as a test, it made it clear you forgot to add ":"
	make new folder at pathOfNewFolder with properties {name:"(MCOTWS); " & newName}
	--MCOTWS=more copies of the website
	set mCOTWSFolder to pathOfNewFolder & "(MCOTWS); " & newName & ":"
	
end tell
tell application "Safari"
	activate
	delay 0.2
	--Get posix path of target file
	set targetFile to pathOfNewFolder
	set pOSIXPathOfTargetFile to POSIX path of targetFile
	--display dialog pOSIXPathOfTargetFile
	
	--Tell activated safari to find and "click save" & Tell safari to activate
	tell application "Safari"
		activate
		delay 0.1
		tell application "System Events"
			tell process "Safari"
				--this will only save stuff in one place
				click menu item "Save As." of menu 1 of menu bar item "File" of menu bar 1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke (ASCII character 31) --> down arrow
				delay 0.1
				keystroke "pa"
				delay 0.1
				keystroke return
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke pOSIXPathOfTargetFile
				key down return
				key up return
				delay 0.2
				key down return
				key up return
			end tell
			tell process "Safari"
				--this will only save stuff in one place
				click menu item "Save As." of menu 1 of menu bar item "File" of menu bar 1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke (ASCII character 31) --> down arrow
				delay 0.1
				keystroke "we"
				delay 0.1
				keystroke return
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke tab
				delay 0.1
				keystroke pOSIXPathOfTargetFile
				key down return
				key up return
				delay 0.2
				key down return
				key up return
			end tell
		end tell
	end tell
	
	--I'm going to put some stuff that gets the old thing saved
	--pathOfNewFolder, i'll have to get the posix of this ... 
	--do stefan ks script
end tell
--I'll also make a new folder in which I put

set templateFile to "Q:x:7:n7:GTD scripting:template folder:SI_Script.txt"
set copyFolder to "Q:X:7:SI:SIAG1:" -- notice this path ends in ":" because it's a folder

--something to write safari docs to the folder and a subfolder
--something to write url, and timestamp to the txt

-- get the text of the template file
set templateText to read file templateFile

-- add the file name in CAPS, a colon, and 2 returns at the beginning of templateText
set capsName to upperCase(newName)
set newText to (capsName & ":" & return & return & templateText & return & return & "URL: " & theURL & return & "Original title: " & theTitle & return & "Download date: " & (current date) as text) -- [this has broken the script before, see http://macscripter.net/post.php?action=post&tid=37231] & "Path of folder: " & (pathOfNewFolder as text)


-- write the newText to pathOfNewTXT
writeTo(pathOfNewTXT, newText, text, false)

-- open the new file in textedit
tell application "TextEdit" to open file pathOfNewTXT
tell application "TextEdit" to activate
delay 0.2
tell application "System Events" to keystroke (ASCII character 31) --> down arrow
tell application "System Events" to keystroke (ASCII character 31) --> down arrow



(*============== SUBROUTINES (BEG) ==============*)
on writeTo(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as text
		if targetFile does not contain ":" then set targetFile to POSIX file targetFile as text
		set openFile to open for access file targetFile with write permission
		if apendData is false then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end writeTo

on upperCase(theText)
	set chrIDs to id of theText
	set a to {}
	repeat with i from 1 to (count of chrIDs)
		set chrID to item i of chrIDs
		if chrID ≥ 97 and chrID ≤ 122 then set chrID to (chrID - 32)
		set end of a to chrID
	end repeat
	return string id a
end upperCase
(*============== SUBROUTINES (END) ==============*)

May I suggest that you edit only your last post. It’s quite confusing how the status is.

First thing to say is: “Welcome in the world of character encodings”. An character encoding is a table who translate a byte (or multiple bytes) into a single character. So when you write a file with another table than you open it with, the byte(s) can be translated wrong to a wrong character. So in your Edit2 you wrote the data as utf-16 (characters translated to bytes with the utf-16 table) and opened it with MacRoman (bytes translated to characters with the MacRoman characters). I’ll hope you see what went wrong in your case.

Well knowing this and reading Nigel’s post again It would be clear what went wrong.