Can "write" create PC friendly text files?

I’m trying to create a script that automatically grabs our sales figures off of a larger report and emails them to my boss.

The script works, and the text files created are perfectly readable on my mac, but they lose all formating when sent to a PC.

Is there an easy way to create a text file with PC line breaks?

Thanks…

Windows PCs use carriage return-line feed to terminate paragraphs. (ASCII character 13 & ASCII character 10) so:

set pc to (ASCII character 13) & (ASCII character 10)
set myPara to "The quick brown fox" & return & "The moon is blue" & return

set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set p to text items of myPara
set AppleScript's text item delimiters to pc
set pcT to p as string
set AppleScript's text item delimiters to tid

pcT

NOTE: Just to make it interesting, UNIX uses \n (new line, or line feed - ascii 10) while Macs use \r (return or carriage return -ascii 13), so depending on how your text was produced, you might be replacing 10 by itself with 13 followed by 10.

Thanks Adam, I’ll try this… but I was hoping it would be as easy as “save as pc text”!
:slight_smile:

The original file was UNIX, but oddly enough it opens fine in both Mac and PC… the line breaks don’t get messed up until I use “write” to create a new text file with the data I need.

Most Apple Apps and certainly the Script Editor and Script Debugger “do it right” for NewLine, Return, and Return/NewLine. PC’s don’t.

In fact, to go a bit further, paragraphs are correctly interpreted for any of them:

set txtN to "first line" & (ASCII character 10) & "second line" & (ASCII character 10) & "third line"
set txtR to "first line" & (ASCII character 13) & "second line" & (ASCII character 13) & "third line"
set txtP to "first line" & ((ASCII character 13) & (ASCII character 10)) & "second line" & ((ASCII character 13) & (ASCII character 10)) & "third line"

set pN to paragraphs of txtN
set PR to paragraphs of txtR
set pP to paragraphs of txtP

repeat with aP in {pN, PR, pP}
	set text item delimiters to (ASCII character 13) & (ASCII character 10)
	set contents of aP to contents of aP as string
	set text item delimiters to ""
end repeat

The other thing that can be screwing you up, however, is that if Darwin created the text, it may be Unicode text, and the PC doesn’t know what to do with that.

Note that many “newer” things on OS X use new line (\n), but sometimes you’ll find carriage returns (like in Script Editor, which predates OS X).

Well, it is unicode text, because that’s what I thought it needed to be. Would saving as text instead of unicode text fix the problem?

Here’s a snippet of the script:

set writeme to (items 2 thru 5 of mylist) as Unicode text
my write_to_file(writeme, this_file, true)

on write_to_file(this_data, target_file, append_data)
	set the target_file to the target_file as text
	set the open_target_file to ¬
		open for access file target_file with write permission
	if append_data is false then ¬
		set eof of the open_target_file to 0
	write this_data to the open_target_file starting at eof
	close access the open_target_file
	return true
end write_to_file

And further, if your text is unicode text, then this script is a way to convert it to ASCII text (courtesy of Nigel Garvey).


set thisText to "Here is a sample of Unicode text" as Unicode text
set {text:plainText} to (text of thisText) as text
plainText

Notepad on Windows can handle Unicode. You might want to try using UTF-8 instead though (Unicode text is UTF-16).

rite this_data to the open_target_file as «class utf8» starting at eof

Thanks guys, I’ll try some of your suggestions…

It think it’s originally by Arthur Knapp. :slight_smile:

Could’ve sworn I mentioned that… :wink:

Apologies, Gents;

My records of attributions aren’t all they should be. :rolleyes:

A