Finder's trash object

I certainly can open it running

tell application "Finder" to open trash

but cannot find a(n elegant) way to close it, nor to verify if it’s open.

I want to make a script that opens it if it’s closed and closes it if it’s open.

Do you mean something like this


tell application "Finder"
	if exists Finder window "Trash" then
		close Finder window "Trash"
	else
		open trash
	end if
end tell

That doesn’t work if either ~/.Trash or trash is open.

tell application "Finder"
	exists Finder window "Trash"
		--> false
	open trash
end tell

window "Trash" is not the same as trash, although it does close if I use close every window.

In ML trash has a dark grey bar on top of window.

That wasn’t clear in your question

open trash in Finder opens a Finder window named “Trash”

It does, when one is running US English.
For everybody else this works:

tell application "Finder"
	set localisedTrash to localized string "N39"
	if exists Finder window localisedTrash then
		close Finder window localisedTrash
	else
		open trash
	end if
end tell

I can close it by id but I cannot find a way to distinguish trash from other windows. Any ideas?

Thanks alastor933. The following script works on English keyboards.

tell application "Finder"
	set LocalisedTrash to localized string "N18"
	if exists Finder window LocalisedTrash then
		close Finder window LocalisedTrash
	else
		activate
		open trash
	end if
end tell

Key for “Trash” is in /System/Library/CoreServices/Finder.app/Contents/Resources/English.lproj/Localizable.strings

Could you please elaborate on how localized string works? Apple’s documentation is rather scant.

The command will retrieve the requested localised string for the current locale, i.e. the language & country the Mac is running.

It will look in a file “Localized.strings” in the Resources folder of the targeted app, as you have found.
You can use a different file by telling the command which file to look in:

tell application "Finder" to return localized string "blah" from table "LocalizableCore"

You can get strings from any app by adding the in bundle qualifier, without telling them.

You’ll find this command in Standard Additions.

What I don’t get is how localized string "N18" is different from "Trash"?

Both statements return the same type text.

What does AppleScript understand by close Finder window LocalisedTrash and by close Finder window "Trash"?

When you run it on a French system it returns : “Corbeille” which is the name given to the trash here.
It’ what is supposed to say the wording : localized string.

close Finder window LocalisedTrash does the job here
because at execution it becomes : close Finder window "Corbeille".
close Finder window "Trash" doesn’t because there is no window with this name here.

Yvan KOENIG running El Capitan 10.11.1 in French (VALLAURIS, France) lundi 16 novembre 2015 15:07:49

So does this work in your French localisation?

tell application "Finder"
	set LocalisedTrash to "Corbeille"
	if exists Finder window LocalisedTrash then
		close Finder window LocalisedTrash
	else
		activate
		open trash
	end if
end tell

I verified key N18 in /System/Library/CoreServices/Finder.app/Contents/Resources/English.lproj/Localizable.strings again and now I see it’s empty!

tell application "Finder"
	set test1 to localized string "N39" from table "Localizable"
	set test2 to localized string "N18" from table "Localizable"
end tell


tell application "Finder"
	localized string "N39" from table "Localizable"
		--> "Trash"
	localized string "N18" from table "Localizable"
		--> ""
end tell
Result:
""

At least it works, but I’m really curious about how this actually functions.

Oh I got it. This works as long as there’s no other window without name in the stack.

tell application "Finder"
	if exists Finder window "" then
		close Finder window ""
	else
		activate
		open trash
	end if
end tell

When the trash is open, here I have a window named “Corbeille” so I assume that you have one named “Trash”.

In the late version of the Finder, engineers changed the key corresponding to the Trash name so the script must be :

if (system attribute "sys2") > 10 then
	set theKey to "N39" # 10.11 running
else
	set theKey to "N18" # "old" system running
end if
tell application "Finder"
	set localisedTrash to localized string theKey
	if exists Finder window localisedTrash then
		close Finder window localisedTrash
	else
		open trash
	end if
end tell

Here, when 10.11 is running, localized string “N18” is : (Pour déplacer les éléments, cliquez sur Authentifier.)

Yvan KOENIG running El Capitan 10.11.1 in French (VALLAURIS, France) lundi 16 novembre 2015 16:52:45

Alternative


tell application "Finder"
	try
		close (1st Finder window whose name of target is "")
	on error
		open trash
	end try
end tell

Or perhaps:

Finder window (get displayed name of trash)

A minor thing: system attribute should probably be marked deprecated, because the underlying gestalt command was deprecated in 10.8. And it’s also relatively slow because it involves sending an Apple event. Something like getting the version of Finder or AppleScript itself would probably be preferable.

Key N18 I confused with an AXIdentifier. Forget it, my bad. Right key is N39 whose value in my system is “”.

What’s the result for this statement in your system? Just curious.

tell application "Finder" to get name of trash

This shouldn’t miss it in case more than one window has name “”.

tell application "Finder"
	activate trash
	set theTrash to name of trash
	if exists Finder window theTrash then
		close Finder window theTrash
	else
		activate
		open trash
	end if
end tell

It returns “” but the window is named Corbeille.

tell application "Finder" to get name of every window
{"Corbeille", "ASObjC scripts"}

Yvan KOENIG running El Capitan 10.11.1 in French (VALLAURIS, France) mardi 17 novembre 2015 09:29:54

The Standard Additions dictionary states :

system attribute‚v : Test attributes of this computer
system attribute [any] : the attribute to test (either a “Gestalt” value or a shell environment variable).
[has integer] : test specific bits of response (ignored for environment variables)
→ any : the result of the query (or a list of all environment variables, if no attribute is provided)

Nothing about a possible deprecation but I will take care of your advice and switch to the version of AppleScript when I will know how to get it. At this time I just know how to get the version of the script editor.
I’m just aware of : use Applescript version “2.3.1” but I can’t use it in a try block.
And as you know, I’m reluctant to speak to the Finder.

Yvan KOENIG (VALLAURIS, France) mardi 17 novembre 2015 09:45:20