How do I write to a text file using AppleScript?

Juan A. Navarro picture Juan A. Navarro · Sep 23, 2010 · Viewed 39.6k times · Source

So, that's it. How can I write to a text file using AppleScript?

I've tried googling around, but answers seem to be years old and I'm not really sure what should be the preferred idiom this days.

Answer

Philip Regan picture Philip Regan · Sep 23, 2010
on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
    try
        set the target_file to the target_file as text
        set the open_target_file to ¬
            open for access file target_file with write permission
        if append_data is false then ¬
            set eof of the open_target_file to 0
        write this_data to the open_target_file starting at eof
        close access the open_target_file
        return true
    on error
        try
            close access file target_file
        end try
        return false
    end try
end write_to_file

Interfacing with it can be cleaned up with the following...

my WriteLog("Once upon a time in Silicon Valley...")

on WriteLog(the_text)
    set this_story to the_text
    set this_file to (((path to desktop folder) as text) & "MY STORY")
    my write_to_file(this_story, this_file, true)
end WriteLog