Append to file question...

I had this script working earlier but I did something to it and can’t figure out my issue.
I want to use this script to append data to the existing file. I keep getting this error… Where did I go wrong ?

Also, can this be improved to only get the folders info and not the folder contents? This is a special use for one folder.

I ripped all my CD’s to FLAC files. I dump the contents of this music folder to iCloud so when I’m hunting new CD’s to rip I have a list of title’s I already have I avoid buying duplicates. Currently this lists the contents or songs of each folder. Thanks

Error message…
File file Macintosh HD:Users:MG:Desktop:abcd.txt is already open." number -49 from file “Macintosh HD:Users:MG:Desktop:abcd.txt” to «class fsrf»



try
	close file "abcd.txt"
end try

--Last run date is 02/20/19, modified on 2/20/19 to append data to file via the close file section
property file_id : null





set this_folder to choose folder with prompt "Choose the root folder"
set log_file_path to (path to desktop as string) & "abcd.txt"
set file_id to open for access file log_file_path with write permission


return
write ("Run Date is " & (current date) & return) to file_id starting at eof
my do_subfolders(this_folder, "")
write (return & return) to file_id starting at eof
close access file_id


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
			log name_prefix & " " & (name of this_file) as string
			write (return & name_prefix & " " & (name 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)
		end repeat
	end tell
end do_subfolders

It seems that it would be useful to edit the beginning as :

try
	close file (path to desktop as string) & "abcd.txt"
end try

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 20 février 2019 16:52:48

Here is a version which runs correctly.



--Last run date is 02/20/19, modified on 2/20/19 to append data to file via the close file section
--property file_id : null





set this_folder to choose folder with prompt "Choose the root folder"

set log_file_path to (path to desktop as string) & "abcd.txt"
try
	close access file log_file_path
end try

set file_id to open for access file log_file_path with write permission


--return
write ("Run Date is " & (current date) & return) to file_id starting at eof
my do_subfolders(this_folder, "", file_id)
write (return & return) to file_id starting at eof
close access file_id


on do_subfolders(this_folder, name_prefix, file_id)
	tell application "Finder"
		set folderName to name of this_folder
		-- log name_prefix & folderName
		# Don't speak to Finder to get rid of a non fatal error
		tell me to write (return & name_prefix & folderName) to file_id starting at eof
		set file_list to (every file in this_folder) as alias list
		repeat with this_file in file_list
			set fileName to name of this_file
			-- log name_prefix & " " & fileName
			# Don't speak to Finder to get rid of a non fatal error
			tell me to write (return & name_prefix & " " & fileName) to file_id starting at eof
		end repeat
		set folder_list to (every folder in this_folder) as alias list
		repeat with this_subfolder in folder_list
			my do_subfolders(this_subfolder, " " & name_prefix, file_id)
		end repeat
	end tell
end do_subfolders

May you explain which infos about subfolders are supposed to be stored ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 20 février 2019 19:21:03

Thanks for your help. I can now append as needed. To answer your question, I will provide an example of what I’m trying to accomplish for this script…

I ran this against a folder stored on my NAS drive;

In the examples below this script returns The folder name “Jethro Tull”, and the Title of the Album “Aqualung” and all the tracks for that album…“01 Aqualung.flac”,
“02 Cross-Eyed Mary.flac”, etc…

Jethro Tull
Aqualung (Steven Wilson Mix And Master)
01 Aqualung.flac
02 Cross-Eyed Mary.flac
03 Cheap Day Return.flac
04 Mother Goose.flac
05 Wond’ring Aloud.flac
06 Up To Me.flac
07 My God.flac
08 Hymn 43.flac
09 Slipstream.flac
10 Locomotive Breath.flac
11 Wind-Up.flac

How can I get only “Jethro Tull” and “Aqualung” and if there are more albums listed under “Jethro Tull”, get them as well.

See the Jeff Beck example…I only want to get Blow by Blow, Flash, Guitar Shop, Jeff, Truth, Wired.

If I only selected the export to look at the folder named Jeff Beck, I’d get Jeff Beck at the top and a listing of the albums I have underneath.

Jeff Beck
Blow by Blow
Flash
Guitar Shop
Jeff
Truth
Wired

Currently this script returns the following when I select Jeff Beck…

Beck, Jeff
Blow by Blow
01 Jeff Beck - You Know What I Mean.flac
02 Jeff Beck - She’s A Woman.flac
03 Jeff Beck - Constipated Duck.flac
04 Jeff Beck - Air Blower.flac
05 Jeff Beck - Scatterbrain.flac
06 Jeff Beck - Cause We’ve Ended As Lovers.flac
07 Jeff Beck - Thelonius.flac
08 Jeff Beck - Freeway Jam.flac
09 Jeff Beck - Diamond Dust.flac

Flash
01 Jeff Beck - Ambitious.flac
02 Jeff Beck - Gets Us All in the End.flac
03 Jeff Beck - Escape.flac
04 Jeff Beck feat. Rod Stewart - People Get Ready.flac
05 Jeff Beck - Stop, Look and Listen.flac
06 Jeff Beck - Get Workin’.flac
07 Jeff Beck - Ecstasy.flac
08 Jeff Beck - Night After Night.flac
09 Jeff Beck - You Know, We Know.flac
10 Jeff Beck - Nighthawks.flac
11 Jeff Beck - Back on the Streets.flac

Guitar Shop
01 Jeff Beck with Terry Bozzio and Tony Hymas - Guitar Shop.flac
02 Jeff Beck with Terry Bozzio and Tony Hymas - Savoy.flac
03 Jeff Beck with Terry Bozzio and Tony Hymas - Behind the Veil.flac
04 Jeff Beck with Terry Bozzio and Tony Hymas - Big Block.flac
05 Jeff Beck with Terry Bozzio and Tony Hymas - Where Were You.flac
06 Jeff Beck with Terry Bozzio and Tony Hymas - Stand on It.flac
07 Jeff Beck with Terry Bozzio and Tony Hymas - Day in the House.flac
08 Jeff Beck with Terry Bozzio and Tony Hymas - Two Rivers.flac
09 Jeff Beck with Terry Bozzio and Tony Hymas - Sling Shot.flac

Jeff
01 Jeff Beck - So What.flac
02 Jeff Beck - Plan B.flac
03 Jeff Beck - Pork-U-Pine.flac
04 Jeff Beck - Seasons.flac
05 Jeff Beck - Trouble Man.flac
06 Jeff Beck - Grease Monkey.flac
07 Jeff Beck - Hot Rod Honeymoon.flac
08 Jeff Beck - Line Dancing With Monkeys.flac
09 Jeff Beck - JB’s Blues.flac
10 Jeff Beck - Pay Me No Mind (Jeff Beck remix).flac
11 Jeff Beck - My Thing.flac
12 Jeff Beck - Bulgaria.flac
13 Jeff Beck - Why Lord Oh Why?.flac

Truth
01 Jeff Beck - Shapes of Things.flac
02 Jeff Beck - Let Me Love You.flac
03 Jeff Beck - Morning Dew.flac
04 Jeff Beck - You Shook Me.flac
05 Jeff Beck - Ol’ Man River.flac
06 Jeff Beck - Greensleeves.flac
07 Jeff Beck - Rock My Plimsoul.flac
08 Jeff Beck - Beck’s Bolero.flac
09 Jeff Beck - Blues De Luxe.flac
10 Jeff Beck - I Ain’t Superstitious.flac

Wired
01 Jeff Beck - Led Boots.flac
02 Jeff Beck - Come Dancing.flac
03 Jeff Beck - Goodbye Pork Pie Hat.flac
04 Jeff Beck - Head for Backstage Pass.flac
05 Jeff Beck - Blue Wind.flac
06 Jeff Beck - Sophie.flac
07 Jeff Beck - Play With Me.flac
08 Jeff Beck - Love Is Green.flac
Wired.cue

Hope this helps…

If I understand correctly this code may do the job.



--Last run date is 02/20/19, modified on 2/20/19 to append data to file via the close file section
--property file_id : null





set this_folder to choose folder with prompt "Choose the root folder"

set log_file_path to (path to desktop as string) & "abcd.txt"
try
	close access file log_file_path
end try

set file_id to open for access file log_file_path with write permission


--return
write ("Run Date is " & (current date) & return) to file_id starting at eof
my do_subfolders(this_folder, "", file_id)
write (return & return) to file_id starting at eof
close access file_id


on do_subfolders(this_folder, name_prefix, file_id)
	tell application "Finder"
		set folderName to name of this_folder
		-- log name_prefix & folderName
		# Don't speak to Finder to get rid of a non fatal error
		tell me to write (return & name_prefix & folderName) to file_id starting at eof
		(*
		# We don't want to display simple files !
		set file_list to (every file in this_folder) as alias list
		repeat with this_file in file_list
			set fileName to name of this_file
			-- log name_prefix & " " & fileName
			# Don't speak to Finder to get rid of a non fatal error
			tell me to write (return & name_prefix & " " & fileName) to file_id starting at eof
		end repeat
		*)
		set folder_list to (every folder in this_folder) as alias list
		repeat with this_subfolder in folder_list
			my do_subfolders(this_subfolder, " " & name_prefix, file_id)
		end repeat
	end tell
end do_subfolders

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 21 février 2019 16:18:29

This works…I much appreciate the help. Now I have an example to use for future reference.

Beatles, The
Let It Be
One
The White Album
Beck, Jeff
Blow by Blow
Flash
Guitar Shop
Jeff
Truth
Wired

One more ?
How can I get the file name to populate automatically (pass the folder name I am running the script against to auto fill the file name info)

Example; The folder is called Music that I selected…I’d like to pass that folder name from the selection prompt to the file name “Music.txt”

Try :


set this_folder to choose folder with prompt "Choose the root folder"

tell application "Finder"
	set theName to name of this_folder
end tell
set log_file_name to theName & ".txt"
set log_file_path to (path to desktop as string) & log_file_name

try
	close access file log_file_path
end try

set file_id to open for access file log_file_path with write permission


--return
write ("Run Date is " & (current date) & return) to file_id starting at eof
my do_subfolders(this_folder, "", file_id)
write (return & return) to file_id starting at eof
close access file_id


on do_subfolders(this_folder, name_prefix, file_id)
	tell application "Finder"
		set folderName to name of this_folder
		-- log name_prefix & folderName
		# Don't speak to Finder to get rid of a non fatal error
		tell me to write (return & name_prefix & folderName) to file_id starting at eof
		(*
		# We don't want to display simple files !
		set file_list to (every file in this_folder) as alias list
		repeat with this_file in file_list
			set fileName to name of this_file
			-- log name_prefix & " " & fileName
			# Don't speak to Finder to get rid of a non fatal error
			tell me to write (return & name_prefix & " " & fileName) to file_id starting at eof
		end repeat
		*)
		set folder_list to (every folder in this_folder) as alias list
		repeat with this_subfolder in folder_list
			my do_subfolders(this_subfolder, " " & name_prefix, file_id)
		end repeat
	end tell
end do_subfolders

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 21 février 2019 18:18:26

This is perfect!
Thank You

I guess that you may get a faster treatment with this version which no longer speak to Finder but to System Events.

(*

https://macscripter.net/viewtopic.php?pid=195143#p195143

Append to file question

*)

set this_folder to choose folder with prompt "Choose the root folder"

tell application "System Events"
	set theName to name of this_folder
end tell
set log_file_name to theName & ".txt"
set log_file_path to (path to desktop as string) & log_file_name

try
	close access file log_file_path
end try

set file_id to open for access file log_file_path with write permission


--return
write ("Run Date is " & (current date) & return) to file_id starting at eof
my do_subfolders(this_folder, "", file_id)
write (return & return) to file_id starting at eof
close access file_id


on do_subfolders(this_folder, name_prefix, file_id)
	tell application "System Events"
		set folderName to name of this_folder
		-- log name_prefix & folderName
		# Don't speak to System Events to get rid of a non fatal error
		tell me to write (return & name_prefix & folderName) to file_id starting at eof
		(*
		# We don't want to display simple files !
		set file_list to (every file in this_folder) --as alias list
		repeat with this_file in file_list
			set fileName to name of this_file
			-- log name_prefix & " " & fileName
			# Don't speak to System Events to get rid of a non fatal error
			tell me to write (return & name_prefix & " " & fileName) to file_id starting at eof
		end repeat
		*)
		set folder_list to (every folder in this_folder) --as alias list
		repeat with this_subfolder in folder_list
			my do_subfolders(this_subfolder, " " & name_prefix, file_id)
		end repeat
	end tell
end do_subfolders

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 21 février 2019 20:02:26

This is very very fast, however the output is not sorted the same as the previous version.

CD Resolution
Lynn, Loretta
Van Lear Rose
Smashing Pumpkins
Zero
Bullet With Butterfly Wings
Earphoria
Adore
Siamese Dream
Melon Collie and the Infinite Sadness
Raitt, Bonnie
Souls ALike
Nick Of Time
Luck Of The Draw
Longing in Their Hearts
Seger, Bob

It sounds as if System Events doesn’t sort folder contents when Finder does it - according to its settings.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 22 février 2019 15:12:00