Converting PC/Unix Text File to Mac Text File

I have a “Easy” question…

I have a Database application that allows me to download an extract file. This file comes to me as ASCII text, tab delim. But it comes to me with Unix line control which AppleWorks 6.0 can’t understand.

Is there any easy script that could be generated that would allow me to drop the file onto, open the file in TextEdit, and convert the Unix Newline (Linefeed) or Windows Newline (Carriage Return/Line Feed) into a Mac Happy Newline (Carriage Return)…?

Or, could the file be converted into AppleWorks 6.0 somehow. AppleWorks will open the file, but ignores the Linefeed characters and runs everything into one long line.

I’m so green to Scripting that I don’t know where to start (but it seems like this should be doable)… Thanks…

If you save this as an application (in Script Editor), it will become a droplet. Please use duplicate files for testing since the original files will be overwritten. I have tested the script (minimal testing was done) and it appears to work quite nicely (converts the line endings) but some of our more talented scripters might have better solutions.

property unix_end : ASCII character 10
property mac_end : ASCII character 13
property dos_end : ((ASCII character 13) & (ASCII character 10))

on open dropped_items
	repeat with item_ in dropped_items
		if folder of (info for item_) is false then
			set content_ to (read item_)
			if content_ contains dos_end then
				set new_content to replace_chars(content_, dos_end, mac_end)
				write_to_file(new_content, item_, false)
			else if content_ contains unix_end then
				set new_content to replace_chars(content_, unix_end, mac_end)
				write_to_file(new_content, item_, false)
			end if
		end if
	end repeat
end open

to write_to_file(this_data, target_file, append_data)
	try
		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
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

Note: The sub-routines are included in Apple’s collection of Essential Sub-Routines

– Rob

Rob:

This seems to do the trick.

Thanks…!