Applescript to retrieve account and password using keychain scripting

applescriptkeychain

How can I use keychain scripting to write a script in Applescript that retrieves the login and password of a website from the login keychain, given the website's URL?

Best Answer

If you know the exact name of the keychain item, you could use the following:

tell application "Keychain Scripting" to tell keychain "login.keychain" to get {account, password} of (first Internet key whose name is "www.google.com")

Thing is, Keychain Scripting is slow and quite buggy. For example, searching for a specific keychain item in the example above using name contains instead of name is does not work. You would have to use a repeat statement similar to what @Philip posted:

tell application "Keychain Scripting" to tell keychain "login.keychain"

    repeat with x from 1 to (count every Internet key)
        if name of Internet key x contains "Google" then
            return {account, password} of Internet key x
        end if

    end repeat
end tell

If you´re okay to use the command line and just want to look up stuff, I´d rather use: security find-internet-password -g -s www.google.com and then grep what you want.