Emtpy characters when writing to txt file. (New to AppleScript)

Hi.

I’m new to AppleScript and these forums.

A project of mine requires saving an image attachment from an email then saving the attachment name and subject to a text file.

The data saved to the text file would be written as “Photo01.jpg::My Vacation”. There seems to be, however, empty characters between each letter. I have to press the arrow key twice to move from one letter to another. This is presenting an issue as the data is then read by a PHP script and cannot be accurately interpreted.

Any help or explanation would be greatly appreciated!

The script I have goes as follows:


using terms from application "Mail"
	on perform mail action with messages theMessages
		tell application "Mail"
			set theMessage to last item of theMessages
			if theMessage's mail attachments is not {} then
				set theSubject to subject of theMessage
				set theAttachment to first item of theMessage's mail attachments
				set theFileName to "HD:path:path:" & theAttachment's name
				try
					save theAttachment in theFileName
					set the_description_file to "HD:path:path:description.txt"
					open for access the_description_file with write permission
					write theAttachment's name & "::" & theSubject to file the_description_file starting at eof
					close access file the_description_file
				on error errnum
				end try
			end if
		end tell
	end perform mail action with messages
end using terms from

I didn’t sort out what you should do about it, but the problem is that you are looking at Unicode text, so instead of one byte per letter it’s two, the first one unprintable.

One technique (due to Nigel Garvey and Arthur Knapp) is this:


set {text:plainText} to (textToConvert) as text
-- plainText is just ASCII

Whoo. After posting I had figured out the solution is to convert the text to ASCII, just didn’t know how until your response.

Thanks, Adam!

Glad it helped. Bear in mind, however, that if the text includes anything outside the ASCII character set (a letter with accent markings or a letter from the Greek alphabet, for examples), it’ll show up as a little blank box.

I believe this is because Unicode text (I’m assuming that’s what Mail uses) is UTF-16. (I think. UTF-16 uses two bytes for every character; Some “lower” range characters have zeroes as there first byte value. When something isn’t expecting UTF-16 behavior, those zeroes are represented as null bytes [aka ASCII character 0].)

Try using UTF-8 instead:

write theAttachment's name & "::" & theSubject to file the_description_file starting at eof as «class utf8»