Change focus to the password dialog box

Hi,
This is an abbreviated part of a longer script, which at one stage, has to delete a selection of files and/or folders directly, bypassing the Trash.
The problem is that the password dialog box opened by the shell command is not focused and the cursor is left wherever it was. One user managed to type his password in a file name :o


tell application "Finder"
	activate
	set DeleteNow to selection as alias list
	if (count DeleteNow) = 0 then
		display dialog "There is nothing selected to delete now."
		error number -128
	end if
	set ShowList to {}
	repeat with i in DeleteNow
		copy i's name to the end of ShowList
	end repeat
	choose from list ShowList with prompt "This is your selection." & return & "Cancel if the list is incorrect or¬
	 you changed your mind." with title "** DELETE NOW, WITHOUT UNDO **" with empty selection allowed
	if result is false then error number -128
	try
		repeat with i in DeleteNow
			set path_i to quoted form of POSIX path of i as string
			
			-- ====================================================================================
			-- How can I change the focus to the password dialog box opened by the following command?
			-- ====================================================================================
			do shell script "rm -rf " & path_i with administrator privileges
		end repeat
	on error errMsg
		display dialog errMsg with title "Errors during delete attempts" with icon stop
	end try
end tell

How can I change the focus to the password dialog box, with the cursor in the password field, ready to accept typing?

Cheers,
Chris

This simple solution occurred to me after a good nigh sleep :wink:
Enclose the actual delete try block within a “tell current application to activate” block:


tell current application
	activate
	-- try block
	do shell script "rm -rf " & path_i with administrator privileges
	-- end try
end tell

This is the amended original script:


tell application "Finder"
	activate
	set DeleteNow to selection as alias list
	if (count DeleteNow) = 0 then
		display dialog "There is nothing selected to delete now."
		error number -128
	end if
	set ShowList to {}
	repeat with i in DeleteNow
		copy i's name to the end of ShowList
	end repeat
	choose from list ShowList with prompt "This is your selection." & return & "Cancel if the list is incorrect or¬
	 you changed your mind." with title "** DELETE NOW, WITHOUT UNDO **" with empty selection allowed
	if result is false then error number -128
	tell current application -- This block will change the focus to the password dialog box.
		activate
		try
			repeat with i in DeleteNow
				set path_i to quoted form of POSIX path of i as string
				do shell script "rm -rf " & path_i with administrator privileges
			end repeat
		on error errMsg
			display dialog errMsg with title "Errors during delete attempts" with icon stop
		end try
	end tell
end tell

Cheers!

This works great in 10.9. Thank you.

However I can’t get it to work in the 10.10 (Public Beta 4).

It seems like it should be possible as the BBedit Authenticated Save helper script (for the MAS version) still gets focus fine in 10.10. But they’re calling the script form a signed app.Does anyone have a solution that works in Yosemite?

A non-password dialog gets focus and it it proceeds the password dialog, that dialog will get focus as well. I currently using a dialog with 1 second timeout (couldn’t make it shorter) as a workaround.


tell current application
		activate
		display dialog "Trying to get focus." with title "Can't focus in Yosemite" buttons {"Cancel", "Idle"} cancel button "Cancel" giving up after (1)
		set output to (do shell script "printf '" & hostsLine & commentString & "' >> /etc/hosts" with administrator privileges)
	end tell