Hello! I have this “handler for reading lines” already written:
set CR to (return as string)
-- is there an ASCII Character for 'new line' ? 'line feed' ?
(* This handler reads line "line_to_read" from file "Path_to_file".
A line is considered if it ends in 'Carriage Return' .*)
to read_line(Path_to_file, line_to_read)
set Prev_delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to CR -- this is what will now separate elements.
set the_data to text items of (read Path_to_file from 1 to eof)
try
set The_Return to (item line_to_read of the_data)
on error
set The_Return to 0
end try
set AppleScript's text item delimiters to Prev_delim -- this MUST be executed, or else mayor crash in afterwards processes.
return The_Return
end read_line
This one works ok as long one nows what kind of file one is reading.
I’d like one for “writing lines”. I started one with exactly the same code as above but changing the try a little, but realized some problems came up if the replacement line is shorter than the older one, and when I wanted to re-write the changed data to the file, some of the old data was left on the file (the last characters)
(* This handler should replace the line 'line' of a file (given it's path)
with 'info'. Again, line is considered if it ends in 'Carriage Return' .*)
to write_line(Path_to_file, line, info)
...
try
set (item line of data_as_list) to info
on error err_msg
set (item line of data_as_list) to ""
end try
my Save_Log(Path_to_file, data_as_list as string, 1)
...
end write_line
where ‘Save_Log’ would be: (I’m just adding this one because ‘write_line’ uses it and because I’d like to now if I’m doing something terrible here I never noticed…)
(* This handler writes 'some_data' to a file (recieves it's path) starting
at 'where'. It does not care if it is overwriting text already written before. *)
on Save_Log(Path_to_file, some_data, where) -- 'where' would be where it will start writing from: eof, 1, 23, 50, -1. -123, etc.
try
try
set where to (where as integer) -- could be 'eof', not a valid integer, but could be 'donkey' too.
-- on error
-- set where to eof -- (Should I? If it was 'donkey', no changes will be made to 'where', so nothing will happen and first 'try' will crash too.)
end try
set Path_to_file to Path_to_file as alias -- to prevent from creating a file (that should be taken care of somewhere else)
open for access Path_to_file with write permission
write some_data to Path_to_file starting at where
close access Path_to_file
on error
try
close access Path_to_file
end try
end try
end Save_Log
Thanks a lot.
BTW: if I saved a script as an app and never backed up. Can I somehow get the source code back? (Tried dropping app on AS, opening in many text editors, etc. no result). I did so accidentally on a script that I’ve working for months… darn. And what is the difference between an app, an app bundle, a ‘run only’ script and all the saving options in AS?
OK, thanks a lot again, hope to get goos news!!
Nikel