0

I running an AppleScript, which is saving the variable I want in a textfile, here is my code :

do shell script "echo " & quoted form of myDateTime & " >> Users/kevin/Documents/data/data_Backup.txt"

do shell script "echo " & quoted form of myNote & " >> Users/kevin/Documents/data/data_Backup.txt"

It's doing what I want, but the each time I run the script, the new data are added at the button of the text, and the oldest data are at on top, how can I have the newest data on top instead?

E.g

data_backup.txt data 3 data 2 data 1

also can I add a count on the text E.g

data_backup.txt data3 case3, data2 case2, data1 Case 1

1 Answer 1

2

You need the read/write commands of AppleScript to read the text, insert the new data at the beginning and write it back.

This is a handler which returns true on success otherwise false

set dataBackupFile to (path to documents folder as text) & "Data:data_Backup.txt"
insertOnTop from "foo" into dataBackupFile
insertOnTop from "bar" into dataBackupFile

on insertOnTop from theData into theFile
    try
        set fileDescriptor to open for access file theFile with write permission
        if (get eof fileDescriptor) > 0 then
            set theContent to read fileDescriptor as «class utf8»
        else
            set theContent to ""
        end if
        set eof fileDescriptor to 0
        write (theData & theContent) to fileDescriptor as «class utf8»
        close access fileDescriptor
        return true
    on error
        try
            close access file theFile
        end try
        return false
    end try
end insertOnTop
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.