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