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