Close specific textedit document

Hello,

I did do search here and with google, but I couldn’t find it. Its maybe very simple, but I will ask for some guidance from the specialists. This is what I would like to do.

In mij test app, I save some formatted data in text format and want to open this document in text edit.
This is working.

However , after the next run on my app, the data will be overwritten and I want to ( close and open again or revert to saved ) reopen the document to look into that.

I know the path and title, so, how can I close in applescript that specific document? I cant choose the front document because I got have more documents open at the time that my app will start running.

thanks in advance

RvA

Edited:
Something like this?

set theFile to "/path/to/file.txt"
tell application "TextEdit"
	set AllWindows to a reference to every window
	repeat with aWindow in AllWindows
		if path of document of aWindow is equal to theFile then
			close document of aWindow without saving
			exit repeat
		end if
	end repeat
	open (POSIX file theFile as string)
end tell

Thank you very much, but I cant get your code working.
Is there a window title prblem? Also what about when two docs are opened with the same filename but different path?

For now, I did came up with the following, but I am more happy with yours when I got it working.


set doc_Path to ("/Volumes/Data/result.txt" as Unicode text)
set doc to POSIX file doc_Path
tell application "TextEdit"
	try
		open (doc as alias)
		close document 1
	end try
end tel

and some thing like yours

tell application "TextEdit"
	try
		set openWindows to every window
		repeat with thisWindow in openWindows
			if name of document of thisWindow is equal to "result.txt" then
				close thisWindow
			end if
		end repeat
	end try
end tell
tell application "TextEdit" to close document "result.txt"

Happy Christmas. :slight_smile:

@Nigel Wow, How simple can live be.

Thank you very much. Nice present.

@DJ, your modified code is working now, thank you.

To come back on my issue with two documents open with the same name but diff path, I did try to use Nigel’s code as base, but can’t get it to work. Any Idea?


set doc to POSIX path of ("/Volumes/Data/result.rtf" as string)

tell application "TextEdit" to close document doc
tell application "TextEdit" to close document  (document path of doc )
tell application "TextEdit" to close document (("Volumes:Data:result.rtf" as string) as alias)

I think I need a reference after the ‘to close document’ but all I get is
Can’t get document “/Volumes/Data/result.rtf”.

Any suggestions?

Happy Christmas

My earlier offering worked because I was using the ‘name’ property of the document and was able to use a direct name reference. If you want to go by the ‘path’ property, you have to use some sort of test. In this particular case, a filter works:

tell application "TextEdit" to close (first document whose path is "/Volumes/Data/result.rtf")

Thank you very much.
Works great.

Happy christmas to you too guys!