1994firehawk

Q: Applescript to blank a text file

Hi, below is a working Applescript that lists the contents of a folder and its sub-folders to a text file named "Folder Inventory". The problem, for example, is that if a sub-folder is emptied of files, the list of its previous contents is still shown in the inventory text file from the last time the script was run. So I would like to delete all text in the file before it is written to again. I've searched and found this code and put my filename in it but I can't get it to work.

Any assistance will be appreciated.

 

set eof "files:folder inventory.txt" to 0

 

 

              property file_id : null


              tell application "Finder" to set this_folder to folder "movies" of disk "files"

              set log_file_path to "files:Folder Inventory.txt"

              set file_id to open for access file log_file_path with write permission

              try

                               write ("Start " & (current date) & return) to file_id  

                               my do_subfolders(this_folder, "")

                               write (return & return) to file_id

                               close access file_id

                               say "finished"

              on error errMsg

                               close access file_id

                               say errMsg

              end try

              on do_subfolders(this_folder, name_prefix)

                               tell application "Finder"

                                               log name_prefix & (name of this_folder) as string

                                              write (return & name_prefix & (name of this_folder) as string) to file_id

                                              set file_list to every file in this_folder

                                              repeat with this_file in file_list

                                                              log name_prefix & " " & (name of this_file) as string

                                                             write (return & name_prefix & " " & (name of this_file) as string) to file_id

                                              end repeat

                                              set folder_list to every folder in this_folder

                                              repeat with this_subfolder in folder_list

                                                              my do_subfolders(this_subfolder, " " & name_prefix)

                                              end repeat

                              end tell

               end do_subfolders

Mac mini, OS X Mavericks (10.9.5)

Posted on Sep 4, 2016 1:24 PM

Close

Q: Applescript to blank a text file

  • All replies
  • Helpful answers

  • by Camelot,

    Camelot Camelot Sep 4, 2016 5:49 PM in response to 1994firehawk
    Level 8 (47,233 points)
    Mac OS X
    Sep 4, 2016 5:49 PM in response to 1994firehawk

    You're on the right path, but set eof takes the file reference that you previously opened, not the path in text form.

     

    so, since you open the file via:

     

                  set file_id to open for access file log_file_path with write permission

     

    the appropriate solution would be:

     

    set eof file_id to 0