Can someone tell me what the best way to insert text to a certain line in a file? For example, say I have a file with 1,000 lines in it. On line 500 it says “TEXT UNDER HERE”. How can I insert a new line after the TEXT UNDER HERE then have the text I want displayed underneath the empty line?
This could probably be done with any application that supports a Find/Replace. Just find the text “TEXT UNDER HERE” and replace it with “TEXT UNDER HERE” & return & myText.
Would it be possible to use Textedit to open a hidden system file (that needs to have the admin password typed in when changed) or would it be best to use “do shell script”?
Hi I don’t know what hidden system file is, but you can insert straight in a text file. It’s not actually inserting, but writing from a certain place. In your case, it writes after the search string “TEXT UNDER HERE”. Here’s an example:
set search_text to “TEXT UNDER HERE”
set insert_text to “Here’s more text.”
set the_file to (choose file)
set ref_num to (open for access the_file with write permission)
try
set the_text to (read ref_num) – eof error if no text
– find the byte
set the_offset to (offset of search_text in the_text)
if (the_offset = 0) then error “Search string wasn’t found.”
set byte_after to (the_offset + (length of search_text))
try
set trail_text to (text byte_after thru -1 of the_text)
on error – there is no trailing text
set trail_text to “”
end try
write (return & insert_text & trail_text) to ref_num starting at byte_after
close access ref_num
on error err_mess
close access ref_num
display dialog err_mess buttons {“OK”} default button “OK”
return
end try
beep 3
gl,