AppleScript: Wie schreibe ich in eine bestimmte Zeile einer Textdatei?

Beispielinhalte einer .txt-Datei auf meinem Mac:

This is sentence A.
This is sentence B.
This is sentence C.
This is sentence D.
This is sentence E.

Ich möchte eine Unterroutine wie folgt implementieren:

writeTextAtSpecificLine(("This is the newcomer sentence." & linefeed), 4)

die Textdatei würde folglich so aussehen:

This is sentence A.
This is sentence B.
This is sentence C.
This is the newcomer sentence
This is sentence D.
This is sentence E.

Ich weiß, dass ich mein gewünschtes Ergebnis erzielen kann, indem ich jede Zeile der Textdatei zu einem eigenen Element in einem AppleScript- listObjekt mache, etwa so:

set theIndividualLinesOfTextFileAsList to paragraphs of (read theFilepathOfTxtFile)

Ich könnte dann die , basierend auf der gewünschten Zeilennummer, aufteilen listund dann alles wieder zusammenfügen.

Aber gibt es eine effizientere Methode?

Meine echte .txt-Datei ist viel länger als der obige Beispielinhalt.

Antworten (2)

Bearbeitete Version:

set selected_file to ((path to documents folder as text) & "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) & (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 & linefeed & ((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

Ich bin mir nicht sicher, ob dies der effizienteste Weg ist, aber dies wird das erreichen, wonach Sie suchen. Funktioniert bei mir mit der neuesten Version von Sierra

tell application "TextEdit" to make new paragraph at after third paragraph of text of document 1 with data "This is the newcomer sentence. \n"

AKTUALISIERTE ANTWORT: OPTION 2
Dies wäre meine Wahl.

property theFile : POSIX path of (path to desktop as text) & "test.txt"
property newContent : missing value
property sedSTDOUT : "sed " -- This Option For Testing
property sedOverwriteFile : "sed -i ' ' " -- This Option Overwrites The File

addNewSentence(sedSTDOUT, "sentence C.", "This is the newcomer sentence.")
--addNewSentence(sedOverwriteFile, "sentence C.", "This is the newcomer sentence.") -- Writes To The File

to addNewSentence(whichSED, lastTextItemsOfSentenceToAddNewSentenceAfter, newSentenceContent)
    set newContent to do shell script whichSED & "'s/" & lastTextItemsOfSentenceToAddNewSentenceAfter & ¬
        "/" & lastTextItemsOfSentenceToAddNewSentenceAfter & "\\" & linefeed & ¬
        newSentenceContent & "/' " & quoted form of theFile
    return newContent
end addNewSentence
Beim Testen hatte ich nur ein Dokument in Textedit geöffnet, daher funktionierte "Dokument 1". Wenn Sie mehrere Dokumente geöffnet haben, müssen Sie den Code möglicherweise ein wenig anpassen, damit er auf das richtige Dokument abzielt.
Ich habe vergessen zu erwähnen, dass ich in die Textdatei schreiben möchte, ohne die Datei zu öffnen. Die Datei wird niemals geöffnet, wenn das Skript ausgeführt wird. Daher suche ich nach einer mehr "hinter den Kulissen"-Methode.