Empty Trash Automator Versus AppleScript

I’ve had for some time an Automator app that empties the trash without any warning window popup (see image).

Trying to put that same AppleScript:

	tell application "Finder"
		empty the trash
	end tell
end run

into an app created by Script Debugger, throws up the warning. Is there any way to eliminate the warning other than within Finder preferences?

Try this:

empty the trash without warns before emptying

According to the Finder’s scripting dictionary, it should be:

tell application "Finder"
	set trash's warns before emptying to false
	empty
end tell

Or more conscientiously:

tell application "Finder"
	tell trash
		set warningState to warns before emptying
		set warns before emptying to false
		empty
		set warns before emptying to warningState
	end tell
end tell

Thanks, both work perfectly, but just to help as I’m learning, why did you use “Or more conscientiously” to describe the second script?

Hi.

The first script just sets the trash’s warns before emptying property to false and then empties the trash. The setting remains false after that until you actively do something about it. If you normally have the setting as true, this can be very annoying.

The second script notes what the setting was when it started to run and restores it once it’s emptied the trash. It’s considered “good manners” in scripting not to leave a user’s settings changed by a script — unless of course that’s the very purpose of the script. But then again, if you yourself are to be the sole user of a script, then observing etiquette isn’t perhaps quite so necessary. :slight_smile:

1 Like

Thank you for the explanation. With that in mind, now when I look at both scripts, I can see where that difference in the two scripts shows up.

After a few MacOS updates (now on Monterey 12.7.2) the Trash warning no longer resets to “on” using this script.

tell application "Finder"
	tell trash
		set warningState to warns before emptying
		set warns before emptying to false
		empty
		set warns before emptying to warningState
	end tell
end tell

Any ideas as to why this changed?

After a bit more playing around, the script I posted works perfectly (turns warning off/on) when run as an application generated by a script. It doesn’t work when added to an Automator application with other AppleScripts. I can live with that.

Think this may remain one of those “mystery” things. Both the scripts you posted work. But on my system (M1, 2020 MacBook Pro, Monterey 12.7.2) the first one ends up turning off the Finder preference “show warning before emptying the Trash”, the second one does not turn off the Finder preference “show warning before emptying the Trash.”

Further, it does not seem to make any difference whether the scripts are run from within Script Debugger, an AppleScript application, or as an AppleScript run from an Automator application. The results are always the same as above, first one turns the warning off, the second one does not.

So the good news is that I have a solution, the bad news is I have no idea why that solution works. But I can live with that as well :smiley:

My post was incorrect, and I’ve deleted it.

Correct or not, the second, longer script, where the trash gets counted works for me without turning off the trash warning. And luckily, I already have a copy :grin:

1 Like

Have found a solution to adding an “Empty Trash” AppleScript to Automator and not having the Finder Preference “Show warning before emptying the Trash” end up being unchecked after the Trash gets emptied.

tell application "Finder"
	empty trash
end tell

Have no idea why this simple script works when others do not, but at least I have a solution.