How to list all files currently open in TextEdit?

Can anyone tell me how to generate a list of all files currently open in TextEdit? Preferably I’d like to save the list of currently open files, then open them all again at a later point.

I tend to have a lot of open file notes, and sometimes lose them upon rebooting.

If I could script “save all currently open files”, name the list of files, and later run a script that opens all files on that list, I could further improve my workflow by have separate file lists related to various projects, so that I’d only need to have smaller groups of files open at any one time, related to just one or more projects.

Thanks for your help!

Model: Mac Pro 2009
Browser: Safari 537.36
Operating System: macOS 10.11

You may try:

tell application "TextEdit"
	set POSIXPaths to path of every document
end tell

set targetFile to (path to desktop as string) & "POSIXPaths.txt"

if (count POSIXPaths) > 0 then
	set theData to my recolle(POSIXPaths, linefeed)
	my writeto(targetFile, theData, «class utf8», false)
	tell application "TextEdit"
		close every document
	end tell
end if

set thePOSIXPaths to paragraphs of (read file targetFile as «class utf8»)
repeat with aPath in thePOSIXPaths
	set aFile to POSIX file aPath -- MUST be out of the tell application block
	tell application "TextEdit"
		open aFile
	end tell
end repeat


#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as «class furl»
		set openFile to open for access targetFile with write permission
		if not apendData then set eof openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end writeto

#=====

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 17 avril 2020 12:02:14

Thank you so much Yvan! I will try it this weekend. :slight_smile: