On OS X 10.8.x I had an Automator service set up to paste a password from clipboard into the password window that pops up for mounting an encrypted disk.
I don't wish to store the password in my keychain, and it is long and tedious to type out.
I never got the applescript within the Automator service quite right. It would generate an error, but it still got the job done (i.e. the password was pasted). Since upgrading to Yosemite it seems the script in the Automator service doesn't work at all. The error generated indicates "window 1" doesn't exist.
Here is what I had:
tell application "System Events" to tell process "SecurityAgent"
set value of text field 1 of window 1 to (the clipboard)
if exists (text field 2 of window 1) then
set value of text field 2 of window 1 to (the clipboard)
end if
click button 1 of group 1 of window 1
end tell
I don't recall where I got this from, but some of it may have come from here
I know very little about AppleScript, and would appreciate some help fine tuning (or completely rewriting) this script so that it does the following:
- Checks there is data in the clipboard
- Checks the SecurityAgent window is active (or in some way ensures it can be interacted with, to avoid generating an error)
- Pastes the content of clipboard
- Clicks the 'Unlock' button
I don't know if it is possible, but it would be handy if it also brought 1Password to the foreground and activated "Copy password" (which is a menu option in the "Items" menu). I can, however, have Automator actions perform that task. But it would be nice to see (for learning) how to achieve that with AppleScript.
Best Answer
With thanks to input from markhunte and tetsujin, I came up with the following solution. With all things considered, the exact requirements evolved to the following:
I came up with the following solution:
I made the whole script conditional on the presence of SecurityAgent, because this script will be assigned to a keyboard shortcut, and if that key combination is inadvertently selected by the user, I'd rather they not be bothered with an unexpected script process.
If there are ways in which I could refine or improve this solution, please advise. I am very happy to be learning more about AppleScript.
FINAL UPDATES: Had to make one final change. It turned out the button numbers for CANCEL and OK change around from the SecurityAgent instance for creating an encrypted disk/disk-image, and for when opening an encrypted disk/disk-image. So I had to modify the IF THEN statement right near the end to handle each scenario.