AppleScript: How to write to a specific line of a text file?
Edited version:

set selected_file to ((path to documents folder as text) &amp; "hom.txt") as alias
set text_data to "This is the newcomer sentence."

InsertLine(selected_file, text_data, 4)

on InsertLine(the_file, the_data, num_line)
    try
        set file_id to open for access the_file with write permission
        set text_data to read file_id

        -- CHECK EMPTY LINE
        if (paragraph num_line of text_data) ≠ "" then
            set the_offset to (offset of (paragraph num_line of text_data) in text_data) - 1
            set remaining_data to (the_data) &amp; (text the_offset thru -1) of text_data
            set eof of file_id to the_offset
        else -- LINE IS EMPTY
            set num_line to num_line - 1
            set the_count to (count of (characters of (paragraph num_line of text_data))) - 1
            set the_offset to (offset of (paragraph num_line of text_data) in text_data) + the_count + 2
            set remaining_data to the_data &amp; linefeed &amp; ((text (the_offset + 1) thru -1) of text_data)
            set eof of file_id to the_offset - 1
        end if

        -- WRITE DATA TO FILE
        --write the_data to file_id -- INSERT LINE
        write remaining_data to file_id -- WRITE REMAINING DATA
        close access file_id
    on error
        try
            close access the_file
        end try
    end try
end InsertLine