Missing Text from .txt to .rtf

This is not really a question on how to write a script but what happens to some data written from a script.
I enjoy watching NASCAR Racing and I wrote a script to record some data during the race.
I use “open for access” to get the data in a text file, everything works good but after words I put this data in a Rich text document with other data that I created.

Problem is after saving the .rtf file and then reopen it, most of the data from the text file is missing.
Some of the data like the header at top of page is there but everything that contains numbers is now gone. Was fine before saving and closing but after reopening the data is gone.

It doesn’t matter if I copy and paste the data in or use a script to put it in.

I’ve been searching the web for problem with TextEdit on this but have not found anything.
Seems to be cause of the way the .txt file was created using “open for access”.

I was hoping someone here may have seen this before.

I am running OS 10.4.11 and tried it on another Mac with same results.

Thanks
darock

After some more testing I found that only .txt files created using applescript using “Open for Access” and paragraphs are just numbers fail

I open a .txt file created with AS convert to Rich Text save>close>and reopen all text that had paragraphs that are numbers are now gone.

If I manually create a text file save and convert to Rich Text then save>close> and reopen its all there.

Seems like a bug in TextEdit or something but only on files created with AS

darock

I’m really puzzled.

I do everyday what you describe with no problem.

Here is a short demo.


set destFolder to path to desktop as text
set file_name to "darock.txt"
set filePath to destFolder & file_name
set theValues to {123.45, 234.56, 345.67, 456.78, 567.89}
repeat with aValue in theValues
	
	my writeto(filePath, (aValue as text) & return, text, true)
end repeat

tell application "TextEdit"
	open file filePath
end tell

--=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
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
		set openFile to open for access file targetFile with write permission
		if not apendData 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

--=====

I hope that it will help you to discover what fails in your own script.

Yvan KOENIG (VALLAURIS, France) mardi 6 mars 2012 09:47:04

Thanks Yvan for all the help in the Past.

It may have something to do with getting the data from the Web.
I wrote a Script like yours and it also worked

I’ll post my script for you to try.
Open up the Text document then convert to Rich Text Save and then Reopen and see if it works for you.


--  Save as stay open Application 

set S to (current date)
property AddTitle : 0
set dlyTime to 1 --  3
--   on run
global dlyTime
global LapByLap
global Place
global Speed
global Yellow
global One
global DaleJr
global Jr
global RaceLap
global SaveFolder
--     global T1 -- Testing only 
--     global Ttl -- Testing only 
set dlyTime to 3
--   set SaveFolder to "KINGSTON:Darryl Home:Scripts :NASCAR:Jr. PitStops:" --   & "Test"
set SaveFolder to (path to desktop as text) --   & "Test"
(*
---------------------- START JIGGLER ----------------------
tell application "System Events"
	set AllApps to name of every process
	if AllApps contains "Jiggler" then
		say "Jiggle"
	else
		try
			say "Lets Jiggle"
			tell application "Jiggler" to launch
		on error
			tell application "Macintosh HD:Applications:Jiggler.app" to launch
		end try
	end if
end tell
*)
------------------START WITH NEW DOCUMENT ------------------
set RaceDate to "Jr.LapLog " & (do shell script "date '+%Y-%m-%d'" as text)
set LapLog to SaveFolder & RaceDate & ".txt" as file specification
set daText to "TIME " & tab & "LAP " & tab & "POSITION " & tab & "SPEED " & tab & tab & "CAUTIONS " & return
try
	open for access LapLog with write permission
	set newText to daText
	--  set eof of LapLog to 0
	write newText to LapLog starting at eof
	close access LapLog
on error theError number theNumber
	beep
	try
		close access LapLog
	end try
	display dialog (theError & return & theNumber)
end try
--------------------- END NEW DOCUMENT ---------------------
--------------------------------------------------------------------------------
------------------ OPEN LEADERBOARD IF NOT ------------------
set LeaderBrd to "Lap-by-lap coverage of NASCAR Sprint Cup Series races - NASCAR.COM Live Leaderboard"
set myURL to "http://www.nascar.com/races/leaderboard/cup/race/"
tell application "Safari"
	try
		if window 1 exists then
			
			set numWin to count windows of application "Safari"
			set allWindow to name of every window
			if allWindow contains LeaderBrd then
				--		say ",,  Leader Board is running"
				set allText to text of document LeaderBrd
			else
				say ",,,  No Leader Board"
				make new document
				set URL of document 1 to myURL
				my page_loaded()
				set allText to text of document LeaderBrd
				
			end if
		else -- No windows exist 
			say ",,,  No Windows in Safari!  "
			make new document
			set URL of document 1 to myURL
			my page_loaded()
			set allText to text of document LeaderBrd
			
		end if
	on error
		make new document
		set URL of document 1 to myURL
		delay 3
		my page_loaded()
		set allText to text of document LeaderBrd
	end try
end tell --Safari
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

on idle
	delay 5
	Pos() --  DO THE STUFF 
	return (60 * dlyTime)
end idle

--   my Pos() -- USE FOR TESTING REMOVE WHEN RUNNING AS APP
----------------------- DO THE STUFF -----------------------	
--	
on Pos()
	set Place to " ?? "
	set Jr to " No Jr. "
	--	set S to (current date)
	
	set LeaderBrd to "Lap-by-lap coverage of NASCAR Sprint Cup Series races - NASCAR.COM Live Leaderboard"
	tell application "Safari"
		set allText to text of document LeaderBrd
	end tell
	
	set LeaderBrd to "Lap-by-lap coverage of NASCAR Sprint Cup Series races - NASCAR.COM Live Leaderboard"
	tell application "Safari"
		set allText to text of document LeaderBrd
	end tell
	try
		set ASTID to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "Flag"
		set RaceLap to paragraph 4 of text item 2 of allText
		set AppleScript's text item delimiters to "Cautions/Laps:"
		set Y2 to text item 2 of allText
		set Yellow to paragraph 2 of Y2
		set AppleScript's text item delimiters to "MORE"
		set One to paragraph 4 of text item 2 of allText
		set AppleScript's text item delimiters to "Dale Earnhardt Jr."
		set T1 to text item 1 of allText
		set Place to paragraph -3 of T1
		set JrLap to paragraph 2 of text item 2 of allText
		set Speed to paragraph 3 of text item 2 of allText
		set DaleJr to "Place: " & Place & tab & " Lap: " & JrLap & tab & " Speed: " & Speed
		set AppleScript's text item delimiters to ASTID
	on error daError
		say ",,,  Oops!!"
		set AppleScript's text item delimiters to ASTID
		display dialog daError
	end try
	say ",,, Running in " & Place & "th"
	--  end Pos
	--   display dialog "Script Run Time: " & dif & return & return & "Lap: " & Laps & return & "Position: " & Place as text giving up after 3
	---------------------- LOG LAP & PLACE ----------------------
	set RaceDate to "Jr.LapLog " & (do shell script "date '+%Y-%m-%d'" as text)
	
	set LapLog to SaveFolder & RaceDate & ".txt" as file specification
	
	set theTime to (do shell script "date +%R")
	set daTitle to "TIME " & tab & "LAP " & tab & "POSITION " & tab & "SPEED " & tab & tab & "CAUTIONS " & return
	set daText to (theTime & tab & RaceLap & tab & Place & tab & tab & Speed & tab & tab & Yellow & return)
	
	try
		if AddTitle ≤ 15 then
			open for access LapLog with write permission
			set newText to daText
			--  set eof of LapLog to 0
			write newText to LapLog starting at eof
			close access LapLog
			set AddTitle to (AddTitle + 1)
		else
			open for access LapLog with write permission
			set newText to daTitle & daText
			--  set eof of LapLog to 0
			write newText to LapLog starting at eof
			close access LapLog
			set AddTitle to 0
		end if
	on error theError number theNumber
		beep
		try
			close access LapLog
		end try
		display dialog (theError & return & theNumber)
	end try
	(*
set E to current date
set Ttl to (E - S)
*)
	beep
end Pos

--------------------------- QUIT ---------------------------
on quit
	display dialog "Has the Race Ended?? " buttons {"Yes", "No"} default button "No" with icon stop
	if button returned of the result is "Yes" then
		
		set LeaderBrd to "Lap-by-lap coverage of NASCAR Sprint Cup Series races - NASCAR.COM Live Leaderboard"
		tell application "Safari"
			set allText to text of document LeaderBrd
		end tell
		-------- GET TOTAL LEADERS --------
		set Cnt to (count of paragraphs of allText)
		try
			repeat with k from 1 to Cnt
				if paragraph k of allText is "Leaders:" then
					tell allText
						set Leaders to ("Leaders: " & tab & (paragraph (k + 1)))
					end tell
				end if
			end repeat
		end try
		-------- GET TOTAL LEAD CHANGES --------
		set Cnt to (count of paragraphs of allText)
		try
			repeat with k from 1 to Cnt
				if paragraph k of allText is "Lead Changes:" then
					tell allText
						set LeadChng to ("Lead Changes: " & tab & (paragraph (k + 1)))
					end tell
				end if
			end repeat
		end try
		-----------------------------------------------
		-------- Log Leaders & Lead Changes -------- 
		set RaceDate to "Jr.LapLog " & (do shell script "date '+%Y-%m-%d'" as text)
		
		--		set LapLog to "KINGSTON:Darryl Home:Scripts :NASCAR:Jr. PitStops:" & RaceDate & ".txt" as file specification
		set LapLog to (path to desktop as text) & RaceDate & ".txt" as file specification
		
		try
			set daText to Leaders & return & LeadChng
			open for access LapLog with write permission
			set newText to daText
			--  set eof of LapLog to 0
			write newText to LapLog starting at eof
			close access LapLog
			set AddTitle to 0
			
		on error theError number theNumber
			beep
			try
				close access LapLog
			end try
			display dialog (theError & return & theNumber)
		end try
		
		beep
		
		
		(*
		tell application "Jiggler" to quit
		say "stop Jiggling"
		continue quit
*)
	else
		continue quit
	end if
end quit

------------------------ PAGE LOADED ------------------------
on page_loaded()
	delay 2 -- Wait for page to start to load
	set wait_flag to ""
	tell application "Safari"
		repeat until wait_flag is "complete"
			set wait_flag to do JavaScript "document.readyState" in document 1
		end repeat
	end tell
end page_loaded

Thanks darock

I apologize but your script fails immediately.

tell current application
current date
→ date “jeudi 8 mars 2012 08:57:06”
path to desktop as text
→ “Macintosh HD:Users:yvankoenig:Desktop:”
do shell script “date ‘+%Y-%m-%d’” as text
→ “2012-03-08”
Résultat :
error “Il est impossible de rendre "Macintosh HD:Users:yvankoenig:Desktop:Jr.LapLog 2012-03-08.txt" en type file specification.” number -1700 from “Macintosh HD:Users:yvankoenig:Desktop:Jr.LapLog 2012-03-08.txt” to file specification

Yvan KOENIG (VALLAURIS, France) jeudi 8 mars 2012 08:59:10

Salu Yvan,

the write-to-file routine might work, but it’s actually wrong syntax and the specifier file specification is outdated


.
set RaceDate to "Jr.LapLog " & (do shell script "date '+%Y-%m-%d'")
set LapLog to SaveFolder & RaceDate & ".txt"
set daText to "TIME " & tab & "LAP " & tab & "POSITION " & tab & "SPEED " & tab & tab & "CAUTIONS " & return
try
	set fileRef to open for access file LapLog with write permission
	-- set eof of LapLog to 0
	write daText to fileRef starting at eof
	close access fileRef
on error theError number theNumber
	beep
	try
		close access file LapLog
	end try
	display dialog (theError & return & theNumber)
end try
.

I don’t understand were is the problem.

I’m working under 10.7.3.
If I open the text file and change the format to rtf, the operating system saves the document as Jr.LapLog 2012-03-08.rtf and delete the original Jr.LapLog 2012-03-08.txt

At nex pass, the script doesn’t find the text file so it creates a new one and write the new set of datas in it.
It’s a perfectly logical behavior.

My guess is that the problem is not the script but the user which was unable to see that the text file was deleted when he changed the format.

The file Jr.LapLog 2012-03-08.rtf contain:

TIME LAP POSITION SPEED CAUTIONS
10:08 02:54:46 312 130.138 27.66 19.996 1 79 [+] 15 7

The file Jr.LapLog 2012-03-08.txt contain :

10:11 02:54:46 312 130.138 27.66 19.996 1 79 [+] 15 7
10:15 02:54:46 312 130.138 27.66 19.996 1 79 [+] 15 7

As far as I know it’s perfect.

I ran the script modified according to Stefan message.



--  Save as stay open Application 

set S to (current date)
property AddTitle : 0
set dlyTime to 1 --  3
--   on run
global dlyTime
global LapByLap
global Place
global Speed
global Yellow
global One
global DaleJr
global Jr
global RaceLap
global SaveFolder
--     global T1 -- Testing only 
--     global Ttl -- Testing only 
set dlyTime to 3
--   set SaveFolder to "KINGSTON:Darryl Home:Scripts :NASCAR:Jr. PitStops:" --   & "Test"
set SaveFolder to (path to desktop as text) --   & "Test"
(*
---------------------- START JIGGLER ----------------------
tell application "System Events"
	set AllApps to name of every process
	if AllApps contains "Jiggler" then
		say "Jiggle"
	else
		try
			say "Lets Jiggle"
			tell application "Jiggler" to launch
		on error
			tell application "Macintosh HD:Applications:Jiggler.app" to launch
		end try
	end if
end tell
*)
------------------START WITH NEW DOCUMENT ------------------
set RaceDate to "Jr.LapLog " & (do shell script "date '+%Y-%m-%d'" as text)
set LapLog to SaveFolder & RaceDate & ".txt" --as file specification
set daText to "TIME " & tab & "LAP " & tab & "POSITION " & tab & "SPEED " & tab & tab & "CAUTIONS " & return
try
	set fileRef to open for access file LapLog with write permission
	-- set eof of LapLog to 0
	write daText to fileRef starting at eof
	close access fileRef
on error theError number theNumber
	beep
	try
		close access file LapLog
	end try
	display dialog (theError & return & theNumber)
end try
--------------------- END NEW DOCUMENT ---------------------
--------------------------------------------------------------------------------
------------------ OPEN LEADERBOARD IF NOT ------------------
set LeaderBrd to "Lap-by-lap coverage of NASCAR Sprint Cup Series races - NASCAR.COM Live Leaderboard"
set myURL to "http://www.nascar.com/races/leaderboard/cup/race/"
tell application "Safari"
	try
		if window 1 exists then
			
			set numWin to count windows of application "Safari"
			set allWindow to name of every window
			if allWindow contains LeaderBrd then
				--		say ",,  Leader Board is running"
				set allText to text of document LeaderBrd
			else
				say ",,,  No Leader Board"
				make new document
				set URL of document 1 to myURL
				my page_loaded()
				set allText to text of document LeaderBrd
				
			end if
		else -- No windows exist 
			say ",,,  No Windows in Safari!  "
			make new document
			set URL of document 1 to myURL
			my page_loaded()
			set allText to text of document LeaderBrd
			
		end if
	on error
		make new document
		set URL of document 1 to myURL
		delay 3
		my page_loaded()
		set allText to text of document LeaderBrd
	end try
end tell --Safari
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

on idle
	delay 5
	Pos() --  DO THE STUFF 
	return (60 * dlyTime)
end idle

--   my Pos() -- USE FOR TESTING REMOVE WHEN RUNNING AS APP
----------------------- DO THE STUFF -----------------------	
--	
on Pos()
	set Place to " ?? "
	set Jr to " No Jr. "
	--	set S to (current date)
	
	set LeaderBrd to "Lap-by-lap coverage of NASCAR Sprint Cup Series races - NASCAR.COM Live Leaderboard"
	tell application "Safari"
		set allText to text of document LeaderBrd
	end tell
	
	set LeaderBrd to "Lap-by-lap coverage of NASCAR Sprint Cup Series races - NASCAR.COM Live Leaderboard"
	tell application "Safari"
		set allText to text of document LeaderBrd
	end tell
	try
		set ASTID to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "Flag"
		set RaceLap to paragraph 4 of text item 2 of allText
		set AppleScript's text item delimiters to "Cautions/Laps:"
		set Y2 to text item 2 of allText
		set Yellow to paragraph 2 of Y2
		set AppleScript's text item delimiters to "MORE"
		set One to paragraph 4 of text item 2 of allText
		set AppleScript's text item delimiters to "Dale Earnhardt Jr."
		set T1 to text item 1 of allText
		set Place to paragraph -3 of T1
		set JrLap to paragraph 2 of text item 2 of allText
		set Speed to paragraph 3 of text item 2 of allText
		set DaleJr to "Place: " & Place & tab & " Lap: " & JrLap & tab & " Speed: " & Speed
		set AppleScript's text item delimiters to ASTID
	on error daError
		say ",,,  Oops!!"
		set AppleScript's text item delimiters to ASTID
		display dialog daError
	end try
	say ",,, Running in " & Place & "th"
	--  end Pos
	--   display dialog "Script Run Time: " & dif & return & return & "Lap: " & Laps & return & "Position: " & Place as text giving up after 3
	---------------------- LOG LAP & PLACE ----------------------
	set RaceDate to "Jr.LapLog " & (do shell script "date '+%Y-%m-%d'" as text)
	
	set LapLog to SaveFolder & RaceDate & ".txt" --as file specification
	
	set theTime to (do shell script "date +%R")
	set daTitle to "TIME " & tab & "LAP " & tab & "POSITION " & tab & "SPEED " & tab & tab & "CAUTIONS " & return
	set daText to (theTime & tab & RaceLap & tab & Place & tab & tab & Speed & tab & tab & Yellow & return)
	
	try
		if AddTitle ≤ 15 then
			
			set fileRef to open for access file LapLog with write permission
			-- set eof of LapLog to 0
			write daText to fileRef starting at eof
			close access fileRef
			set AddTitle to (AddTitle + 1)
			
		else
			set fileRef to open for access file LapLog with write permission
			-- set eof of LapLog to 0
			write daText to fileRef starting at eof
			close access fileRef
			set AddTitle to 0
		end if
	on error theError number theNumber
		beep
		try
			close access file LapLog
		end try
		display dialog (theError & return & theNumber)
	end try
	(*
set E to current date
set Ttl to (E - S)
*)
	beep
end Pos

--------------------------- QUIT ---------------------------
on quit
	display dialog "Has the Race Ended?? " buttons {"Yes", "No"} default button "No" with icon stop
	if button returned of the result is "Yes" then
		
		set LeaderBrd to "Lap-by-lap coverage of NASCAR Sprint Cup Series races - NASCAR.COM Live Leaderboard"
		tell application "Safari"
			set allText to text of document LeaderBrd
		end tell
		-------- GET TOTAL LEADERS --------
		set Cnt to (count of paragraphs of allText)
		try
			repeat with k from 1 to Cnt
				if paragraph k of allText is "Leaders:" then
					tell allText
						set Leaders to ("Leaders: " & tab & (paragraph (k + 1)))
					end tell
				end if
			end repeat
		end try
		-------- GET TOTAL LEAD CHANGES --------
		set Cnt to (count of paragraphs of allText)
		try
			repeat with k from 1 to Cnt
				if paragraph k of allText is "Lead Changes:" then
					tell allText
						set LeadChng to ("Lead Changes: " & tab & (paragraph (k + 1)))
					end tell
				end if
			end repeat
		end try
		-----------------------------------------------
		-------- Log Leaders & Lead Changes -------- 
		set RaceDate to "Jr.LapLog " & (do shell script "date '+%Y-%m-%d'" as text)
		
		--		set LapLog to "KINGSTON:Darryl Home:Scripts :NASCAR:Jr. PitStops:" & RaceDate & ".txt" as file specification
		set LapLog to (path to desktop as text) & RaceDate & ".txt" as file specification
		
		try
			set daText to Leaders & return & LeadChng
			open for access LapLog with write permission
			set newText to daText
			--  set eof of LapLog to 0
			write newText to LapLog starting at eof
			close access LapLog
			set AddTitle to 0
			
		on error theError number theNumber
			beep
			try
				close access LapLog
			end try
			display dialog (theError & return & theNumber)
		end try
		
		beep
		
		
		(*
		tell application "Jiggler" to quit
		say "stop Jiggling"
		continue quit
*)
	else
		continue quit
	end if
end quit

------------------------ PAGE LOADED ------------------------
on page_loaded()
	delay 2 -- Wait for page to start to load
	set wait_flag to ""
	tell application "Safari"
		repeat until wait_flag is "complete"
			set wait_flag to do JavaScript "document.readyState" in document 1
		end repeat
	end tell
end page_loaded

but I would edit the Pos handler this way :


on Pos()
	set Place to " ?? "
	set Jr to " No Jr. "
	--	set S to (current date)
	
	set LeaderBrd to "Lap-by-lap coverage of NASCAR Sprint Cup Series races - NASCAR.COM Live Leaderboard"
	tell application "Safari"
		set allText to text of document LeaderBrd
	end tell
	
	set LeaderBrd to "Lap-by-lap coverage of NASCAR Sprint Cup Series races - NASCAR.COM Live Leaderboard"
	tell application "Safari"
		set allText to text of document LeaderBrd
	end tell
	try
		set ASTID to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "Flag"
		set RaceLap to paragraph 4 of text item 2 of allText
		set AppleScript's text item delimiters to "Cautions/Laps:"
		set Y2 to text item 2 of allText
		set Yellow to paragraph 2 of Y2
		set AppleScript's text item delimiters to "MORE"
		set One to paragraph 4 of text item 2 of allText
		set AppleScript's text item delimiters to "Dale Earnhardt Jr."
		set T1 to text item 1 of allText
		set Place to paragraph -3 of T1
		set JrLap to paragraph 2 of text item 2 of allText
		set Speed to paragraph 3 of text item 2 of allText
		set DaleJr to "Place: " & Place & tab & " Lap: " & JrLap & tab & " Speed: " & Speed
		set AppleScript's text item delimiters to ASTID
	on error daError
		say ",,,  Oops!!"
		set AppleScript's text item delimiters to ASTID
		display dialog daError
	end try
	say ",,, Running in " & Place & "th"
	--  end Pos
	--   display dialog "Script Run Time: " & dif & return & return & "Lap: " & Laps & return & "Position: " & Place as text giving up after 3
	---------------------- LOG LAP & PLACE ----------------------
	set RaceDate to "Jr.LapLog " & (do shell script "date '+%Y-%m-%d'" as text)
	
	set LapLog to SaveFolder & RaceDate & ".txt" --as file specification
	
	set theTime to (do shell script "date +%R")
	set daTitle to "TIME " & tab & "LAP " & tab & "POSITION " & tab & "SPEED " & tab & tab & "CAUTIONS " & return
	set daText to (theTime & tab & RaceLap & tab & Place & tab & tab & Speed & tab & tab & Yellow & return)
	
	try
		set fileRef to open for access file LapLog with write permission
		-- set eof of LapLog to 0
		write daText to fileRef starting at eof
		close access fileRef
		if AddTitle ≤ 15 then
			set AddTitle to (AddTitle + 1)
		else
			set AddTitle to 0
		end if
	on error theError number theNumber
		beep
		try
			close access file LapLog
		end try
		display dialog (theError & return & theNumber)
	end try
	(*
set E to current date
set Ttl to (E - S)
*)
	beep
end Pos

Yvan KOENIG (VALLAURIS, France) jeudi 8 mars 2012 10:22:10