I’ve wrote this little script to write information into a sequential file, information to be written in the file CustomerOrder are based on user entry through Display Dialog boxes.
New paragraphs must be added the file using the value provided by the user for items CustomerId and InvoiceNo.
Paragraphs are supposed to be entered always starting at the end of the file.
At this moment the file CustomerOrder.TXT file is not being filled correctly. Would someone have an idea why?
Thanks in advance!
Daniel
set theOrder to (path to home folder as text) & "CustomerOrder.TXT"
set CustomerID to ""
set InvoiceNo to ""
tell application "Finder"
repeat while CustomerID is not equal to "End"
set {text returned:CustomerID, button returned:buttonReturned} to display dialog "Please enter Customer ID number :" buttons {"OK"} default button 1 default answer CustomerID
if CustomerID is equal to "End" then
exit repeat
end if
set {text returned:InvoiceNo, button returned:buttonReturned} to display dialog "Please enter Invoice No. :" buttons {"OK"} default button 1 default answer InvoiceNo
if not (exists file theOrder) then
set fileRef to open for access theOrder with write permission
else
set fileRef to open for access theOrder with write permission
set eof of fileRef to 0
end if
write (return & InvoiceNo & " " & CustomerID) as string to fileRef
close access fileRef
end repeat
end tell
the default behavior of the write command is starting at 0,
if you want to append the text you must specify starting at eof
I added some error handling if something goes wrong with the open text file
set theOrder to (path to home folder as text) & "CustomerOrder.TXT"
set CustomerID to ""
set InvoiceNo to ""
repeat
set {text returned:CustomerID} to display dialog "Please enter Customer ID number :" buttons {"End", "OK"} cancel button "End" default button 2 default answer CustomerID
set {text returned:InvoiceNo} to display dialog "Please enter Invoice No. :" buttons {"OK"} default button 1 default answer InvoiceNo
try
set fileRef to open for access file theOrder with write permission
write (InvoiceNo & " " & CustomerID & return) to fileRef starting at eof
close access fileRef
on error
try
close file theOrder
end try
end try
end repeat
Note: the & operator concatenates strings if all operands are strings or can be coerced to string.
An explicit as string coercion is not needed