I have this code (saved as an application, stay open)that captures the weight on a scale on a network. The scale displays the wait on any browser on any computer on my LAN once you type the URL as the IP address of the scale.
I use Curl and some filters to get the weight.
Before I used a “delay 5” on the end of the repeating statement. but later discovered that the “delay” statement would hog the CPU and stop the users from logging out of the computer. My application was not quitable.
Using “return 5” instead of “delay 5” made my application quitable, but gave me one problem: The application wouldn’t “remenber” the last “stable weight”.
So I added a function in my script where I am writing a text file (Temporario.tmp) on the Desktop. This way when the script reads the weight again, it compares the new weight with the old.
A Log with all the weights put on the scale is written also.
The script is functional now. The problem is that every time my script finds a new "stable weight’ on the scale’s server, it not only writes the value on "Temporario.tmp’, but also writes a copy of the file in the root of my hard disk. See the sreenshot. The names of these weird files are just aleatory numbers.
Can anyone help me with what might be happening?
Would you suggest me another technic of getting my script to remenber the last stable weight?
Thanks!
on idle
set the iDay to day of the (current date) as integer
if iDay < 10 then set iDay to "0" & iDay as text
set the iMonth to month of the (current date) as text
set FilePath2 to "Macintosh HD:Users:bernardo:Desktop:Temporario.tmp"
set TextoInicial to " "
set LastReading to ""
repeat while true
set the FilePath2 to open for access file FilePath2
set LastReading to read FilePath2
close access FilePath2
try
set feedback to do shell script "~/cleaner.script"
set StableWeight to paragraph 5 of feedback
-- this will return something like " 1234Kg" ( the weight on the scale in Kilograms)
on error
set StableWeight to "erro numero 1 balança desligada?"
end try
if StableWeight is not equal to LastReading and feedback contains "ESTVEL" then
set MyText to ("Em " & (current date) & " o peso mudou para..." & StableWeight & return)
set FilePathMyFile to (path to home folder) & "Library:Logs:BalancaToledo:LogBalanca_V_1.5_" & iDay & "_" & iMonth & ".log"
my write_to_file(MyText, FilePathMyFile)
my write_to_file2(StableWeight, FilePath2)
else
end if
return 5
end repeat
end idle
on write_to_file(this_data, target_file)
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
write this_data to the open_target_file starting at eof -- writes at end of file
close access the open_target_file
on error
try
close access file target_file
end try
end try
end write_to_file
on write_to_file2(this_data, target_file)
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
write this_data to the open_target_file starting at 0 -- writes at begining of file
close access the open_target_file
on error
try
close access file target_file
end try
end try
end write_to_file2