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
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.
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
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
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
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.