SecurityAgent window deactivated if running shell script w admin priv

Initial Problem: some files won’t delete on my computer, I need to frequently run “sudo rm -R” from terminal. So I built an applescript that can launch with a key command, and it works:

tell application "Finder" to set selection_POSIX to quoted form of (POSIX path of the (selection as alias))
do shell script "rm -R " & selection_POSIX with administrator privileges

The only problem now is that when I trigger the above applescript from Keyboard Maestro (the program I use to launch applescripts via key commands), the SecurityAgent window asking for my password comes up deactivated/background in Yosemite. This means I need to use the mouse to click on it instead of just typing my password straight away.

Solutions I’ve tried:
telling SecurityAgent to activate (tried both before and after shell script)
telling System events to tell process SecurityAgent to set frontmost to true (again, tried before and after)
running the shell script from within a tell System Events block, and using an ignoring application responses block to tell SecurityAgent to activate - in case it’s a parallel processing issue.

For some reason, I just can’t get that password request window to come up activated when triggered from a keystroke. Any ideas?

Model: MacBook Air
Browser: Safari 600.7.12
Operating System: Mac OS X (10.10)


a solution is to provide the password in the do shell script line

do shell script "rm -R " & selection_POSIX with administrator privileges password "myPassword"

Thanks, Stefan - thought about that, my concern there would be security related having my system password hanging out in a script… any way to do that more securely?

You may look at :

There is a neat answer to your problem.

Yvan KOENIG running Yosemite 10.10.4 in French (VALLAURIS, France) mardi 4 août

Merci Yvan, I thought about that too, but ultimately would prefer to just make the box frontmost so I can type in the password easily and securely, otherwise would need to update the algorithm with changing password, and still not confident it would be that secure as the algorithm would be plainly available in the script. Autres idées?