If I run this shell command in macOS (11.2.1) Terminal:
[format]sudo rm -rf ~/.Trash/*[/format]
… after entering my Mac password in the system dialog, the trash is successfully forced empty.
But if I try to force empty the trash from this AppleScript, the system dialog appears OK, and the notification works as expected, but the trash is not force-emptied:
The “force-empty-trash.sh” shell script being referenced is:
[format]#!/bin/bash
if osascript -e “do shell script "sudo rm -rf ~/.Trash/*" with administrator privileges”;
then
osascript -e “display notification "Trash was forced empty." with title "OK"”
else
osascript -e “display notification "Trash was NOT forced empty!" with title "ERROR"”
fi[/format]
The shell script shows the same behaviour when run from Terminal.
Hi. Like the terminal, the shell requires a password for sudo.
set wordpass to (display dialog "An admin password is requested for super user access." with icon stop with title "Take heed!" default answer "password")'s text returned
do shell script "printf " & wordpass's quoted form & space & "| sudo -S env_reset,timestamp_timeout=0 rm -rf ~/.Trash/*"
set wordpass to ""
Yes I know that for sudo the password must be entered, and the calling of the shell script from the AppleScript evokes the system dialog, where that password is entered. Yes, your script works. I’m still wondering why my attempt doesn’t - using my method, the shell command [format]rm -rf ~/.Trash/*[/format] is not executed, despite the password being entered.
I believe that your attempt was also directed to the wrong shell. I called your osascript, sans the first line—#!/bin/bash—from my desktop with the below code, and it worked as expected, including the prompt.
do shell script "/bin/sh " & (choose file)'s POSIX path's quoted form
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set pwd to ""
set pwd to display dialog "Please enter password for user MyUser…" default answer pwd with title "Password" with hidden answer
if button returned of pwd is "OK" then
set pwd to text returned of pwd
do shell script "rm -rf ~/.Trash/*" user name "MyUser" password pwd with administrator privileges
end if
Here is a better version that will loop on given a bad username/password combo until user cancels or gives valid credentials
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set pwd to ""
set dialogText to "Please enter password for user MyUser."
repeat
set pwd to display dialog dialogText default answer pwd with title "Enter Password…" with hidden answer
if button returned of pwd is "OK" then
set pwd to text returned of pwd
try
do shell script "rm -rf ~/.Trash/*" user name "MyUser" password pwd with administrator privileges
exit repeat
on error errStr number errorNumber
if errorNumber ≠ -60007 then -- The administrator user name or password was incorrect.
exit repeat
end if
set dialogText to "Error! The administrator user name or password was incorrect." & return & "Try again…"
end try
end if
end repeat
Make sure the loaded file is plain text. If you happen to be using Catalina, you may have a different default shell—zsh. This isn’t a necessary change, but avoiding the manual quoting is desirable.
[format]if osascript -e ‘do shell script “sudo rm -rf ~/.Trash/*” with administrator privileges’;
then
osascript -e 'display notification “Trash was force emptied.” with title “OK” ’
else
osascript -e 'display notification “Trash was NOT force emptied!” with title “ERROR” ’
fi[/format]
Well, I’ve tried many which ways, and post #2 from Marc Anthony works, which is always a plus! Thank you for that!
I was trying to get the external script working, so as to have the benefit of some sort of feedback notification, instead of it just working silently, but no variation of shell or whatever seems to work for that. Maybe an AppleScript equivalent is possible.
But that working script is obviously better than no result at all!
You can use regular dialog statements in a try block.
set wordpass to (display dialog "An admin password is requested for super user access." with icon stop with title "Take heed!" default answer "password")'s text returned
try
do shell script "printf " & wordpass's quoted form & space & "| sudo -S env_reset,timestamp_timeout=0 rm -rf ~/.Trash/*"
set wordpass to ""
display dialog "The trash was force emptied."
on error
display dialog "That force emptying didn't go so well."
end try
I can’t get any of these scripts to empty the trash in Sonoma 14.7. Granted, four years has passed since this thread was created, and clearly Apple has made changes to macOS.
Is there a modified script that force-empties the trash in Sonoma and Sequoia?
I totally agree with robertfern!
In the end, the safest way is still using Finder’s “empty trash.”
The rm -Rf option can cause unintended file deletions if there’s a symbolic link in the trash
(I’ve learned that the hard way when deleting from Containers… sigh).
So, for a safer way, you’ll need accessibility access for:
/usr/bin/sudo -u SOME_USER /usr/bin/osascript -e 'tell application "Finder" to empty the trash without warns before emptying'
Alternatively, if you have full disk access, you can use:
Looking at the script you posted in post #7, how would that need to be changed to just accept my user name and password please? I actually had to Google “sudoers” and it doesn’t look like something I’d like to mess with.
Edit: Also, if it matters, my terminal is using “zsh”.
I tried to run the rm -rf ~/.Trash/* command directly as a shell command and also as an AppleScript with the do shell script command, and this only worked if the app used to run the command had “Full Disk Access” and in most cases “Accessibility” permissions enabled. The apps I’m referring to include Terminal, Script Editor, Script Debugger, FastScripts, Script Menu, and osascript. Prompting for a password did not make a difference.
BTW, I certainly agree that the standard methods for emptying the trash should normally be used, but there does appear to be instances where stuff gets stuck in the trash. In those cases, other approaches are probably needed.