Clean AppleScript has error in Automator

I have AppleScript code which returns Folder name, creation date, filename, modification day, date, time, and repeats thru sub folders. Now I need to do the same task in Automator.

This AppleScript.app worked when code was built as an app on several Folders…


property file_id : null

--set this_folder to (path to me)

set this_folder to choose folder with prompt "Choose the root folder"
set log_file_path to (path to desktop as string) & "Folder Inventory List.txt"
set file_id to open for access file log_file_path with write permission
--write ("Start " & (current date) & return) to file_id starting at eof
my do_subfolders(this_folder, "")
write (return) to file_id starting at eof -- write (return & return) to file_id starting at eof
close access file_id
display dialog "Finished"
return

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 starting at eof
		
		set file_list to every file in this_folder
		repeat with this_file in file_list
			get creation date of this_file
			log name_prefix & "      " & (name of this_file) as string
			write (return & name_prefix & "      " & (name of this_file) & "   " & (creation date of this_file) as string) to file_id starting at eof
		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 & "  " & (creation date of this_file))
		end repeat
	end tell
end do_subfolders

returning text like this…

[format]_Book 1.8
Thursday, 16 January 2020 at 14:47:04
VFC-PS
Thursday, 16 January 2020 at 14:47:04 SassoBoo.vfc Wednesday, 11 March 2020 at 18:39:16
Thursday, 16 January 2020 at 14:47:04 SassoBooIta.vfc Wednesday, 11 March 2020 at 18:38:55[/format]

But started to produce errors when run more than 8 times…
The variable this_file is not defined.
The variable this_file is not defined. (-2753) , which displayed the Folder names in the txt file but no Contents.

Rebuilding as an .app in AppleScript Version 2.2 (2.2) it won’t run as its not a PowerPC app.
So pasting the AppleScript code into the Automator Action “Run AppleScript”, I get…

Syntax Error
File file Macintosh HD:Users:adrianshome:Desktop:Folder Inventory List.txt is already open.
But it created a .txt file and which displays the Folder name, _Book 1.8 but nothing else.

Any help will be much appreciated,
Adrian

Hi.

I haven’t looked at the Automator context yet, but two things which strike me looking at your code:

  1. There’s no provision for closing the file access in the event of an error stopping the script. If the file’s left open with write permission, it won’t be openable again with write permission until the application running the script either closes the access itself or quits (thereby giving up all its currently open file access channels).

  2. Your this_file variable is defined as the loop variable in the first repeat in the handler. If there are no files in a folder, the first repeat won’t be executed and this_file won’t be defined. So when you try to access it in the second repeat, you’ll get the error you describe. Even when the first repeat does execute, the value of this_file in the second repeat will be the value it was given last time round the first. I’m not sure what the script’s supposed to do. Should the variable in the second repeat be this_subfolder?

Hello Nigel,

  1. That would explain the error I get.
  2. Yes the second repeat is for subfolders.
    I’ll tinker with this and post result.
    Many thanks…