Help with AppleScript

Hello,

I would like to close a TextEdit file and I have this AppleScript which needs modification, kindly assist.

tell application "TextEdit"
	activate
	set f to "/Users/len/Desktop/my File.Txt"
	close f without saving
end tell

Thanks.

Lennox

What are you trying to do? The AppleScript doesn’t make sense.

That’s not how it works. You’d need to do something like:

tell application "TextEdit"
     activate
     close document 1 (* without saving *)
end tell

I’ve commented out the “without saving” bit so you don’t accidentally close an important document without saving changes.

It looks like you’re a relative newcomer to AppleScript; however, helping you learn the basics of AppleScript is beyond the scope of this forum. I would suggest you read any one of the excellent tutorials that are out there - AppleScript has been around for over 30 years and there’s a lot of good resources on the web for learning.

Thanks, Eric,

This actually works… and is specific to a particular file.

tell application "TextEdit"
set myFile to "my file.Txt"
close (every window whose name is myFile)
end tell

Thanks gain.

Lennox

I suggest you look at the path property of TextEdit’s document class. You could probably use that to target a document by path (instead of by name, which is hazardous because people name files the same all the time).

Hi Eric,

Thanks, but I would appreciate a link to “TextEdit’s document class”.

I looked here TextEdit Class | WinForms Controls | DevExpress Documentation but that seems to be VB/Windows.

Thanks again.

Lennox

Look into Script Editor on the Mac and the TextEdit terminology dictionary.

Thanks
Lennox

This code would work.

tell application “TextEdit”

activate

set TheList to every document whose path is “/Users/len/Desktop/my File.Txt”

set cnt to count items of TheList

if cnt = 1 then

close (item 1 of TheList) without saving

else if cnt = 0 then

display dialog “Document not found.” buttons {“OK”} default button 1

else --more than one

display dialog “More than one document with that path. Don’t know which one to choose.” buttons {“OK”} default button 1

end if

end tell

Thanks Arnaud,

That works as expected in an independent, standalone, AppleScript, I had to replace the double-quotes with double-quotes with my keyboard… no a problem/issue.

But when I incorporate in an app and call it by a mouse-down click, or the Close event of the app, there is no response, the window is not closed.

How can I fix that?

Thanks.

I use many other AppleScripts in my apps and they all work.

Lennox