Very new to Script Editor and AppleScript, but have a short (successful) script to empty “Trash” in all Apple Mail accounts. Trash is properly emptied, however, prompt to Cancel/Erase continues to appear, requiring manual response, which I would like to eliminate, but seem (so far) unable to resolve. Any help greatly appreciated! (Hope this is properly posted!)
Jeff Anderson
Script is here (to be executed via bash):
#!/bin/bash
osascript <<EOF
tell application “Mail” to activate
tell application “System Events”
tell process “Mail”
click menu item “In All Accounts…” of menu “Erase Deleted Items” of menu item “Erase Deleted Items” of menu “Mailbox” of menu bar 1
delay 0.5 – brief wait for the confirmation sheet
if exists sheet 1 of window 1 then
click button “Erase” of sheet 1 of window 1
end if
end tell
end tell
EOF
Note that you have an invalid comment following the delay.
This works for me here on 15.7.5. Dialog appears, and immediately dismisses.
tell application "Mail" to activate
tell application "System Events"
tell process "Mail"
ignoring application responses
click menu item "In All Accounts…" of menu "Erase Deleted Items" of menu item "Erase Deleted Items" of menu "Mailbox" of menu bar 1
end ignoring
delay 0.5 -- brief wait for the confirmation sheet
if exists sheet 1 of window 1 then
click button "Erase" of sheet 1 of window 1
end if
end tell
end tell
Awesome! That now works for me as well.
It seems my version of BBEdit perhaps “corrected” my – into an EM or EN dash. Will check on that…appreciate your noting that!
Thank you Paul so very much!!!
Jeff A
Paul…sorry to impose (and don’t wish to be a “pest”) but might I ask another question regarding this script?
I have discovered that as long as Apple Mail is not running, or is running/active with a Viewer Window open, the script is processed and no manual “Cancel/Erase” prompt is presented. However if Apple Mail is running with the Viewer Window minimized, or running with no Viewer Window open, the script is processed but a manual “Cancel/Erase” prompt always appears.
Might you have any thoughts on why that happens, or perhaps some kind of work-around?
Thank you so very much…
Jeff A
If the viewer window is closed the “abandoned” sheet becomes “window 1”.
tell application "Mail" to activate
tell application "System Events"
tell process "Mail"
ignoring application responses
click menu item "In All Accounts…" of menu "Erase Deleted Items" of menu item "Erase Deleted Items" of menu "Mailbox" of menu bar 1
end ignoring
delay 0.5 -- brief wait for the confirmation sheet
windows
if exists sheet 1 of window 1 then
click button "Erase" of sheet 1 of window 1
end if
if exists button "Erase" of window 1 then
click button "Erase" of window 1
end if
end tell
end tell
Ahh…got it (little by little)!
Again, thank you, thank you!